]> localhost Git - adventofcode2018.git/blame_incremental - day02/boxes.hs
Day 3: Extract unoverlap into its own function
[adventofcode2018.git] / day02 / boxes.hs
... / ...
CommitLineData
1import qualified Data.Map as M
2
3main :: IO ()
4main = 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
12countN :: Int -> [String] -> Int
13countN 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
19countChar :: String -> M.Map Char Int
20countChar = 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
24abc :: [String] -> String
25abc (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
31diff :: Eq a => [a] -> [a] -> [a]
32diff [] [] = []
33diff (a:as) (b:bs)
34 | a == b = (a : diff as bs)
35 | otherwise = diff as bs