generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10.rs
176 lines (150 loc) · 4.39 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use std::{collections::HashSet, ops::Add};
use itertools::Itertools;
advent_of_code::solution!(10);
#[derive(Debug, Eq, Hash, PartialEq, Clone, Copy)]
struct Position(usize, usize);
pub fn part_one(input: &str) -> Option<usize> {
let mut trailheads = vec![];
let map = input
.lines()
.enumerate()
.map(|(ydx, line)| {
line.chars()
.enumerate()
.map(|(xdx, ch)| {
let num = ch.to_digit(10).unwrap_or(100);
if num == 0 {
trailheads.push((xdx, ydx));
}
num
})
.collect_vec()
})
.collect_vec();
let mut res = 0;
for th in trailheads {
let tmp = dfs_step(&map, Position(th.0, th.1), 0);
res += tmp.len();
}
Some(res)
}
fn dfs_step(map: &[Vec<u32>], current_pos: Position, current_val: u32) -> HashSet<Position> {
if current_val == 9 {
let mut set = HashSet::new();
set.insert(current_pos);
return set;
}
let mut result = HashSet::new();
// Up
if current_pos.1 > 0 && map[current_pos.1 - 1][current_pos.0] == current_val + 1 {
result.extend(dfs_step(
map,
Position(current_pos.0, current_pos.1 - 1),
current_val + 1,
));
};
// Righht
if current_pos.0 < map[0].len() - 1 && map[current_pos.1][current_pos.0 + 1] == current_val + 1
{
result.extend(dfs_step(
map,
Position(current_pos.0 + 1, current_pos.1),
current_val + 1,
));
};
// Down
if current_pos.1 < map.len() - 1 && map[current_pos.1 + 1][current_pos.0] == current_val + 1 {
result.extend(dfs_step(
map,
Position(current_pos.0, current_pos.1 + 1),
current_val + 1,
));
};
// Left
if current_pos.0 > 0 && map[current_pos.1][current_pos.0 - 1] == current_val + 1 {
result.extend(dfs_step(
map,
Position(current_pos.0 - 1, current_pos.1),
current_val + 1,
));
};
result
}
pub fn part_two(input: &str) -> Option<u32> {
let mut trailheads = vec![];
let map = input
.lines()
.enumerate()
.map(|(ydx, line)| {
line.chars()
.enumerate()
.map(|(xdx, ch)| {
let num = ch.to_digit(10).unwrap_or(100);
if num == 0 {
trailheads.push((xdx, ydx));
}
num
})
.collect_vec()
})
.collect_vec();
let mut res = 0;
for th in trailheads {
res += dfs_step_2(&map, Position(th.0, th.1), 0);
}
Some(res)
}
fn dfs_step_2(map: &[Vec<u32>], current_pos: Position, current_val: u32) -> u32 {
if current_val == 9 {
return 1;
}
let mut result: u32 = 0;
// Up
if current_pos.1 > 0 && map[current_pos.1 - 1][current_pos.0] == current_val + 1 {
result = result.add(dfs_step_2(
map,
Position(current_pos.0, current_pos.1 - 1),
current_val + 1,
));
};
// Righht
if current_pos.0 < map[0].len() - 1 && map[current_pos.1][current_pos.0 + 1] == current_val + 1
{
result = result.add(dfs_step_2(
map,
Position(current_pos.0 + 1, current_pos.1),
current_val + 1,
));
};
// Down
if current_pos.1 < map.len() - 1 && map[current_pos.1 + 1][current_pos.0] == current_val + 1 {
result = result.add(dfs_step_2(
map,
Position(current_pos.0, current_pos.1 + 1),
current_val + 1,
));
};
// Left
if current_pos.0 > 0 && map[current_pos.1][current_pos.0 - 1] == current_val + 1 {
result = result.add(dfs_step_2(
map,
Position(current_pos.0 - 1, current_pos.1),
current_val + 1,
));
};
result
}
#[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));
}
}