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