And we're back to Advent of Code for another year!

Day 1 is the usual gentle warm-up, and this problem has a shape that fits nicely into Haskell's model. We take a list of numbers, apply some manipulations to it, and read the result.

In this case, the steps are (for part 1):

  1. Sort the numbers sort
  2. Group them group
  3. Transform each group into a pair of (group length, first item of group) fmap ...
  4. Sort the groups sort
  5. Reverse order (the three largest size) reverse
  6. Take the first three take 3
  7. Find the IDs fmap snd
  8. Multiply them product

Each step is annotated with the function that does it.

Function application goes from right t0 left, to this is written as

part1 ids = product $ fmap snd $ take 3 $ reverse $ sort $ fmap (\g -> (length g, head g)) $ group $ sort ids

Part 2 is the same, only without the reverse step.

Code

You can get the code from Codeberg.