From d23fee8c4be7ab6014b4eb31b3ebc28a3c4d468d Mon Sep 17 00:00:00 2001 From: Max Nuding Date: Thu, 8 Dec 2022 08:27:43 +0000 Subject: [PATCH] Improve score/is_visible calculation --- src/day08/mod.rs | 91 ++++++++++++++++++++++-------------------------- 1 file changed, 42 insertions(+), 49 deletions(-) diff --git a/src/day08/mod.rs b/src/day08/mod.rs index 3c4ae76..86305bb 100644 --- a/src/day08/mod.rs +++ b/src/day08/mod.rs @@ -22,55 +22,48 @@ pub fn run() { .enumerate() .skip(1) .map(|(col, height)| { - let mut sides_visible = 4; - let mut score = 1; - - score *= if let Some(sc_score) = (0..col) - .rev() - .map(|cc| (col - cc, row_vec[cc])) - .find(|(_sc_score, other_height)| other_height >= height) - { - sides_visible -= 1; - sc_score.0 - } else { - col - }; - - score *= if let Some(sc_score) = (col + 1..width) - .map(|cc| (cc - col, row_vec[cc])) - .find(|(_sc_score, other_height)| other_height >= height) - { - sides_visible -= 1; - sc_score.0 - } else { - width - col - 1 - }; - - score *= if let Some(sc_score) = (0..row) - .rev() - .map(|rr| (row - rr, grid[rr][col])) - .find(|(_sc_score, other_height)| other_height >= height) - { - sides_visible -= 1; - sc_score.0 - } else { - row - }; - - score *= if let Some(sc_score) = grid - .iter() - .enumerate() - .skip(row + 1) - .map(|(rr, l)| (rr - row, l[col])) - .find(|(_sc_score, other_height)| other_height >= height) - { - sides_visible -= 1; - sc_score.0 - } else { - grid.len() - row - 1 - }; - - (sides_visible > 0, score) + let values = [ + if let Some(sc_score) = (0..col) + .rev() + .map(|col_left| (col - col_left, row_vec[col_left])) + .find(|(_sc_score, other_height)| other_height >= height) + { + (1, sc_score.0) + } else { + (0, col) + }, + if let Some(sc_score) = (col + 1..width) + .map(|col_right| (col_right - col, row_vec[col_right])) + .find(|(_sc_score, other_height)| other_height >= height) + { + (1, sc_score.0) + } else { + (0, width - col - 1) + }, + if let Some(sc_score) = (0..row) + .rev() + .map(|row_above| (row - row_above, grid[row_above][col])) + .find(|(_sc_score, other_height)| other_height >= height) + { + (1, sc_score.0) + } else { + (0, row) + }, + if let Some(sc_score) = grid + .iter() + .enumerate() + .skip(row + 1) + .map(|(row_below, l)| (row_below - row, l[col])) + .find(|(_sc_score, other_height)| other_height >= height) + { + (1, sc_score.0) + } else { + (0, grid.len() - row - 1) + }, + ]; + let is_visible = values.map(|e| e.0).iter().sum::() < 4; + let scenic = values.map(|e| e.1).iter().product::(); + (is_visible, scenic) }) .collect::>() })