Cleanup
This commit is contained in:
parent
bccf31bf85
commit
de08913fba
1 changed files with 31 additions and 57 deletions
|
|
@ -5,7 +5,7 @@ const INPUT: &str = include_str!("../../input/day08.txt");
|
||||||
fn main() {
|
fn main() {
|
||||||
let grid = Grid::new(INPUT);
|
let grid = Grid::new(INPUT);
|
||||||
|
|
||||||
let visibility = grid.count_visible();
|
let visibility = grid.find_visible();
|
||||||
let part1 = visibility.iter().filter(|(_, v)| !v.is_empty()).count();
|
let part1 = visibility.iter().filter(|(_, v)| !v.is_empty()).count();
|
||||||
println!("Part 1: {}", part1);
|
println!("Part 1: {}", part1);
|
||||||
|
|
||||||
|
|
@ -54,70 +54,42 @@ impl Grid {
|
||||||
Self { map, rows, cols }
|
Self { map, rows, cols }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn count_visible(&self) -> HashMap<RC, HashSet<Visibility>> {
|
fn find_visible(&self) -> HashMap<RC, HashSet<Visibility>> {
|
||||||
// Assume all trees are visible from all sides until we learn that they're blocked.
|
// Assume all trees are visible from all sides until we learn that they're blocked.
|
||||||
let mut visibility: HashMap<RC, HashSet<Visibility>> = HashMap::new();
|
let mut visibility: HashMap<RC, HashSet<Visibility>> = HashMap::new();
|
||||||
|
|
||||||
for r in 0..self.rows {
|
for (r, c) in itertools::iproduct!(0..self.rows, 0..self.cols) {
|
||||||
for c in 0..self.cols {
|
|
||||||
visibility.insert((r, c), Visibility::all());
|
visibility.insert((r, c), Visibility::all());
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
// Now see what each tree would block.
|
||||||
for (r1, c1) in itertools::iproduct!(0..self.rows, 0..self.cols) {
|
for (r1, c1) in itertools::iproduct!(0..self.rows, 0..self.cols) {
|
||||||
let big = (r1, c1);
|
let height = self.map[&(r1, c1)];
|
||||||
let h1 = self.map[&big];
|
|
||||||
|
|
||||||
for (r2, c2) in (0..r1).map(|r| (r, c1)) {
|
use Visibility::*;
|
||||||
let lil = (r2, c2);
|
let block: Vec<(Visibility, Vec<RC>)> = vec![
|
||||||
let h2 = self.map[&lil];
|
(Top, (0..r1).map(|r| (r, c1)).collect()),
|
||||||
|
(Bottom, ((r1 + 1)..self.rows).map(|r| (r, c1)).collect()),
|
||||||
|
(Right, (0..c1).map(|c| (r1, c)).collect()),
|
||||||
|
(Left, ((c1 + 1)..self.cols).map(|c| (r1, c)).collect()),
|
||||||
|
];
|
||||||
|
|
||||||
if h1 >= h2 {
|
for (vis, ray) in block {
|
||||||
visibility.entry(lil).and_modify(|v| {
|
for rc in ray {
|
||||||
v.remove(&Visibility::Bottom);
|
if height >= self.map[&rc] {
|
||||||
|
visibility.entry(rc).and_modify(|v| {
|
||||||
|
v.remove(&vis);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (r2, c2) in ((r1 + 1)..self.rows).map(|r| (r, c1)) {
|
|
||||||
let lil = (r2, c2);
|
|
||||||
let h2 = self.map[&lil];
|
|
||||||
|
|
||||||
if h1 >= h2 {
|
|
||||||
visibility.entry(lil).and_modify(|v| {
|
|
||||||
v.remove(&Visibility::Top);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (r2, c2) in (0..c1).map(|c| (r1, c)) {
|
|
||||||
let lil = (r2, c2);
|
|
||||||
let h2 = self.map[&lil];
|
|
||||||
|
|
||||||
if h1 >= h2 {
|
|
||||||
visibility.entry(lil).and_modify(|v| {
|
|
||||||
v.remove(&Visibility::Right);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (r2, c2) in ((c1 + 1)..self.cols).map(|c| (r1, c)) {
|
|
||||||
let lil = (r2, c2);
|
|
||||||
let h2 = self.map[&lil];
|
|
||||||
|
|
||||||
if h1 >= h2 {
|
|
||||||
visibility.entry(lil).and_modify(|v| {
|
|
||||||
v.remove(&Visibility::Left);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
visibility
|
visibility
|
||||||
}
|
}
|
||||||
|
|
||||||
fn score_views(&self) -> HashMap<RC, u64> {
|
fn score_views(&self) -> HashMap<RC, usize> {
|
||||||
let mut scene: HashMap<RC, u64> = HashMap::new();
|
let mut scene: HashMap<RC, usize> = HashMap::new();
|
||||||
|
|
||||||
for (r1, c1) in itertools::iproduct!(0..self.rows, 0..self.cols) {
|
for (r1, c1) in itertools::iproduct!(0..self.rows, 0..self.cols) {
|
||||||
let big = (r1, c1);
|
let big = (r1, c1);
|
||||||
|
|
@ -125,19 +97,21 @@ impl Grid {
|
||||||
|
|
||||||
let mut score = 1;
|
let mut score = 1;
|
||||||
|
|
||||||
|
let blockage = |rc: &RC| -> bool { self.map[rc] >= h1 };
|
||||||
|
|
||||||
let up = (0..r1).rev().map(|r| (r, c1));
|
let up = (0..r1).rev().map(|r| (r, c1));
|
||||||
score *= take_until(up, |rc| self.map[rc] >= h1).len();
|
score *= take_until(up, blockage).len();
|
||||||
|
|
||||||
let down = ((r1 + 1)..self.rows).map(|r| (r, c1));
|
let down = ((r1 + 1)..self.rows).map(|r| (r, c1));
|
||||||
score *= take_until(down, |rc| self.map[rc] >= h1).len();
|
score *= take_until(down, blockage).len();
|
||||||
|
|
||||||
let left = (0..c1).rev().map(|c| (r1, c));
|
let left = (0..c1).rev().map(|c| (r1, c));
|
||||||
score *= take_until(left, |rc| self.map[rc] >= h1).len();
|
score *= take_until(left, blockage).len();
|
||||||
|
|
||||||
let right = ((c1 + 1)..self.cols).map(|c| (r1, c));
|
let right = ((c1 + 1)..self.cols).map(|c| (r1, c));
|
||||||
score *= take_until(right, |rc| self.map[rc] >= h1).len();
|
score *= take_until(right, blockage).len();
|
||||||
|
|
||||||
scene.insert(big, score as u64);
|
scene.insert(big, score);
|
||||||
}
|
}
|
||||||
|
|
||||||
scene
|
scene
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue