From: Neil Smith Date: Tue, 6 Dec 2016 10:14:18 +0000 (+0000) Subject: Removed the Data.Map X-Git-Url: https://git.njae.me.uk/?p=advent-of-code-16.git;a=commitdiff_plain;h=3b8c2b217a88f32329e5453296887b3c11f81841 Removed the Data.Map --- 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