Tidied imports
[advent-of-code-16.git] / advent06.hs
1 module Main(main) where
2
3 import Data.List (transpose)
4 import Data.Char (isLetter)
5 import qualified Data.Map.Lazy as Map
6
7
8 input = "cxdnnyjw"
9
10 main :: IO ()
11 main = do
12 text <- readFile "advent06.txt"
13 let message = lines text
14 part1 message
15 part2 message
16
17
18 part1 :: [String] -> IO ()
19 part1 message = do
20 print $ map (fst) $ map (mostCommon) $ map (countedLetters) $ transpose message
21
22 part2 :: [String] -> IO ()
23 part2 message = do
24 print $ map (fst) $ map (leastCommon) $ map (countedLetters) $ transpose message
25
26
27
28 countedLetters :: String -> Map.Map Char Int
29 countedLetters name = Map.fromListWith (+) [(c, 1) | c <- filter (isLetter) name]
30
31 mostCommon = Map.foldlWithKey (mostCommonP) ('a', 0)
32
33 mostCommonP (letter0, count0) letter count
34 | count > count0 = (letter, count)
35 | otherwise = (letter0, count0)
36
37 leastCommon = Map.foldlWithKey (leastCommonP) ('a', maxBound :: Int)
38
39 leastCommonP (letter0, count0) letter count
40 | count < count0 = (letter, count)
41 | otherwise = (letter0, count0)