module Main(main) where
-import Data.List (transpose)
-import Data.Char (isLetter)
-import qualified Data.Map.Lazy as Map
+import Data.List (transpose, maximum, minimum, sort, group)
+import Data.Tuple (swap)
main :: IO ()
main = do
part1 :: [String] -> IO ()
part1 message = do
- print $ map (fst) $ map (mostCommon) $ map (countedLetters) $ transpose message
+ putStrLn $ map (snd . maximum . counts) $ transpose message
part2 :: [String] -> IO ()
part2 message = do
- print $ map (fst) $ map (leastCommon) $ map (countedLetters) $ transpose message
+ putStrLn $ map (snd . minimum . counts) $ 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
+counts :: (Eq a, Ord a) => [a] -> [(Int, a)]
+counts = map (\g -> (length g, head g)) . group . sort
\ No newline at end of file