projects
/
advent-of-code-16.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
43207f4
)
Removed the Data.Map
author
Neil Smith
<neil.git@njae.me.uk>
Tue, 6 Dec 2016 10:14:18 +0000
(10:14 +0000)
committer
Neil Smith
<neil.git@njae.me.uk>
Tue, 6 Dec 2016 10:14:18 +0000
(10:14 +0000)
advent06.hs
patch
|
blob
|
history
diff --git
a/advent06.hs
b/advent06.hs
index 0d32e4cdd89d8a7b81d70a5dcf3df0c0b8dda4e1..e2a41e7d539dd1aff78275e5251d7f3a7abe2945 100644
(file)
--- a/
advent06.hs
+++ b/
advent06.hs
@@
-1,11
+1,7
@@
--- Better version of advent06.hs, using more standard library functions.
-
module Main(main) where
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 Data.Tuple (swap)
-import qualified Data.Map.Lazy as Map
main :: IO ()
main = do
main :: IO ()
main = do
@@
-16,17
+12,11
@@
main = do
part1 :: [String] -> IO ()
part1 message = do
part1 :: [String] -> IO ()
part1 message = do
- putStrLn $ map (
fst) $ map (mostCommon) $ map (countedLetter
s) $ transpose message
+ putStrLn $ map (
snd . maximum . count
s) $ transpose message
part2 :: [String] -> IO ()
part2 message = do
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