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<_>>();
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::<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")]
{