Compare commits

..

2 Commits

Author SHA1 Message Date
bb8075b0c0
Remove input files from repo 2022-12-07 13:36:11 +01:00
97b9ec2826
day3 2022-12-07 13:34:44 +01:00
5 changed files with 50 additions and 4766 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
input.txt

View File

@ -1,4 +1,4 @@
DAY = 2
DAY = 3
DIR = day$(DAY)

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

48
day3/main.hs Normal file
View File

@ -0,0 +1,48 @@
import System.IO
import Data.Char (ord)
import Data.Set (Set)
import qualified Data.Set as Set
import Data.List.Split
itemprio :: Char -> Int
itemprio c
| 'a' <= c && c <= 'z' = ord c - ord 'a' + 1
| 'A' <= c && c <= 'Z' = ord c - ord 'A' + 27
duplitems :: Ord a => [a] -> [a] -> [a]
duplitems c1 c2 = Set.elems $ Set.intersection s1 s2
where s1 = (Set.fromList c1)
s2 = (Set.fromList c2)
splitEqual :: Int -> [a] -> [[a]]
splitEqual n x = chunksOf (length x `div` n) x
rsprio :: String -> Int
rsprio x = itemprio $ head $ duplitems (head rs) (last rs)
where rs = splitEqual 2 x
grpprio :: [String] -> Int
grpprio = itemprio . head . foldr1 duplitems
handler :: String -> String
handler s = (show $ sum $ map rsprio $ lines s) ++ "\n" ++
(show $ sum $ map grpprio $ chunksOf 3 $ lines s) ++ "\n"
main :: IO ()
main = do
interact handler