Day 9
[advent-of-code-16.git] / advent06.hs
index 4d99fd2930df872e7febb52274966a72521a4b5a..e2a41e7d539dd1aff78275e5251d7f3a7abe2945 100644 (file)
@@ -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