Today’s location to search for the chief historian is familiar (Again! Makes sense, you’ve been to many interesting places.).
You remember visiting it.
As is tradition, the an elf there asks for your help with something (it’s their printer, yaaaaaaaay).
They want to print updates to some manual (at least they are saving paper and not printing the entire thing again).
Your puzzle input today are printing instructions.
The updates need to be printed in a specific order.
The first part of the input consists of the ordering rules. X|Y notation, where X and Y are both numbers, means that page number Xmust be printed before page number Y.
The second part of your input is a list of updates, one per line.
Each update has several page numbers, separated by a comma.
An example input looks like this:
Apparently for today, each input is constructed so each possible page number pair is represented in the ordering rules.
Part 1
Start by printing the updates that are already correctly ordered.
The question asks for the sum of the middle page number from the correctly-ordered updates.
I chose 2 solving methods again today, I don’t know which one I like more, you decide!
The first one builds up a map of orderings that maps each page number to a list of numbers that come before it.
The second one builds up a map of orderings for each 2 number pair.
eg. (47, 53) maps to Less because of the 47|53 rule.
The code checks if an update is ordered correctly using those maps, and if it is adds the middle page number to a sum.
Option 1: Map numbers to a list of numbers
Option 2: Build a map of individual orderings
Part 2
Now print the incorrectly ordered updates.
The question asks for the sum of the middle page number from the incorrectly-ordered updates.
A small change to the code from part 1.
If an update is already ordered, skip it, if it is not, sort it, and add to the sum.