-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday17.rs
205 lines (187 loc) · 6.13 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use aoc_runner_derive::aoc;
fn parse(input: &str) -> utils::Map {
let mut matrix = pathfinding::matrix::Matrix::new(
input.lines().count(),
input.lines().next().unwrap().len(),
0,
);
input.lines().enumerate().for_each(|(y, line)| {
line.chars().enumerate().for_each(|(x, c)| {
matrix[(x, y)] = c.to_digit(10).unwrap();
});
});
utils::Map::new(matrix)
}
#[aoc(day17, part1)]
#[must_use]
pub fn part1(input: &str) -> u32 {
let input = parse(input);
input.find_shortest_path(0, 3)
}
#[aoc(day17, part2)]
#[must_use]
pub fn part2(input: &str) -> u32 {
let input = parse(input);
input.find_shortest_path(4, 10)
}
mod utils {
#[derive(derive_more::Deref)]
pub struct Map(pathfinding::matrix::Matrix<u32>);
impl Map {
pub fn new(value: pathfinding::matrix::Matrix<u32>) -> Self {
Self(value)
}
pub fn find_shortest_path(
&self,
min_consecutive_moves: usize,
max_consecutive_moves: usize,
) -> u32 {
let start_nodes = [
Node::new((0, 0), Direction::South, 0),
Node::new((0, 0), Direction::East, 0),
];
start_nodes
.iter()
.map(|start| {
let (_, distance) = pathfinding::prelude::dijkstra(
start,
|node| node.successors(self, min_consecutive_moves, max_consecutive_moves),
|node| node.success(self, min_consecutive_moves),
)
.unwrap();
distance
})
.min()
.unwrap()
}
fn propagate(
&self,
position: (usize, usize),
direction: Direction,
) -> Option<(usize, usize)> {
match direction {
Direction::North => position.1.checked_sub(1).map(|next_y| (position.0, next_y)),
Direction::West => position.0.checked_sub(1).map(|next_x| (next_x, position.1)),
Direction::South => {
let next_y = position.1 + 1;
(next_y < self.rows).then_some((position.0, next_y))
}
Direction::East => {
let next_x = position.0 + 1;
(next_x < self.columns).then_some((next_x, position.1))
}
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
enum Direction {
North,
West,
South,
East,
}
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
struct Node {
position: (usize, usize),
direction: Direction,
n_consecutive_moves: usize,
}
impl Node {
fn new(position: (usize, usize), direction: Direction, n_consecutive_moves: usize) -> Self {
Self {
position,
direction,
n_consecutive_moves,
}
}
fn available_directions(
&self,
min_consecutive_moves: usize,
max_consecutive_moves: usize,
) -> Vec<Direction> {
if self.n_consecutive_moves < min_consecutive_moves {
vec![self.direction]
} else if self.n_consecutive_moves == max_consecutive_moves {
match self.direction {
Direction::North | Direction::South => {
vec![Direction::West, Direction::East]
}
Direction::West | Direction::East => {
vec![Direction::North, Direction::South]
}
}
} else {
match self.direction {
Direction::North => {
vec![Direction::North, Direction::West, Direction::East]
}
Direction::West => {
vec![Direction::West, Direction::North, Direction::South]
}
Direction::South => {
vec![Direction::South, Direction::West, Direction::East]
}
Direction::East => {
vec![Direction::East, Direction::North, Direction::South]
}
}
}
}
fn successors(
&self,
map: &Map,
min_consecutive_moves: usize,
max_consecutive_moves: usize,
) -> Vec<(Node, u32)> {
self.available_directions(min_consecutive_moves, max_consecutive_moves)
.iter()
.filter_map(|&next_direction| {
map.propagate(self.position, next_direction)
.map(|next_position| {
let next_n_consecutive_moves = if next_direction == self.direction {
self.n_consecutive_moves + 1
} else {
1
};
(
Node::new(next_position, next_direction, next_n_consecutive_moves),
map[next_position],
)
})
})
.collect()
}
fn success(&self, map: &Map, min_consecutive_moves: usize) -> bool {
(self.position.0 + 1 == map.columns && self.position.1 + 1 == map.rows)
&& self.n_consecutive_moves >= min_consecutive_moves
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use indoc::indoc;
const SAMPLE: &str = indoc! {"
2413432311323
3215453535623
3255245654254
3446585845452
4546657867536
1438598798454
4457876987766
3637877979653
4654967986887
4564679986453
1224686865563
2546548887735
4322674655533
"};
#[test]
pub fn part1_example() {
assert_eq!(part1(SAMPLE), 102);
}
#[test]
pub fn part2_example() {
assert_eq!(part2(SAMPLE), 94);
}
}