Function aoc::y22d08::y22d08

source ·
pub fn y22d08(input: &str, part: u32) -> u32
Expand description

The solution for the day eight challenge.

Given the input as a string and the part as an integer where 1 returns the number of trees that are visible and 2 returns the highest possible scenic score (viewing distance in all 4 directions multiplied together) we compute the answer for both parts one and two at the same time and then just return the desired answer.

We start by parsing the input into the vector of integer vectors as described above. We then loop through all of the interior trees one at a time. We look out in each direction (note the range reverse for looking left and up) to determine how many trees we can see until we get a tree that is taller or equal to the tree that we are inspecting (or the edge). If we reach the edge then the tree is visible from that direction and we can increase our visible tree counter. In part one, we could move on to the next tree directly (continue) but in part two we need to calculate the viewing distance from all directions. As we look at each other tree we increase the viewing distance for that direction until we get to a view-blocking tree or the edge. As described in the prompt the scenic score is calculated by multiplying the four viewing distances together. After inspecting all four directions we calculate the score and add it to the scenic score heap. If we want the highest scenic score (part 2) then we pop the heap, otherwise we return the total visible trees.

§Example

// probably read this from the input file...
let input = "33333\n34223\n32123\n32223\n33333\n";
assert_eq!(y22d08(input, 1), 17);
assert_eq!(y22d08(input, 2), 9);