generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
17.rs
296 lines (252 loc) · 7.98 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
use bucket_queue::{BucketQueue, LastInFirstOutQueue};
use advent_of_code::util::coord::Direction;
use advent_of_code::util::shortest_path::{CostMap, OpenSet, Problem};
use advent_of_code::util::{shortest_path, Indexer, VecMap, VecSet, VecTable};
advent_of_code::solution!(17);
type CoordT = u16;
type Coord = advent_of_code::util::coord::Coord<CoordT>;
type CoordIndexer = advent_of_code::util::coord::CoordIndexer<CoordT>;
type CoordIndex = u16;
type Cost = u16;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Axis {
Horizontal,
Vertical,
}
impl Axis {
const fn orthogonal(&self) -> Axis {
match self {
Axis::Horizontal => Axis::Vertical,
Axis::Vertical => Axis::Horizontal,
}
}
const fn directions(&self) -> [Direction; 2] {
match self {
Axis::Horizontal => [Direction::Left, Direction::Right],
Axis::Vertical => [Direction::Up, Direction::Down],
}
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
struct State {
coord_index: CoordIndex,
axis: Axis,
}
#[derive(Clone, Copy)]
struct StateIndexer {
grid_len: usize,
}
impl StateIndexer {
fn new(grid_len: usize) -> Self {
Self { grid_len }
}
}
impl Indexer<State> for StateIndexer {
fn len(&self) -> usize {
self.grid_len * 2
}
fn index_for(&self, key: &State) -> usize {
let coord_index = key.coord_index as usize;
let axis = match key.axis {
Axis::Horizontal => 0,
Axis::Vertical => 1,
};
(coord_index << 1) + axis
}
}
fn parse_input(input: &str) -> VecTable<Coord, Cost, CoordIndexer> {
let mut width = None;
let data = input
.lines()
.flat_map(|line| {
if width.is_none() {
width = Some(line.len());
} else {
debug_assert_eq!(width, Some(line.len()));
}
line.chars().map(|c| ((c as u8) - b'0') as Cost)
})
.collect::<Vec<_>>();
let width = width.unwrap();
let height = data.len() / width;
let indexer = CoordIndexer::new(width as CoordT, height as CoordT);
VecTable::from_vec(data, indexer)
}
struct ClumsyCrucibleProblem {
grid: Box<[Cost]>,
grid_width: CoordT,
grid_height: CoordT,
min_steps: CoordT,
max_steps: CoordT,
}
impl ClumsyCrucibleProblem {
fn source_index(&self) -> CoordIndex {
(self.grid.len() - 1) as CoordIndex
}
fn target_index(&self) -> CoordIndex {
0
}
}
impl Problem for ClumsyCrucibleProblem {
type State = State;
type Cost = Cost;
fn sources(&self) -> impl IntoIterator<Item = Self::State> {
[Axis::Horizontal, Axis::Vertical].map(move |axis| {
let coord_index = self.source_index();
State { coord_index, axis }
})
}
fn is_target(&self, state: &Self::State) -> bool {
state.coord_index == self.target_index()
}
fn successors(
&self,
state: &Self::State,
) -> impl IntoIterator<Item = (Self::State, Self::Cost)> {
let coord_index = state.coord_index;
let axis = state.axis;
axis.directions()
.into_iter()
.filter_map(move |direction| {
let x = (coord_index % self.grid_width) as CoordT;
let y = (coord_index / self.grid_width) as CoordT;
let steps_to_edge = match direction {
Direction::Up => y,
Direction::Right => self.grid_width - x - 1,
Direction::Down => self.grid_height - y - 1,
Direction::Left => x,
};
if steps_to_edge < self.min_steps {
// Not enough space to move in this direction
return None;
}
Some((direction, steps_to_edge))
})
.flat_map(move |(direction, steps_to_edge)| {
let coord_step = match direction {
Direction::Up => (0 as CoordT).wrapping_sub(self.grid_width),
Direction::Right => 1,
Direction::Down => self.grid_width,
Direction::Left => (0 as CoordT).wrapping_sub(1),
};
let mut next_coord_index = coord_index;
let mut next_cost = 0;
let num_pre_steps = self.min_steps - 1;
let num_steps = self.max_steps.min(steps_to_edge) - num_pre_steps;
(0..num_pre_steps).for_each(|_| {
next_cost += self.grid[next_coord_index as usize];
next_coord_index = next_coord_index.wrapping_add(coord_step);
});
(0..num_steps).map(move |_| {
next_cost += self.grid[next_coord_index as usize];
next_coord_index = next_coord_index.wrapping_add(coord_step);
let next_state = State {
coord_index: next_coord_index,
axis: axis.orthogonal(),
};
(next_state, next_cost as Cost)
})
})
}
fn heuristic(&self, state: &Self::State) -> Self::Cost {
// Manhattan distance to the target coord
let x = state.coord_index % self.grid_width;
let y = state.coord_index / self.grid_width;
let target_x = self.target_index() % self.grid_width;
let target_y = self.target_index() / self.grid_width;
target_x.abs_diff(x) + target_y.abs_diff(y)
}
}
struct MyOpenSet {
queue: BucketQueue<Vec<State>>,
visited: VecSet<State, StateIndexer>,
}
impl MyOpenSet {
fn new(state_indexer: StateIndexer) -> Self {
Self {
queue: BucketQueue::new(),
visited: VecSet::new(state_indexer),
}
}
}
impl OpenSet<State, Cost> for MyOpenSet {
#[inline]
fn insert(&mut self, state: State, cost: Cost) {
self.queue.push(state, cost as usize)
}
#[inline]
fn pop_min(&mut self) -> Option<State> {
while let Some(state) = self.queue.pop_min() {
if self.visited.insert(state) {
return Some(state);
}
}
None
}
}
struct MyCostMap {
map: VecMap<State, Cost, StateIndexer>,
}
impl MyCostMap {
fn new(state_indexer: StateIndexer) -> Self {
Self {
map: VecMap::new(state_indexer),
}
}
}
impl CostMap<State, Cost> for MyCostMap {
fn get(&self, state: &State) -> Option<Cost> {
self.map.get(state).copied()
}
fn insert(&mut self, state: State, cost: Cost) -> bool {
match self.map.entry(&state) {
Some(prev_cost) if *prev_cost <= cost => false,
entry => {
*entry = Some(cost);
true
}
}
}
}
fn solve(input: &str, ultra: bool) -> Option<Cost> {
let grid = parse_input(input);
let min_steps = if ultra { 4 } else { 1 };
let max_steps = if ultra { 10 } else { 3 };
let coord_indexer = *grid.indexer();
let width = coord_indexer.width;
let height = coord_indexer.height;
let grid = grid.to_vec().into_boxed_slice();
let state_indexer = StateIndexer::new(grid.len());
let problem = ClumsyCrucibleProblem {
grid,
grid_width: width,
grid_height: height,
min_steps,
max_steps,
};
shortest_path::a_star(
problem,
MyOpenSet::new(state_indexer),
MyCostMap::new(state_indexer),
)
}
pub fn part_one(input: &str) -> Option<Cost> {
solve(input, false)
}
pub fn part_two(input: &str) -> Option<Cost> {
solve(input, true)
}
#[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));
}
}