-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday20.rs
213 lines (198 loc) · 6.73 KB
/
day20.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
use aoc_runner_derive::aoc;
use strum::IntoEnumIterator;
#[aoc(day20, part1)]
#[must_use]
pub fn part1(input: &str) -> usize {
Maze::<139>::parse(input).solve1::<100>()
}
#[aoc(day20, part2)]
#[must_use]
pub fn part2(input: &str) -> usize {
Maze::<139>::parse(input).solve2::<100>()
}
struct Maze<const D: usize> {
map: nalgebra::SMatrix<u8, D, D>,
costmap: nalgebra::SMatrix<[usize; 2], D, D>,
original_cost: usize,
}
impl<const D: usize> Maze<D> {
#[inline]
fn parse(input: &str) -> Self {
let input = input.as_bytes();
let map = nalgebra::SMatrix::<u8, D, D>::from_iterator(
input
.chunks(unsafe { D.unchecked_add(3) })
.skip(1)
.take(D)
.flat_map(|line| line.iter().skip(1).take(D).copied()),
);
let pos: Vec<_> = map
.iter()
.enumerate()
.filter(|(_, &b)| b == b'S' || b == b'E')
.take(2)
.map(|(i, _)| (i % D, i / D))
.collect();
let mut costmap = nalgebra::SMatrix::from_element([usize::MAX; 2]);
for (i, &pos) in pos.iter().enumerate() {
costmap[pos][i] = 0;
let mut queue = pos;
'outer: loop {
let new_cost = unsafe { costmap[queue][i].unchecked_add(1) };
if let Some(new_pos) = Direction::iter()
.map(|dir| dir.step(queue))
.filter(|&new_pos| new_pos.0 < D && new_pos.1 < D)
.filter(|&new_pos| map[new_pos] != b'#')
.find(|&new_pos| new_cost < costmap[new_pos][i])
{
queue = new_pos;
costmap[new_pos][i] = new_cost;
continue 'outer;
}
break;
}
}
Self {
map,
costmap,
original_cost: costmap[pos[0]][1],
}
}
#[inline]
fn solve1<const L: usize>(self) -> usize {
self.map
.column_iter()
.enumerate()
.flat_map(|(y, col)| {
col.into_iter()
.enumerate()
.filter(|(_, &b)| b == b'#')
.map(move |(x, _)| (x, y))
})
.map(|pos| {
Direction::iter()
.filter(|dir| {
let new_pos0 = dir.step(pos);
if new_pos0.0 >= D || new_pos0.1 >= D {
return false;
}
let new_cost0 = self.costmap[new_pos0][0];
if new_cost0 == usize::MAX {
return false;
}
let new_pos1 = dir.reverse().step(pos);
if new_pos1.0 >= D || new_pos1.1 >= D {
return false;
}
let new_cost1 = self.costmap[new_pos1][1];
if new_cost1 == usize::MAX {
return false;
}
let new_cost =
unsafe { new_cost0.unchecked_add(new_cost1).unchecked_add(2) };
new_cost < self.original_cost
&& unsafe { self.original_cost.unchecked_sub(new_cost) } >= L
})
.count()
})
.sum()
}
#[inline]
fn solve2<const L: usize>(self) -> usize {
self.map
.column_iter()
.enumerate()
.flat_map(|(y, col)| {
col.into_iter()
.enumerate()
.filter(|(_, &b)| b != b'#')
.map(move |(x, _)| (x, y))
})
.map(|pos| {
let start_cost = self.costmap[pos][0];
let pos = (pos.0 as isize, pos.1 as isize);
(-((pos.0).min(20))..=20)
.map(|dx| (dx, unsafe { pos.0.unchecked_add(dx) }))
.filter(|&(_, new_x)| new_x < D as isize)
.map(|(dx, new_x)| {
let dx_abs = dx.abs();
let max_y = unsafe { 20_isize.unchecked_sub(dx_abs) };
(-((pos.1).min(max_y))..=max_y)
.map(|dy| (dy, unsafe { pos.1.unchecked_add(dy) }))
.filter(|&(_, new_y)| new_y < D as isize)
.filter(|&(dy, new_y)| {
let new_pos = (new_x as usize, new_y as usize);
let goal_cost = self.costmap[new_pos][1];
if goal_cost == usize::MAX {
return false;
}
let new_cost = unsafe {
start_cost
.unchecked_add(goal_cost)
.unchecked_add(dx_abs.unchecked_add(dy.abs()) as usize)
};
new_cost < self.original_cost
&& unsafe { self.original_cost.unchecked_sub(new_cost) } >= L
})
.count()
})
.sum::<usize>()
})
.sum()
}
}
#[derive(Clone, Copy, strum::EnumIter)]
enum Direction {
N,
E,
S,
W,
}
impl Direction {
fn reverse(self) -> Self {
match self {
Self::N => Self::S,
Self::E => Self::W,
Self::S => Self::N,
Self::W => Self::E,
}
}
fn step(self, pos: (usize, usize)) -> (usize, usize) {
match self {
Self::N => (pos.0, pos.1.wrapping_sub(1)),
Self::E => (unsafe { pos.0.unchecked_add(1) }, pos.1),
Self::S => (pos.0, unsafe { pos.1.unchecked_add(1) }),
Self::W => (pos.0.wrapping_sub(1), pos.1),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use indoc::indoc;
const SAMPLE: &str = indoc! {"
###############
#...#...#.....#
#.#.#.#.#.###.#
#S#...#.#.#...#
#######.#.#.###
#######.#.#...#
#######.#.###.#
###..E#...#...#
###.#######.###
#...###...#...#
#.#####.#.###.#
#.#...#.#.#...#
#.#.#.#.#.#.###
#...#...#...###
###############
"};
#[test]
pub fn part1_example() {
assert_eq!(Maze::<13>::parse(SAMPLE).solve1::<64>(), 1);
}
#[test]
pub fn part2_example() {
assert_eq!(Maze::<13>::parse(SAMPLE).solve2::<76>(), 3);
}
}