Files
aoc-2022/src/day08/mod.rs

84 lines
2.9 KiB
Rust

use crate::read;
pub fn run() {
let grid: Vec<Vec<u32>> = read("08")
.lines()
.map(|l| {
l.chars()
.map(|c| c.to_digit(10).unwrap())
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
let width = grid.first().unwrap().len();
let res = grid
.iter()
.enumerate()
.skip(1)
.flat_map(|(row, row_vec)| {
row_vec
.iter()
.enumerate()
.skip(1)
.map(|(col, height)| {
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<_>>()
})
.collect::<Vec<_>>();
#[cfg(feature = "part1")]
{
let count = res.iter().filter(|e| e.0).count();
println!("Day 8, Part 01: {}", count);
}
#[cfg(feature = "part2")]
{
let max_score = res.iter().map(|e| e.1).max().unwrap();
println!("Day 8, Part 02: {}", max_score);
}
}