This is Day 3 of Advent of Code 2023! If you would like to solve the problems before looking at the solution, you can find them here.

In part 1 of Day 3, we're working with a grid with numbers (part numbers) and symbols (gears) sprinkled around in a field of dots. We would like to find out what part numbers are adjacent to a gear. One of the things that I had to consider very early on was the edge cases - in this case, literally the edge cases - where the part numbers in the first/last row/column would have fewer neighbors than the rest of part numbers in the grid. I fixed this by adding a row of dots above the first row and below the last row, and a dot around each line. Once that was done, calculating if a part number had a gear next to it was pretty straightforward. I learnt about maps and closures working on this, and it feels like a pretty nifty thing to know. Click here to jump to the part 2 discussion.

For part 2 of Day 3, this is where things get complicated, and this feels like the first real puzzler this season. In the first part, we were focusing on part numbers but here, we need to find gears that have exactly two part numbers next to it. Instead of clumping all neighbors together, I had to parse the left, right, top, and bottom neighbors separately, and going through the neighbors to find asterisks. I then used a HashMap to connect asterisks to their neighboring part numbers and then filtering the HashMap to only include asterisks that had exactly two neighbors. The part numbers were then multiplied to obtain a gear ratio for each gear, which was then summed for the final answer.

That's all folks! If you made it this far, enjoy some AI art generated by u/dimpopo using the prompt for this puzzle.

Cheers,

Devang