Day 3 - Discoded/adventofcode GitHub Wiki

Day 3

The engineer explains that an engine part seems to be missing from the engine, but nobody can figure out which one. If you can add up all the part numbers in the engine schematic, it should be easy to work out which part is missing.

The engine schematic (your puzzle input) consists of a visual representation of the engine. There are lots of numbers and symbols you don't really understand, but apparently any number adjacent to a symbol, even diagonally, is a "part number" and should be included in your sum. (Periods (.) do not count as a symbol.)

Input:

467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..

Output:

467, 35, 633, 617, 592, 755, 664, 598

Final answer: 4361 (Sum of the part numbers 467+35+633...+598)

Solution:

Two rare cases: First line doesnt have a line above it Last line does not have a line below it Another two cases where the number is directly on the edge, in which the slot directly left or right does not exist. There are also cases where these two are combined.

Case 1 (First Line):
-----------------------
467. or .114.  or ..114
...*    .....     .....
-----------------------

Regular Case:
-----------------------
..*.    .....    .....    ....
.35. or .633. or 633.. or .633
....    .#...    #....    #...
-----------------------
Case 2 (Last Line):
-----------------------
...$.    .*...     ..*..
.664. or .598. or  ..598
------------------------
Handle first and last case.
Handling base case:
    For each line, find symbol:
        if symbol, check the 9 adjacent spots:
            If number found, find the whole number,
            Add to sum