You reach a gondola lift, but it’s broken (what a surprise).
So you offer to help fix it.
The engine is missing a part.
Today’s input file is the engine schematic, a visual representation of the engine.
An example input looks like this:
Part 1
In the schematic, periods are empty spaces.
Any number adjacent (even diagonally) to a symbol is a part number.
The question asks what the sum is of all part numbers in the engine schematic.
In the example, only 2 numbers are not part numbers: 114 (top right) and 58 (middle right).
First, the engine schematic is built from the input as a 2D list where each point is a character (a point, a digit, or a symbol).
We iterate through the schematic, checking every digit.
While we do so, we keep track of a few temporary variables:
The current number so far (a number can have multiple digits)
If the current number is adjacent to a symbol
For every digit we encounter, those temporary variables get updated:
We concatenate the digit to the end of the current number.
We check if any point adjacent to the digit is a symbol
If the next horizontal point is not a digit (it’s either the end of a line, a symbol, or an empty space), the current number is complete.
At that point, we check if the current number is adjacent to a symbol.
If it is, we increment our final sum.
Finally, we reset the temporary variables before we start building up the next number.
In pseudocode:
Code
Part 2
The gondola starts working, but it’s going painfully slow!
The engineer tells you one of the gears in the engine is wrong.
On your schematic, a gear is marked as a star (*) that is adjacent to exactly two part numbers.
You can calculate the gear ratio of a gear by multiplying the numbers of the two adjacent parts.
The question asks to find the sum of all gear ratios.
The code is very similar to part 1.
The variable we keep track of throughout the loop changes (sum in part1), and one temporary variable.
The variable we keep track of throughout the loop is a map of all stars, with positions as keys, and a list of adjacent part numbers as value.
We iterate through the schematic, checking every digit.
While we do so, we keep track of a few temporary variables:
The current number so far (a number can have multiple digits)
A set of all star positions that are adjacent to the current number
For every digit we encounter, those temporary variables get updated:
We concatenate the digit to the end of the current number.
We add any adjacent star positions to the adjacent stars set.
If the next horizontal point is not a digit (it’s either the end of a line, a symbol, or an empty space), the current number is complete.
At that point, we add all stars in the set to the map we keep track of throughout the loop.
Finally, we reset the temporary variables before we start building up the next number.
After the loop over the engine schematics ends, we filter the map we kept track of so only the valid gears are left. (stars with exactly 2 adjacent numbers.)
We multiply those numbers together, and sum those products.