Improve score/is_visible calculation

This commit is contained in:
Max Nuding 2022-12-08 08:27:43 +00:00
parent ab0889b661
commit d23fee8c4b

View File

@ -22,55 +22,48 @@ pub fn run() {
.enumerate() .enumerate()
.skip(1) .skip(1)
.map(|(col, height)| { .map(|(col, height)| {
let mut sides_visible = 4; let values = [
let mut score = 1; if let Some(sc_score) = (0..col)
.rev()
score *= if let Some(sc_score) = (0..col) .map(|col_left| (col - col_left, row_vec[col_left]))
.rev() .find(|(_sc_score, other_height)| other_height >= height)
.map(|cc| (col - cc, row_vec[cc])) {
.find(|(_sc_score, other_height)| other_height >= height) (1, sc_score.0)
{ } else {
sides_visible -= 1; (0, col)
sc_score.0 },
} else { if let Some(sc_score) = (col + 1..width)
col .map(|col_right| (col_right - col, row_vec[col_right]))
}; .find(|(_sc_score, other_height)| other_height >= height)
{
score *= if let Some(sc_score) = (col + 1..width) (1, sc_score.0)
.map(|cc| (cc - col, row_vec[cc])) } else {
.find(|(_sc_score, other_height)| other_height >= height) (0, width - col - 1)
{ },
sides_visible -= 1; if let Some(sc_score) = (0..row)
sc_score.0 .rev()
} else { .map(|row_above| (row - row_above, grid[row_above][col]))
width - col - 1 .find(|(_sc_score, other_height)| other_height >= height)
}; {
(1, sc_score.0)
score *= if let Some(sc_score) = (0..row) } else {
.rev() (0, row)
.map(|rr| (row - rr, grid[rr][col])) },
.find(|(_sc_score, other_height)| other_height >= height) if let Some(sc_score) = grid
{ .iter()
sides_visible -= 1; .enumerate()
sc_score.0 .skip(row + 1)
} else { .map(|(row_below, l)| (row_below - row, l[col]))
row .find(|(_sc_score, other_height)| other_height >= height)
}; {
(1, sc_score.0)
score *= if let Some(sc_score) = grid } else {
.iter() (0, grid.len() - row - 1)
.enumerate() },
.skip(row + 1) ];
.map(|(rr, l)| (rr - row, l[col])) let is_visible = values.map(|e| e.0).iter().sum::<i32>() < 4;
.find(|(_sc_score, other_height)| other_height >= height) let scenic = values.map(|e| e.1).iter().product::<usize>();
{ (is_visible, scenic)
sides_visible -= 1;
sc_score.0
} else {
grid.len() - row - 1
};
(sides_visible > 0, score)
}) })
.collect::<Vec<_>>() .collect::<Vec<_>>()
}) })