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,66 +11,73 @@ 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()
let mut sides_visible = 4; .enumerate()
let mut score = 1; .skip(1)
.flat_map(|(row, row_vec)| {
score *= if let Some(sc_score) = (0..col) row_vec
.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() .iter()
.enumerate() .enumerate()
.skip(row + 1) .skip(1)
.map(|(rr, l)| (rr - row, l[col])) .map(|(col, height)| {
.find(|(_sc_score, other_height)| other_height >= height) let mut sides_visible = 4;
{ let mut score = 1;
sides_visible -= 1;
sc_score.0
} else {
grid.len() - row - 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 { score *= if let Some(sc_score) = (col + 1..width)
count += 1; .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::<Vec<_>>()
})
.collect::<Vec<_>>();
let count = res.iter().filter(|e| e.0).count();
let max_score = res.iter().map(|e| e.1).max().unwrap();
#[cfg(feature = "part1")] #[cfg(feature = "part1")]
{ {