From 8509a884ff1ceaac80dc2cd356fc8a30a8f060c1 Mon Sep 17 00:00:00 2001 From: Max Nuding Date: Thu, 8 Dec 2022 07:53:26 +0000 Subject: [PATCH] Repalce for-loops by flat_map and map --- src/day08/mod.rs | 117 +++++++++++++++++++++++++---------------------- 1 file changed, 62 insertions(+), 55 deletions(-) diff --git a/src/day08/mod.rs b/src/day08/mod.rs index 85c8c55..e9ff9bf 100644 --- a/src/day08/mod.rs +++ b/src/day08/mod.rs @@ -11,66 +11,73 @@ pub fn run() { .collect::>(); let width = grid.first().unwrap().len(); - let mut count = 0; - let mut max_score = 0; - for (row, row_vec) in grid.iter().enumerate().skip(1) { - for (col, height) in row_vec.iter().enumerate().skip(1) { - 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 + let res = grid + .iter() + .enumerate() + .skip(1) + .flat_map(|(row, row_vec)| { + row_vec .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 - }; + .skip(1) + .map(|(col, height)| { + let mut sides_visible = 4; + let mut score = 1; - max_score = max_score.max(score); + 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 + }; - if sides_visible > 0 { - count += 1; - } - } - } + 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) + }) + .collect::>() + }) + .collect::>(); + + let count = res.iter().filter(|e| e.0).count(); + let max_score = res.iter().map(|e| e.1).max().unwrap(); #[cfg(feature = "part1")] {