Day 6: Signals and Noise


In [1]:
inputLines = lines <$> readFile "input/day6.txt"

In [2]:
import Control.Arrow
import Data.List (group, sort, transpose)

A function that extracts the most or least frequent item from a list


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"


'a'
'c'

Part 1: Find the most frequent character for each column


In [4]:
map mostFrequent . transpose <$> inputLines


"bjosfbce"

Part 2: Find the least frequent character for each column


In [5]:
map leastFrequent . transpose <$> inputLines


"veqfxzfx"