77d40934c1d83388f9558c37d7948f5f7c766d13
[advent-of-code-16.git] / adventofcode16 / app / advent06-old.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 main :: IO ()
8 main = do
9 text <- readFile "advent06.txt"
10 let message = lines text
11 part1 message
12 part2 message
13
14 part1 :: [String] -> IO ()
15 part1 message = do
16 print $ map (fst) $ map (mostCommon) $ map (countedLetters) $ transpose message
17
18 part2 :: [String] -> IO ()
19 part2 message = do
20 print $ map (fst) $ map (leastCommon) $ map (countedLetters) $ transpose message
21
22
23 countedLetters :: String -> Map.Map Char Int
24 countedLetters name = Map.fromListWith (+) [(c, 1) | c <- filter (isLetter) name]
25
26 mostCommon = Map.foldlWithKey (mostCommonP) ('a', 0)
27
28 mostCommonP (letter0, count0) letter count
29 | count > count0 = (letter, count)
30 | otherwise = (letter0, count0)
31
32 leastCommon = Map.foldlWithKey (leastCommonP) ('a', maxBound :: Int)
33
34 leastCommonP (letter0, count0) letter count
35 | count < count0 = (letter, count)
36 | otherwise = (letter0, count0)