2022/day8: part 2
This commit is contained in:
1 file changed
+21
-2
+21
-2
@@ -1,4 +1,5 @@
|
||||
use aoc_runner_derive::{aoc, aoc_generator};
|
||||
use itertools::Itertools;
|
||||
use std::str::FromStr;
|
||||
|
||||
use grid::{Coord2d, Grid};
|
||||
@@ -49,7 +50,25 @@ fn part1(input: &Grid<u8>) -> u64 {
|
||||
|
||||
#[aoc(day8, part2)]
|
||||
fn part2(input: &Grid<u8>) -> u64 {
|
||||
todo!()
|
||||
let mut scenic_scores = input.same_shape(0usize);
|
||||
for tree in input.coord_iter() {
|
||||
let height = input.get(&tree);
|
||||
let left_score = (1..=tree.x)
|
||||
.take_while_inclusive(|x_ofs| input.get(&(tree.x - x_ofs, tree.y)) < height)
|
||||
.count();
|
||||
let right_score = (1..(input.width() as i64 - tree.x))
|
||||
.take_while_inclusive(|x_ofs| input.get(&(tree.x + x_ofs, tree.y)) < height)
|
||||
.count();
|
||||
let up_score = (1..=tree.y)
|
||||
.take_while_inclusive(|y_ofs| input.get(&(tree.x, tree.y - y_ofs)) < height)
|
||||
.count();
|
||||
let down_score = (1..(input.height() as i64 - tree.y))
|
||||
.take_while_inclusive(|y_ofs| input.get(&(tree.x, tree.y + y_ofs)) < height)
|
||||
.count();
|
||||
scenic_scores.set(&tree, left_score * right_score * up_score * down_score);
|
||||
}
|
||||
|
||||
*scenic_scores.item_iter().map(|i| i.value).max().unwrap() as u64
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -70,7 +89,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[case(EXAMPLE, 0)]
|
||||
#[case(EXAMPLE, 8)]
|
||||
fn part2_example(#[case] input: &str, #[case] expected: u64) {
|
||||
assert_eq!(part2(&parse(input)), expected);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user