X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=advent06.hs;h=e2a41e7d539dd1aff78275e5251d7f3a7abe2945;hb=b8d1abb7cae751efd0fbbb1a2f5e416132ad66d2;hp=4d99fd2930df872e7febb52274966a72521a4b5a;hpb=d7f603546349671e29d25bc65c4eab34121eb8ed;p=advent-of-code-16.git diff --git a/advent06.hs b/advent06.hs index 4d99fd2..e2a41e7 100644 --- a/advent06.hs +++ b/advent06.hs @@ -1,12 +1,7 @@ module Main(main) where -import Data.List (last, intersperse, sortBy, intercalate, isInfixOf, transpose) -import Data.List.Split (splitOn) -import Data.Char (isLetter, ord, chr) -import qualified Data.Map.Lazy as Map - - -input = "cxdnnyjw" +import Data.List (transpose, maximum, minimum, sort, group) +import Data.Tuple (swap) main :: IO () main = do @@ -15,28 +10,13 @@ main = do part1 message part2 message - 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 - - - -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) + putStrLn $ map (snd . minimum . counts) $ transpose message -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