From f8e2ed0cb7e6ca7c06a3566a52a5f4a438e4c318 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Thu, 8 Dec 2022 16:38:46 -0800 Subject: [PATCH] done with part 2 --- 2022-aoc/src/d8.rs | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/2022-aoc/src/d8.rs b/2022-aoc/src/d8.rs index a54f677..b153127 100644 --- a/2022-aoc/src/d8.rs +++ b/2022-aoc/src/d8.rs @@ -5,7 +5,6 @@ use aoc_runner_derive::{aoc as aoc_run, aoc_generator}; use ndarray::prelude::*; type Forest = Array2; -type Tree = (i8, i8); #[aoc_generator(day8)] fn parse_input_day1(input: &str) -> Forest { @@ -77,5 +76,38 @@ fn part1(input: &Forest) -> usize { #[aoc_run(day8, part2)] fn part2(input: &Forest) -> usize { - 0 + let nrows = input.nrows(); + let ncols = input.ncols(); + let mut max = 0; + for row in 0..nrows { + for col in 0..ncols { + let (e, w) = check_span(col, &input.row(row)); + let (n, s) = check_span(row, &input.column(col)); + max = (e * w * n * s).max(max); + } + } + max +} + +fn check_span(addr: usize, span: &ArrayView1) -> (usize, usize) { + let height = span[addr]; + let (mut a, mut b) = (0, 0); + // + let neg = span.slice(s![0..addr]); + let pos = span.slice(s![addr..]); + for e in neg.iter().rev() { + a += 1; + if *e >= height { + break; + } + } + + for e in pos.iter().skip(1) { + b += 1; + if *e >= height { + break; + } + } + + (a, b) }