-
Notifications
You must be signed in to change notification settings - Fork 0
/
day17.rs
304 lines (269 loc) · 8.16 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
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
use aoc_runner_derive::{aoc, aoc_generator};
use itertools::Itertools;
use pathfinding::directed::dijkstra::dijkstra;
#[derive(Debug, PartialEq, Eq)]
pub struct Grid {
grid: Vec<Vec<u8>>,
}
impl Grid {
fn get_cost(&self, (y, x): (usize, usize)) -> Option<usize> {
self.grid.get(y)?.get(x).copied().map(From::from)
}
#[allow(unused)]
fn debug_grid(&self, ms: &[Movement]) {
for (r, row) in self.grid.iter().enumerate() {
for (c, cell) in row.iter().enumerate() {
if let Some(m) = ms.iter().find(|m| m.pos == (r, c)) {
let sym = match m.dir {
Direction::North => '^',
Direction::West => '<',
Direction::South => 'v',
Direction::East => '>',
Direction::None => '?',
};
print!("{sym}");
} else {
print!("{cell}",);
}
}
println!();
}
}
}
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
enum Direction {
North,
West,
South,
East,
None,
}
impl Direction {
fn ninety_turn(&self) -> [Direction; 2] {
match self {
Direction::North | Direction::South => [Direction::East, Direction::West],
Direction::West | Direction::East => [Direction::North, Direction::South],
Direction::None => panic!("we should not call ninety_turn on a None"),
}
}
fn next_pos(&self, (y, x): (usize, usize)) -> (usize, usize) {
match self {
Direction::North => (y.wrapping_sub(1), x),
Direction::West => (y, x.wrapping_sub(1)),
Direction::South => (y + 1, x),
Direction::East => (y, x + 1),
Direction::None => panic!("we should not call get_cost on a None"),
}
}
fn reverse(&self) -> Self {
match self {
Direction::North => Direction::South,
Direction::West => Direction::East,
Direction::South => Direction::North,
Direction::East => Direction::West,
Direction::None => Direction::None,
}
}
}
#[aoc_generator(day17)]
pub fn generator(input: &str) -> Grid {
let grid = input
.lines()
.map(|line| line.bytes().map(|b| b - b'0').collect())
.collect();
Grid { grid }
}
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
pub struct Movement {
dir: Direction,
repeat: usize,
pos: (usize, usize),
}
#[aoc(day17, part1)]
pub fn part1(inputs: &Grid) -> usize {
let height = inputs.grid.len();
let width = inputs.grid[0].len();
let ans = dijkstra(
&Movement {
dir: Direction::None,
repeat: 0,
pos: (0, 0),
},
|m| {
if m.repeat == 3 {
m.dir
.ninety_turn()
.into_iter()
.filter_map(|d| {
let new_pos = d.next_pos(m.pos);
let cost = inputs.get_cost(new_pos)?;
Some((
Movement {
dir: d,
repeat: 1,
pos: new_pos,
},
cost,
))
})
.collect_vec()
} else {
[
Direction::North,
Direction::West,
Direction::South,
Direction::East,
]
.into_iter()
.filter_map(|d| {
if m.dir.reverse() == d {
return None;
}
let repeat = if m.dir == d { m.repeat + 1 } else { 1 };
let new_pos = d.next_pos(m.pos);
let cost = inputs.get_cost(new_pos)?;
Some((
Movement {
dir: d,
repeat,
pos: new_pos,
},
cost,
))
})
.collect_vec()
}
},
|m| m.pos == (height - 1, width - 1),
)
.unwrap();
// inputs.debug_grid(&ans.0);
// println!("{:?}", ans.0);
ans.1
}
#[aoc(day17, part2)]
pub fn part2(inputs: &Grid) -> usize {
let height = inputs.grid.len();
let width = inputs.grid[0].len();
let ans = dijkstra(
&Movement {
dir: Direction::None,
repeat: 0,
pos: (0, 0),
},
|m| {
if m.repeat < 4 && m.dir != Direction::None {
[m.dir]
.into_iter()
.filter_map(|d| {
let new_pos = d.next_pos(m.pos);
let cost = inputs.get_cost(new_pos)?;
Some((
Movement {
dir: d,
repeat: m.repeat + 1,
pos: new_pos,
},
cost,
))
})
.collect_vec()
} else if m.repeat == 10 && m.dir != Direction::None {
m.dir
.ninety_turn()
.into_iter()
.filter_map(|d| {
let new_pos = d.next_pos(m.pos);
let cost = inputs.get_cost(new_pos)?;
Some((
Movement {
dir: d,
repeat: 1,
pos: new_pos,
},
cost,
))
})
.collect_vec()
} else {
[
Direction::North,
Direction::West,
Direction::South,
Direction::East,
]
.into_iter()
.filter_map(|d| {
if m.dir.reverse() == d {
return None;
}
let repeat = if m.dir == d { m.repeat + 1 } else { 1 };
let new_pos = d.next_pos(m.pos);
let cost = inputs.get_cost(new_pos)?;
Some((
Movement {
dir: d,
repeat,
pos: new_pos,
},
cost,
))
})
.collect_vec()
}
},
|m| m.repeat > 3 && m.pos == (height - 1, width - 1),
)
.unwrap();
// inputs.debug_grid(&ans.0);
// println!("{:?}", ans.0);
ans.1
}
#[cfg(test)]
mod tests {
use super::*;
const SAMPLE: &str = r"2413432311323
3215453535623
3255245654254
3446585845452
4546657867536
1438598798454
4457876987766
3637877979653
4654967986887
4564679986453
1224686865563
2546548887735
4322674655533";
#[test]
pub fn input_test() {
println!("{:?}", generator(SAMPLE));
// assert_eq!(generator(SAMPLE), Object());
}
#[test]
pub fn part1_test() {
assert_eq!(part1(&generator(SAMPLE)), 102);
}
#[test]
pub fn part2_test() {
const SAMPLE2: &str = "111111111111
999999999991
999999999991
999999999991
999999999991";
assert_eq!(part2(&generator(SAMPLE)), 94);
assert_eq!(part2(&generator(SAMPLE2)), 71);
}
mod regression {
use super::*;
const INPUT: &str = include_str!("../input/2023/day17.txt");
const ANSWERS: (usize, usize) = (791, 900);
#[test]
pub fn test() {
let input = INPUT.trim_end_matches('\n');
let output = generator(input);
assert_eq!(part1(&output), ANSWERS.0);
assert_eq!(part2(&output), ANSWERS.1);
}
}
}