generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17.rs
325 lines (275 loc) · 8.85 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
use std::{
cmp::Ordering,
collections::{BinaryHeap, HashMap, HashSet},
};
advent_of_code::solution!(17);
pub fn part_one(input: &str) -> Option<usize> {
let graph = Graph::from_input(input, 1, 3);
graph.smallest_path_cost_across()
}
pub fn part_two(input: &str) -> Option<usize> {
let graph = Graph::from_input(input, 4, 10);
graph.smallest_path_cost_across()
}
struct Graph {
grid: Vec<Vec<usize>>,
same_direction_min: usize,
same_direction_max: usize,
}
impl Graph {
fn from_input(input: &str, same_direction_min: usize, same_direction_max: usize) -> Graph {
let grid = input
.lines()
.map(|line| {
line.chars()
.map(|char| char.to_digit(10).unwrap() as usize)
.collect()
})
.collect();
Graph {
grid,
same_direction_min,
same_direction_max,
}
}
fn smallest_path_cost_across(&self) -> Option<usize> {
let start = Coordinate { x: 0, y: 0 };
let end = Coordinate {
x: self.grid[self.grid.len() - 1].len() - 1,
y: self.grid.len() - 1,
};
self.smallest_path_cost(start, end)
}
/**
* Uses A Start search algorithm: https://en.wikipedia.org/wiki/A*_search_algorithm
*/
fn smallest_path_cost(&self, start: Coordinate, end: Coordinate) -> Option<usize> {
let mut min_costs = HashMap::new();
let mut nodes_to_visit = BinaryHeap::new();
let mut position_origins = HashMap::new();
let start_position = Position {
coordinate: start.clone(),
enter_direction: Direction::None,
same_direction_count: 1,
};
min_costs.insert(start_position.clone(), 0);
nodes_to_visit.push(Node {
cost: 0,
estimated_cost: self.estimate_cost_between(&start, &end),
position: start_position,
});
while let Some(node) = nodes_to_visit.pop() {
// We might have nodes with duplicate positions in the heap, so we need to check whether this node has non-minimal cost
if node.cost > *min_costs.get(&node.position).unwrap_or(&usize::MAX) {
continue;
}
if node.position.coordinate == end {
self.print_path(&node, &position_origins);
return Some(node.cost);
}
for neighbor in self.neighbors(&node, &end) {
if neighbor.cost < *min_costs.get(&neighbor.position).unwrap_or(&usize::MAX) {
position_origins.insert(neighbor.position.clone(), node.position.clone());
min_costs.insert(neighbor.position.clone(), neighbor.cost);
nodes_to_visit.push(neighbor);
}
}
}
None
}
fn estimate_cost_between(&self, start: &Coordinate, end: &Coordinate) -> usize {
let x_diff = if start.x > end.x {
start.x - end.x
} else {
end.x - start.x
};
let y_diff = if start.y > end.y {
start.y - end.y
} else {
end.y - start.y
};
x_diff + y_diff
}
fn neighbors<'a>(
&'a self,
node: &'a Node,
end: &'a Coordinate,
) -> impl Iterator<Item = Node> + '_ {
[
Direction::Up,
Direction::Down,
Direction::Left,
Direction::Right,
]
.into_iter()
.filter_map(move |direction| {
if let Some((position, cost_to_position)) =
self.get_next_position(&node.position, direction)
{
let cost = node.cost + cost_to_position;
Some(Node {
cost,
estimated_cost: cost + self.estimate_cost_between(&position.coordinate, end),
position,
})
} else {
None
}
})
}
fn get_next_position(
&self,
position: &Position,
direction: Direction,
) -> Option<(Position, usize)> {
let is_same_direction = direction == position.enter_direction;
let is_overflowing_direction_max =
is_same_direction && position.same_direction_count == self.same_direction_max;
if is_overflowing_direction_max {
return None;
}
let is_backwards_direction = direction == position.enter_direction.reverse();
if is_backwards_direction {
return None;
}
let next_position_distance = if is_same_direction {
1
} else {
self.same_direction_min
};
let is_next_position_outside_grid = match direction {
Direction::Up => position.coordinate.y < next_position_distance,
Direction::Down => position.coordinate.y + next_position_distance > self.grid.len() - 1,
Direction::Left => position.coordinate.x < next_position_distance,
Direction::Right => {
position.coordinate.x + next_position_distance
> self.grid[position.coordinate.y].len() - 1
}
Direction::None => false,
};
if is_next_position_outside_grid {
return None;
}
let cost_to_next_position: usize = (1..=next_position_distance)
.map(|distance| self.cost(&position.coordinate.next(direction, distance)))
.sum();
Some((
position.next(direction, next_position_distance),
cost_to_next_position,
))
}
fn cost(&self, coordinate: &Coordinate) -> usize {
self.grid[coordinate.y][coordinate.x]
}
fn print_path(&self, node: &Node, position_origins: &HashMap<Position, Position>) {
let mut path_coordinates = HashSet::new();
let mut current = &node.position;
while let Some(previous) = position_origins.get(current) {
path_coordinates.insert(¤t.coordinate);
current = previous;
}
println!("Path taken:");
for y in 0..self.grid.len() {
for x in 0..self.grid[0].len() {
if path_coordinates.contains(&Coordinate { x, y }) {
print!("\x1b[32m{}\x1b[0m", self.grid[y][x]);
} else {
print!("{}", self.grid[y][x]);
}
}
println!();
}
}
}
#[derive(Hash, Eq, PartialEq, Clone, Debug)]
struct Coordinate {
x: usize,
y: usize,
}
impl Coordinate {
fn next(&self, direction: Direction, distance: usize) -> Coordinate {
Coordinate {
x: match direction {
Direction::Left => self.x - distance,
Direction::Right => self.x + distance,
_ => self.x,
},
y: match direction {
Direction::Up => self.y - distance,
Direction::Down => self.y + distance,
_ => self.y,
},
}
}
}
#[derive(Hash, Eq, PartialEq, Clone, Debug)]
struct Position {
coordinate: Coordinate,
enter_direction: Direction,
same_direction_count: usize,
}
impl Position {
fn next(&self, direction: Direction, distance: usize) -> Position {
Position {
coordinate: self.coordinate.next(direction, distance),
enter_direction: direction,
same_direction_count: if direction == self.enter_direction {
self.same_direction_count + distance
} else {
distance
},
}
}
}
#[derive(Eq, PartialEq, Debug)]
struct Node {
cost: usize,
estimated_cost: usize,
position: Position,
}
impl Ord for Node {
fn cmp(&self, other: &Self) -> Ordering {
other
.estimated_cost
.cmp(&self.estimated_cost)
.then_with(|| other.cost.cmp(&self.cost))
}
}
impl PartialOrd for Node {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
#[derive(Hash, Eq, PartialEq, Copy, Clone, Debug)]
enum Direction {
Up,
Down,
Left,
Right,
None,
}
impl Direction {
fn reverse(&self) -> Direction {
match self {
Direction::Up => Direction::Down,
Direction::Down => Direction::Up,
Direction::Left => Direction::Right,
Direction::Right => Direction::Left,
Direction::None => Direction::None,
}
}
}
#[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));
}
}