Function aoc::y23d03::y23d03p2

source ·
pub fn y23d03p2(input: &str) -> u32
Expand description

The solution for part two of the day three challenge.

As in part one we take the problem input as a string. We then parse the input keeping track of the numbers as we see them (similarly to part one) and also all of the coordinates of the * characters as we need them to find the gears. Once we have all of the numbers (and their starting x, ending x, and y coordinates) we loop through all of the stars. For each star we check the border of all of the numbers to see if there is any overlap with the star (which means that they are adjacent). If they are then we add the number to the neighbors vector. If the number of neighbors is exactly two (given by the prompt) then we multiply them together and add them to the final sum.

§Example

// probably read this from the input file...
let input = "123.5\n...*.\n.....\n..456";
assert_eq!(y23d03p2(input), 615);