X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=advent06.hs;h=e2a41e7d539dd1aff78275e5251d7f3a7abe2945;hb=22109336eb81722af230c53ef5938475a846efb8;hp=0d32e4cdd89d8a7b81d70a5dcf3df0c0b8dda4e1;hpb=43207f4732674c96772536c32c4ed051de7848f6;p=advent-of-code-16.git diff --git a/advent06.hs b/advent06.hs index 0d32e4c..e2a41e7 100644 --- a/advent06.hs +++ b/advent06.hs @@ -1,11 +1,7 @@ --- Better version of advent06.hs, using more standard library functions. - module Main(main) where -import Data.List (transpose, maximum, minimum) -import Data.Char (isLetter) +import Data.List (transpose, maximum, minimum, sort, group) import Data.Tuple (swap) -import qualified Data.Map.Lazy as Map main :: IO () main = do @@ -16,17 +12,11 @@ main = do part1 :: [String] -> IO () part1 message = do - putStrLn $ map (fst) $ map (mostCommon) $ map (countedLetters) $ transpose message + putStrLn $ map (snd . maximum . counts) $ transpose message part2 :: [String] -> IO () part2 message = do - putStrLn $ map (fst) $ map (leastCommon) $ map (countedLetters) $ transpose message - -countedLetters :: String -> [(Char, Int)] -countedLetters name = Map.toList $ Map.fromListWith (+) [(c, 1) | c <- filter (isLetter) name] - -mostCommon :: [(Char, Int)] -> (Char, Int) -mostCommon = swap . maximum . map (swap) + putStrLn $ map (snd . minimum . counts) $ transpose message -leastCommon :: [(Char, Int)] -> (Char, Int) -leastCommon = swap . minimum . map (swap) +counts :: (Eq a, Ord a) => [a] -> [(Int, a)] +counts = map (\g -> (length g, head g)) . group . sort \ No newline at end of file