1 module Main(main) where
3 import Data.List (transpose)
4 import Data.Char (isLetter)
5 import qualified Data.Map.Lazy as Map
9 text <- readFile "advent06.txt"
10 let message = lines text
14 part1 :: [String] -> IO ()
16 print $ map (fst) $ map (mostCommon) $ map (countedLetters) $ transpose message
18 part2 :: [String] -> IO ()
20 print $ map (fst) $ map (leastCommon) $ map (countedLetters) $ transpose message
23 countedLetters :: String -> Map.Map Char Int
24 countedLetters name = Map.fromListWith (+) [(c, 1) | c <- filter (isLetter) name]
26 mostCommon = Map.foldlWithKey (mostCommonP) ('a', 0)
28 mostCommonP (letter0, count0) letter count
29 | count > count0 = (letter, count)
30 | otherwise = (letter0, count0)
32 leastCommon = Map.foldlWithKey (leastCommonP) ('a', maxBound :: Int)
34 leastCommonP (letter0, count0) letter count
35 | count < count0 = (letter, count)
36 | otherwise = (letter0, count0)