-
Notifications
You must be signed in to change notification settings - Fork 0
/
quest03.rs
42 lines (35 loc) · 936 Bytes
/
quest03.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use crate::util::grid::*;
use crate::util::point::*;
use std::collections::VecDeque;
pub fn part1(notes: &str) -> u32 {
dig(notes, &ORTHOGONAL)
}
pub fn part2(notes: &str) -> u32 {
dig(notes, &ORTHOGONAL)
}
pub fn part3(notes: &str) -> u32 {
dig(notes, &DIAGONAL)
}
fn dig(notes: &str, neighbors: &[Point]) -> u32 {
let grid = Grid::parse(notes);
let mut depth = grid.same_size_with(0);
let mut todo = VecDeque::new();
for x in 0..grid.width {
for y in 0..grid.height {
let p = Point::new(x, y);
if grid[p] == b'#' {
todo.push_back(p);
}
}
}
while let Some(p) = todo.pop_front() {
if neighbors
.iter()
.all(|&n| depth[p] <= if depth.contains(p + n) { depth[p + n] } else { 0 })
{
depth[p] += 1;
todo.push_back(p);
}
}
depth.bytes.iter().sum()
}