Repalce for-loops by flat_map and map

This commit is contained in:
Max Nuding 2022-12-08 07:53:26 +00:00
parent b4f9e56227
commit 8509a884ff

View File

@ -11,11 +11,17 @@ pub fn run() {
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let width = grid.first().unwrap().len(); 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) { let res = grid
for (col, height) in row_vec.iter().enumerate().skip(1) { .iter()
.enumerate()
.skip(1)
.flat_map(|(row, row_vec)| {
row_vec
.iter()
.enumerate()
.skip(1)
.map(|(col, height)| {
let mut sides_visible = 4; let mut sides_visible = 4;
let mut score = 1; let mut score = 1;
@ -64,13 +70,14 @@ pub fn run() {
grid.len() - row - 1 grid.len() - row - 1
}; };
max_score = max_score.max(score); (sides_visible > 0, score)
})
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
if sides_visible > 0 { let count = res.iter().filter(|e| e.0).count();
count += 1; let max_score = res.iter().map(|e| e.1).max().unwrap();
}
}
}
#[cfg(feature = "part1")] #[cfg(feature = "part1")]
{ {