generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
10.rs
146 lines (117 loc) · 3.49 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use std::collections::HashSet;
use advent_of_code::ascii_array_2d_with;
advent_of_code::solution!();
pub fn part_one(input: &str) -> Option<u32> {
let map = ascii_array_2d_with(input, |n| {
debug_assert!(n.is_ascii_digit());
n - b'0'
});
let trailheads = map
.iter_with_index()
.filter(|(_, &value)| value == 0)
.map(|(pos, _)| pos);
let mut trailheads_score = 0;
let mut goals = HashSet::new();
let mut to_explore = Vec::new();
for trailhead in trailheads {
goals.clear();
to_explore.clear();
to_explore.push((trailhead, 0));
while let Some(((r, c), value)) = to_explore.pop() {
if value == 9 {
goals.insert((r, c));
continue;
}
let target = value + 1;
// Up
if let Some(r) = r.checked_sub(1) {
if map.get(r, c).copied().unwrap() == target {
to_explore.push(((r, c), target));
}
}
// Down
{
let r = r + 1;
if map.get(r, c).copied().is_some_and(|x| x == target) {
to_explore.push(((r, c), target));
}
}
// Left
if let Some(c) = c.checked_sub(1) {
if map.get(r, c).copied().unwrap() == target {
to_explore.push(((r, c), target));
}
}
// Right
{
let c = c + 1;
if map.get(r, c).copied().is_some_and(|x| x == target) {
to_explore.push(((r, c), target));
}
}
}
trailheads_score += goals.len();
}
Some(trailheads_score.try_into().unwrap())
}
pub fn part_two(input: &str) -> Option<u32> {
let map = ascii_array_2d_with(input, |n| {
debug_assert!(n.is_ascii_digit());
n - b'0'
});
let mut to_explore = Vec::new();
for (pos, &value) in map.iter_with_index() {
if value == 0 {
to_explore.push((pos, 0));
}
}
let mut trailheads_score = 0;
while let Some(((r, c), value)) = to_explore.pop() {
if value == 9 {
trailheads_score += 1;
continue;
}
let target = value + 1;
// Up
if let Some(r) = r.checked_sub(1) {
if map.get(r, c).copied().unwrap() == target {
to_explore.push(((r, c), target));
}
}
// Down
{
let r = r + 1;
if map.get(r, c).copied().is_some_and(|x| x == target) {
to_explore.push(((r, c), target));
}
}
// Left
if let Some(c) = c.checked_sub(1) {
if map.get(r, c).copied().unwrap() == target {
to_explore.push(((r, c), target));
}
}
// Right
{
let c = c + 1;
if map.get(r, c).copied().is_some_and(|x| x == target) {
to_explore.push(((r, c), target));
}
}
}
Some(trailheads_score)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(36));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(81));
}
}