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:
Tree | Max | Visible |
---|---|---|
3 | -1 | true |
0 | 3 | false |
3 | 3 | false |
7 | 3 | true |
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.