Advent of Code 2022 day 1

Another December, another Advent of Code!

Day 1 was another gentle warmup, where I spent most of my time looking at the Data.List.Split library and not seeing the function I needed.

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

Code

You can get the code from my locally-hosted Git repo, or from Gitlab.