--- /dev/null
+module Main(main) where
+
+import Data.List (transpose)
+import Data.Char (isLetter)
+import qualified Data.Map.Lazy as Map
+
+main :: IO ()
+main = do
+ text <- readFile "advent06.txt"
+ let message = lines text
+ part1 message
+ part2 message
+
+part1 :: [String] -> IO ()
+part1 message = do
+ print $ map (fst) $ map (mostCommon) $ map (countedLetters) $ transpose message
+
+part2 :: [String] -> IO ()
+part2 message = do
+ print $ map (fst) $ map (leastCommon) $ map (countedLetters) $ transpose message
+
+
+countedLetters :: String -> Map.Map Char Int
+countedLetters name = Map.fromListWith (+) [(c, 1) | c <- filter (isLetter) name]
+
+mostCommon = Map.foldlWithKey (mostCommonP) ('a', 0)
+
+mostCommonP (letter0, count0) letter count
+ | count > count0 = (letter, count)
+ | otherwise = (letter0, count0)
+
+leastCommon = Map.foldlWithKey (leastCommonP) ('a', maxBound :: Int)
+
+leastCommonP (letter0, count0) letter count
+ | count < count0 = (letter, count)
+ | otherwise = (letter0, count0)
\ No newline at end of file
+-- Better version of advent06.hs, using more standard library functions.
+
module Main(main) where
-import Data.List (transpose)
+import Data.List (transpose, maximum, minimum)
import Data.Char (isLetter)
+import Data.Tuple (swap)
import qualified Data.Map.Lazy as Map
main :: IO ()
part1 :: [String] -> IO ()
part1 message = do
- print $ map (fst) $ map (mostCommon) $ map (countedLetters) $ transpose message
+ putStrLn $ map (fst) $ map (mostCommon) $ map (countedLetters) $ transpose message
part2 :: [String] -> IO ()
part2 message = do
- print $ map (fst) $ map (leastCommon) $ map (countedLetters) $ transpose message
-
-
-countedLetters :: String -> Map.Map Char Int
-countedLetters name = Map.fromListWith (+) [(c, 1) | c <- filter (isLetter) name]
-
-mostCommon = Map.foldlWithKey (mostCommonP) ('a', 0)
+ putStrLn $ map (fst) $ map (leastCommon) $ map (countedLetters) $ transpose message
-mostCommonP (letter0, count0) letter count
- | count > count0 = (letter, count)
- | otherwise = (letter0, count0)
+countedLetters :: String -> [(Char, Int)]
+countedLetters name = Map.toList $ Map.fromListWith (+) [(c, 1) | c <- filter (isLetter) name]
-leastCommon = Map.foldlWithKey (leastCommonP) ('a', maxBound :: Int)
+mostCommon :: [(Char, Int)] -> (Char, Int)
+mostCommon = swap . maximum . map (swap)
-leastCommonP (letter0, count0) letter count
- | count < count0 = (letter, count)
- | otherwise = (letter0, count0)
\ No newline at end of file
+leastCommon :: [(Char, Int)] -> (Char, Int)
+leastCommon = swap . minimum . map (swap)