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()
.skip(1)
.map(|(col, height)| {
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
.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)
let values = [
if let Some(sc_score) = (0..col)
.rev()
.map(|col_left| (col - col_left, row_vec[col_left]))
.find(|(_sc_score, other_height)| other_height >= height)
{
(1, sc_score.0)
} else {
(0, col)
},
if let Some(sc_score) = (col + 1..width)
.map(|col_right| (col_right - col, row_vec[col_right]))
.find(|(_sc_score, other_height)| other_height >= height)
{
(1, sc_score.0)
} else {
(0, width - col - 1)
},
if let Some(sc_score) = (0..row)
.rev()
.map(|row_above| (row - row_above, grid[row_above][col]))
.find(|(_sc_score, other_height)| other_height >= height)
{
(1, sc_score.0)
} else {
(0, row)
},
if let Some(sc_score) = grid
.iter()
.enumerate()
.skip(row + 1)
.map(|(row_below, l)| (row_below - row, l[col]))
.find(|(_sc_score, other_height)| other_height >= height)
{
(1, sc_score.0)
} else {
(0, grid.len() - row - 1)
},
];
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<_>>()
})