X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=advent06.hs;h=e2a41e7d539dd1aff78275e5251d7f3a7abe2945;hb=1f63e23a5c72a2aa7589a002e54dbb75e7a033bb;hp=77d40934c1d83388f9558c37d7948f5f7c766d13;hpb=5bc8c68da5691e57648112363a1df1eda7f0fe3a;p=advent-of-code-16.git diff --git a/advent06.hs b/advent06.hs index 77d4093..e2a41e7 100644 --- a/advent06.hs +++ b/advent06.hs @@ -1,8 +1,7 @@ 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 @@ -13,24 +12,11 @@ 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