generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17.rs
264 lines (216 loc) · 6.45 KB
/
17.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
advent_of_code::solution!(17);
use advent_of_code::maneatingape::grid::*;
use advent_of_code::maneatingape::hash::*;
use advent_of_code::maneatingape::heap::*;
use advent_of_code::maneatingape::point::*;
type Direction = u8;
#[derive(PartialEq, Eq, Hash, Clone, Copy)]
struct Element {
location: Point,
direction: Direction,
direction_count: u8,
}
fn parse_data(input: &str) -> Grid<u8> {
let mut grid = Grid::parse(input);
for elem in grid.bytes.iter_mut() {
*elem -= b'0';
}
grid
}
fn get_neighbors_part_1(grid: &Grid<u8>, element: Element) -> Vec<Element> {
let Element {
location,
direction,
direction_count,
} = element;
let mut result = Vec::with_capacity(4);
if location.x > 0 && direction != b'R' && !(direction_count >= 3 && direction == b'L') {
let direction_count = match direction {
b'L' => direction_count + 1,
_ => 1,
};
result.push(Element {
location: location + LEFT,
direction: b'L',
direction_count,
});
}
if location.x < grid.width - 1
&& direction != b'L'
&& !(direction_count >= 3 && direction == b'R')
{
let direction_count = match direction {
b'R' => direction_count + 1,
_ => 1,
};
result.push(Element {
location: location + RIGHT,
direction: b'R',
direction_count,
});
}
if location.y > 0 && direction != b'D' && !(direction_count >= 3 && direction == b'U') {
let direction_count = match direction {
b'U' => direction_count + 1,
_ => 1,
};
result.push(Element {
location: location + UP,
direction: b'U',
direction_count,
});
}
if location.y < grid.height - 1
&& direction != b'U'
&& !(direction_count >= 3 && direction == b'D')
{
let direction_count = match direction {
b'D' => direction_count + 1,
_ => 1,
};
result.push(Element {
location: location + DOWN,
direction: b'D',
direction_count,
});
}
result
}
fn get_neighbors_part_2(grid: &Grid<u8>, element: Element) -> Vec<Element> {
let Element {
location,
direction,
direction_count,
} = element;
let mut result = Vec::with_capacity(4);
if location.x > 0
&& direction != b'R'
&& !(direction_count < 4 && direction != b'L')
&& !(direction_count >= 10 && direction == b'L')
{
let direction_count = match direction {
b'L' => direction_count + 1,
_ => 1,
};
result.push(Element {
location: location + LEFT,
direction: b'L',
direction_count,
});
}
if location.x < grid.width - 1
&& direction != b'L'
&& !(direction_count < 4 && direction != b'R')
&& !(direction_count >= 10 && direction == b'R')
{
let direction_count = match direction {
b'R' => direction_count + 1,
_ => 1,
};
result.push(Element {
location: location + RIGHT,
direction: b'R',
direction_count,
});
}
if location.y > 0
&& direction != b'D'
&& !(direction_count < 4 && direction != b'U')
&& !(direction_count >= 10 && direction == b'U')
{
let direction_count = match direction {
b'U' => direction_count + 1,
_ => 1,
};
result.push(Element {
location: location + UP,
direction: b'U',
direction_count,
});
}
if location.y < grid.height - 1
&& direction != b'U'
&& !(direction_count < 4 && direction != b'D')
&& !(direction_count >= 10 && direction == b'D')
{
let direction_count = match direction {
b'D' => direction_count + 1,
_ => 1,
};
result.push(Element {
location: location + DOWN,
direction: b'D',
direction_count,
});
}
result
}
fn find_path_cost<F>(grid: &Grid<u8>, get_neighbors: F) -> u32
where
F: Fn(&Grid<u8>, Element) -> Vec<Element>,
{
let root_element_right = Element {
location: Point::new(0, 0),
direction: b'R',
direction_count: 0,
};
let root_element_down = Element {
location: Point::new(0, 0),
direction: b'D',
direction_count: 0,
};
fn is_goal(grid: &Grid<u8>, element: &Element) -> bool {
element.location.x == grid.width - 1 && element.location.y == grid.height - 1
}
let mut visited = FastSet::new();
let mut dist = FastMap::new();
dist.insert(root_element_right, 0);
dist.insert(root_element_down, 0);
let mut queue = MinHeap::new();
queue.push(0, root_element_right);
queue.push(0, root_element_down);
while let Some(queue_element) = queue.pop() {
let element = queue_element.1;
if is_goal(grid, &element) {
return queue_element.0;
}
if visited.contains(&element) {
continue;
}
visited.insert(element);
for new_element in get_neighbors(grid, element) {
let dist_element = dist.get(&element).unwrap();
let dist_new_element = dist.get(&new_element).copied().unwrap_or(u32::MAX);
let dist_to_new_element = dist_element + grid[new_element.location] as u32;
if dist_to_new_element < dist_new_element {
dist.insert(new_element, dist_to_new_element);
queue.push(dist_to_new_element, new_element);
}
}
}
unreachable!()
}
pub fn part_one(input: &str) -> Option<u32> {
let grid = parse_data(input);
let result = find_path_cost(&grid, get_neighbors_part_1);
Some(result)
}
pub fn part_two(input: &str) -> Option<u32> {
let grid = parse_data(input);
let result = find_path_cost(&grid, get_neighbors_part_2);
Some(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(102));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(94));
}
}