Function aoc::year2022::day08::part1

source ·
pub fn part1(input: &(usize, Vec<i8>)) -> usize
Expand description

Calculate visible trees using a rolling maximum for each row and column in left, right, up and down directions.

Using the top row of the sample and going left to right:

TreeMaxVisible
3-1true
03false
33false
73true

The last tree in each row and column doesn’t need to be checked since it’s covered by the loop in the opposite direction.

A tree is visible if it can be seen from any direction. As a minor optimization, rather than have 4 separate loops pairs, the left, right, up and down loops are all rolled into one pair, to amortise the cost of loop logic.

The 4 corners trees don’t need to be checked since they’re always visible so they’re added directly to the total.