Begin Day 4, implement parser
[adventofcode2018.git] / day02 / boxes.hs
1 import qualified Data.Map as M
2
3 main :: IO ()
4 main = do
5 input <- getContents
6 let ids = lines input
7 let count_twos = countN 2 ids
8 let count_threes = countN 3 ids
9 print (count_twos * count_threes)
10 putStrLn . abc $ ids
11
12 countN :: Int -> [String] -> Int
13 countN check_count = sum . map (go check_count)
14 where go :: Int -> String -> Int
15 go cc test = if any (== cc) . M.elems . countChar $ test
16 then 1
17 else 0
18
19 countChar :: String -> M.Map Char Int
20 countChar = foldl go M.empty
21 where go :: M.Map Char Int -> Char -> M.Map Char Int
22 go chmap char = M.insert char ((M.findWithDefault 0 char chmap) + 1) chmap
23
24 abc :: [String] -> String
25 abc (i:is)
26 | null test = abc is
27 | otherwise = head test
28 where cor_len = (length i) - 1
29 test = filter (\s -> (length s) == cor_len) . map (diff i) $ is
30
31 diff :: Eq a => [a] -> [a] -> [a]
32 diff [] [] = []
33 diff (a:as) (b:bs)
34 | a == b = (a : diff as bs)
35 | otherwise = diff as bs