-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday17.rs
183 lines (147 loc) · 4.7 KB
/
day17.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
177
178
179
180
181
182
183
//! [Day 17: Two Steps Forward](https://adventofcode.com/2016/day/17)
use std::collections::VecDeque;
struct Puzzle<'a> {
password: &'a str,
}
impl<'a> Puzzle<'a> {
const fn new(data: &'a str) -> Self {
Self {
password: data.trim_ascii(),
}
}
/// Solve part one.
fn part1(&self) -> String {
let mut base_digest = md5::Context::new();
base_digest.consume(self.password);
let mut q = VecDeque::new();
q.push_back((0, 0, 0, String::new()));
while let Some((x, y, steps, path)) = q.pop_front() {
if (x, y) == (3, 3) {
return path;
}
let mut digest = base_digest.clone();
digest.consume(&path);
let hash = digest.compute();
let hash: Vec<_> = format!("{hash:x}").chars().collect();
let is_open = |i| 'b' <= hash[i] && hash[i] <= 'f';
// north/up 1st digit of the hash
if y > 0 && is_open(0) {
let np = path.clone() + "U";
q.push_back((x, y - 1, steps + 1, np));
}
// south/down, 2nd digit of the hash
if y < 3 && is_open(1) {
let np = path.clone() + "D";
q.push_back((x, y + 1, steps + 1, np));
}
// west/left, 3rd digit of the hash
if x > 0 && is_open(2) {
let np = path.clone() + "L";
q.push_back((x - 1, y, steps + 1, np));
}
// east/right, 4th digit of the hash
if x < 3 && is_open(3) {
let np = path.clone() + "R";
q.push_back((x + 1, y, steps + 1, np));
}
}
String::new()
}
/// Solve part two.
fn part2(&self) -> u32 {
let mut max_steps = 0;
let mut base_digest = md5::Context::new();
base_digest.consume(self.password);
let mut q = VecDeque::new();
q.push_back((0, 0, 0, base_digest.clone()));
while let Some((x, y, steps, digest)) = q.pop_front() {
if (x, y) == (3, 3) {
if max_steps < steps {
max_steps = steps;
}
continue;
}
let hash = digest.clone().compute();
let hash: Vec<_> = format!("{hash:x}").chars().collect();
let is_open = |i| 'b' <= hash[i] && hash[i] <= 'f';
// north/up 1st digit of the hash
if y > 0 && is_open(0) {
let mut np = md5::Context::new();
digest.clone_into(&mut np);
np.consume("U");
q.push_back((x, y - 1, steps + 1, np));
}
// south/down, 2nd digit of the hash
if y < 3 && is_open(1) {
let mut np = md5::Context::new();
digest.clone_into(&mut np);
np.consume("D");
q.push_back((x, y + 1, steps + 1, np));
}
// west/left, 3rd digit of the hash
if x > 0 && is_open(2) {
let mut np = md5::Context::new();
digest.clone_into(&mut np);
np.consume("L");
q.push_back((x - 1, y, steps + 1, np));
}
// east/right, 4th digit of the hash
if x < 3 && is_open(3) {
let mut np = md5::Context::new();
digest.clone_into(&mut np);
np.consume("R");
q.push_back((x + 1, y, steps + 1, np));
}
}
max_steps
}
}
/// # Panics
#[must_use]
pub fn solve(data: &str) -> (String, u32) {
let puzzle = Puzzle::new(data);
(puzzle.part1(), puzzle.part2())
}
pub fn main() {
let args = aoc::parse_args();
args.run(solve);
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test00() {
let puzzle = Puzzle::new("hijkl");
assert_eq!(puzzle.part1(), ""); // no path
}
#[test]
fn test01() {
let puzzle = Puzzle::new("ihgpwlah");
assert_eq!(puzzle.part1(), "DDRRRD");
}
#[test]
fn test02() {
let puzzle = Puzzle::new("kglvqrro");
assert_eq!(puzzle.part1(), "DDUDRLRRUDRD");
}
#[test]
fn test03() {
let puzzle = Puzzle::new("ulqzkmiv");
assert_eq!(puzzle.part1(), "DRURDRUDDLLDLUURRDULRLDUUDDDRR");
}
#[test]
fn test04() {
let puzzle = Puzzle::new("ihgpwlah");
assert_eq!(puzzle.part2(), 370);
}
#[test]
fn test05() {
let puzzle = Puzzle::new("kglvqrro");
assert_eq!(puzzle.part2(), 492);
}
#[test]
fn test06() {
let puzzle = Puzzle::new("ulqzkmiv");
assert_eq!(puzzle.part2(), 830);
}
}