-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10.rs
61 lines (53 loc) · 1.55 KB
/
10.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#![feature(test)]
use aoc::grid::Direction;
type Input = Vec<Vec<u8>>;
fn setup(input: &str) -> Input {
input
.trim()
.lines()
.map(|l| l.bytes().map(|b| b - b'0').collect())
.collect()
}
fn solve<const P2: bool>(input: &Input) -> usize {
let mut visited = vec![usize::MAX; if !P2 { input.len() * input[0].len() } else { 0 }];
input
.iter()
.enumerate()
.flat_map(|(i, row)| {
row.iter()
.enumerate()
.filter(|(_, &x)| x == 0)
.map(move |(j, _)| (i, j))
})
.map(|(si, sj)| {
let mut stack = vec![(sj, si)];
let mut out = 0;
while let Some((j, i)) = stack.pop() {
if !P2 {
let k = j * input[0].len() + i;
let v = sj * input[0].len() + si;
if visited[k] == v {
continue;
}
visited[k] = v;
}
if input[i][j] == 9 {
out += 1;
continue;
}
stack.extend(Direction::iter().flat_map(|d| {
d.step(j, i, input[0].len(), input.len())
.filter(|&(nj, ni)| input[ni][nj] == input[i][j] + 1)
}));
}
out
})
.sum()
}
fn part1(input: &Input) -> usize {
solve::<false>(input)
}
fn part2(input: &Input) -> usize {
solve::<true>(input)
}
aoc::main!(2024, 10, ex: 1);