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