In [1]:
inputLines = lines <$> readFile "input/day6.txt"
In [2]:
import Control.Arrow
import Data.List (group, sort, transpose)
In [3]:
-- Note: (length &&& head) is equivalent to (\x -> (length x, head x))
selectItemFromGroupedList :: (Eq a, Ord a) => ([(Int, a)] -> (Int, a)) -> [a] -> a
selectItemFromGroupedList selector = snd . selector . map (length &&& head) . group . sort
mostFrequent :: (Eq a, Ord a) => [a] -> a
mostFrequent = selectItemFromGroupedList maximum
leastFrequent :: (Eq a, Ord a) => [a] -> a
leastFrequent = selectItemFromGroupedList minimum
-- check that it works
mostFrequent "abcaba"
leastFrequent "abcaba"
In [4]:
map mostFrequent . transpose <$> inputLines
In [5]:
map leastFrequent . transpose <$> inputLines