Another December, another Advent of Code!
I needed to convert the input into a list of groups of numbers.
lines would split the input into lines, then I eventually noticed
splitWhen in Data.List.Split did exactly what I needed to split the data into groups. Nested mappings of
read did the conversion into numbers.
At this point, part 1 was summing the groups and finding the largest sum. Part 2 involved sorting the group totals and summing the largest three. The code is a direct expression of the problem.
main :: IO () main = do dataFileName <- getDataFileName numStrs <- readFile dataFileName let calories = fmap (fmap (read @Int)) $ splitWhen null $ lines numStrs print $ part1 calories print $ part2 calories part1 :: [[Int]] -> Int part1 = maximum . fmap sum part2 :: [[Int]] -> Int part2 = sum . take 3 . reverse . sort . fmap sum