From 82f4503b3e9fc0bede3975c5d41ad95c9fc8d2fb Mon Sep 17 00:00:00 2001 From: Dory Date: Sun, 10 Mar 2024 22:56:04 -0700 Subject: [PATCH] d8p2 --- day8/src/main.rs | 82 +++++++++++++++++++++++------------------------ day8/src/main1.rs | 59 ++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 42 deletions(-) create mode 100644 day8/src/main1.rs diff --git a/day8/src/main.rs b/day8/src/main.rs index 0a4b117..225e749 100644 --- a/day8/src/main.rs +++ b/day8/src/main.rs @@ -4,55 +4,53 @@ use std::fs; fn main() { let input = fs::read_to_string(&env::args().nth(1).unwrap()).unwrap(); let forest: Vec> = input.lines() - .map(|line| line.chars().map(|c| c.to_digit(10).unwrap() as i8).collect()) + .map(|line| line.chars() + .map(|c| c.to_digit(10).unwrap() as i8).collect()) .collect(); let nrows = forest.len(); let ncols = forest[0].len(); - let min_from_left: Vec> = forest.iter() - .map(|row| row.iter() - .scan(-1, |h, &tree| { - let old = *h; *h = i8::max(*h as i8, tree); Some(old) - }) - .collect()) - .collect(); + let mut scores = vec![vec![1; ncols]; nrows]; + for r in 1..nrows-1 { + for c in 1..ncols-1 { + let h = forest[r][c]; - let min_from_right: Vec> = forest.iter() - .map(|row: &Vec| row.iter() - .rev() - .scan(-1, |h, &tree| { - let old = *h; *h = i8::max(*h as i8, tree); Some(old) - }) - .collect::>().iter() - .rev().map(|x| *x) - .collect()) - .collect(); - - let mut min_from_top: Vec> = vec![vec![-1; ncols]; nrows]; - for c in 0..ncols { - for r in 1..nrows { - min_from_top[r][c] = i8::max(min_from_top[r-1][c], forest[r-1][c]); - } - } - - let mut min_from_bot: Vec> = vec![vec![-1; ncols]; nrows]; - for c in 0..ncols { - for r in (0..nrows-1).rev() { - min_from_bot[r][c] = i8::max(min_from_bot[r+1][c], forest[r+1][c]); - } - } - - let mut sum = 0; - for r in 0..nrows { - for c in 0..ncols { - if forest[r][c] > min_from_left[r][c] - || forest[r][c] > min_from_right[r][c] - || forest[r][c] > min_from_top[r][c] - || forest[r][c] > min_from_bot[r][c] { - sum += 1; + let mut j = c - 1; + let mut count = 0; + while j > 0 && forest[r][j] < h { + count += 1; + j -= 1; } + scores[r][c] *= count + 1; + + let mut j = c + 1; + let mut count = 0; + while j < ncols-1 && forest[r][j] < h { + count += 1; + j += 1; + } + scores[r][c] *= count + 1; + + let mut i = r - 1; + let mut count = 0; + while i > 0 && forest[i][c] < h { + count += 1; + i -= 1; + } + scores[r][c] *= count + 1; + + let mut i = r + 1; + let mut count = 0; + while i < nrows-1 && forest[i][c] < h { + count += 1; + i += 1; + } + scores[r][c] *= count + 1; + } } - println!("{:?}", sum); + + let answer = scores.iter().map(|row| row.iter().max()).max(); + println!("{:?}", answer); } diff --git a/day8/src/main1.rs b/day8/src/main1.rs new file mode 100644 index 0000000..6eb7987 --- /dev/null +++ b/day8/src/main1.rs @@ -0,0 +1,59 @@ +use std::env; +use std::fs; + +fn main() { + let input = fs::read_to_string(&env::args().nth(1).unwrap()).unwrap(); + let forest: Vec> = input.lines() + .map(|line| line.chars() + .map(|c| c.to_digit(10).unwrap() as i8).collect()) + .collect(); + let nrows = forest.len(); + let ncols = forest[0].len(); + + let min_from_left: Vec> = forest.iter() + .map(|row| row.iter() + .scan(-1, |h, &tree| { + let old = *h; *h = i8::max(*h as i8, tree); Some(old) + }) + .collect()) + .collect(); + + let min_from_right: Vec> = forest.iter() + .map(|row: &Vec| row.iter() + .rev() + .scan(-1, |h, &tree| { + let old = *h; *h = i8::max(*h as i8, tree); Some(old) + }) + .collect::>().iter() + .rev().map(|x| *x) + .collect()) + .collect(); + + let mut min_from_top: Vec> = vec![vec![-1; ncols]; nrows]; + for c in 0..ncols { + for r in 1..nrows { + min_from_top[r][c] = i8::max(min_from_top[r-1][c], forest[r-1][c]); + } + } + + let mut min_from_bot: Vec> = vec![vec![-1; ncols]; nrows]; + for c in 0..ncols { + for r in (0..nrows-1).rev() { + min_from_bot[r][c] = i8::max(min_from_bot[r+1][c], forest[r+1][c]); + } + } + + let mut sum = 0; + for r in 0..nrows { + for c in 0..ncols { + if forest[r][c] > min_from_left[r][c] + || forest[r][c] > min_from_right[r][c] + || forest[r][c] > min_from_top[r][c] + || forest[r][c] > min_from_bot[r][c] { + sum += 1; + } + } + } + println!("{:?}", sum); +} +