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):
- Sort the numbers
sort - Group them
group - Transform each group into a pair of (group length, first item of group)
fmap ... - Sort the groups
sort - Reverse order (the three largest size)
reverse - Take the first three
take 3 - Find the IDs
fmap snd - 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 idsPart 2 is the same, only without the reverse step.
Code
You can get the code from Codeberg.