-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday15.rs
241 lines (222 loc) · 7.29 KB
/
day15.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
use aoc_runner_derive::aoc;
#[aoc(day15, part1)]
#[must_use]
pub fn part1(input: &str) -> usize {
let mut warehouse = Warehouse::parse(input);
let steps =
Warehouse::translate_instructions(warehouse.instructions, warehouse.map.ncols() as isize);
for step in steps {
let current_position = warehouse.robot_pos;
let next_position = current_position.wrapping_add_signed(step);
match warehouse.map[next_position] {
b'.' => {
warehouse.robot_pos = next_position;
}
b'O' => {
if let Some(free_position) =
Warehouse::find_next_free_position(warehouse.map.as_ref(), next_position, step)
{
warehouse.robot_pos = next_position;
warehouse.map[next_position] = b'.';
warehouse.map[free_position] = b'O';
}
}
_ => {}
}
}
Warehouse::score(warehouse.map.as_ref(), warehouse.map.ncols(), b'O')
}
#[aoc(day15, part2)]
#[must_use]
pub fn part2(input: &str) -> usize {
let warehouse = Warehouse::parse(input);
let mut map = warehouse
.map
.iter()
.flat_map(|&b| match b {
b'#' => [b'#', b'#'],
b'.' => [b'.', b'.'],
b'O' => [b'[', b']'],
_ => unreachable!(),
})
.collect::<Vec<_>>();
let mut robot_pos = 2 * (warehouse.map.ncols() * (warehouse.robot_pos / warehouse.map.ncols()))
+ 2 * (warehouse.robot_pos % warehouse.map.ncols());
let steps = Warehouse::translate_instructions(
warehouse.instructions,
2 * warehouse.map.ncols() as isize,
);
for step in steps {
let next_position = robot_pos.wrapping_add_signed(step);
match map[next_position] {
b'.' => {
robot_pos = next_position;
}
b'[' | b']' => {
if Warehouse::check_obstacle(&map, next_position, step) {
Warehouse::move_box(&mut map, next_position, step);
robot_pos = next_position;
}
}
_ => {}
}
}
Warehouse::score(&map, 2 * warehouse.map.ncols(), b'[')
}
struct Warehouse<'a> {
map: nalgebra::DMatrix<u8>,
instructions: &'a str,
robot_pos: usize,
}
impl<'a> Warehouse<'a> {
fn parse(input: &'a str) -> Self {
let (input_map, instructions) = input.split_once("\n\n").unwrap();
let input_map = input_map.as_bytes();
let width = input_map.iter().position(|&c| c == b'\n').unwrap();
let mut map = nalgebra::DMatrix::from_element(width, (input_map.len() - 1) / width, b'.');
let mut robot_pos = 0;
input_map
.chunks(width + 1)
.enumerate()
.for_each(|(y, line)| {
line.iter()
.enumerate()
.take(width)
.for_each(|(x, &c)| match c {
b'@' => {
robot_pos = x + y * width;
}
_ => map[(x, y)] = c,
});
});
Self {
map,
instructions,
robot_pos,
}
}
#[inline]
fn translate_instructions(
instructions: &str,
width: isize,
) -> impl Iterator<Item = isize> + '_ {
instructions
.bytes()
.filter(|b| !b.is_ascii_whitespace())
.map(move |c| match c {
b'^' => -width,
b'v' => width,
b'<' => -1,
b'>' => 1,
_ => {
unreachable!()
}
})
}
fn find_next_free_position(map: &[u8], start: usize, step: isize) -> Option<usize> {
let mut position = start.wrapping_add_signed(step);
while map[position] == b'O' {
position = position.wrapping_add_signed(step);
}
if map[position] == b'.' {
Some(position)
} else {
None
}
}
#[inline]
fn score(map: &[u8], cols: usize, box_byte: u8) -> usize {
map.iter()
.enumerate()
.map(|(i, &b)| {
if b == box_byte {
100 * (i / cols) + (i % cols)
} else {
0
}
})
.sum()
}
fn check_obstacle(map: &[u8], pos: usize, step: isize) -> bool {
let (left, right) = match map[pos] {
b'[' => (pos, pos + 1),
b']' => (pos - 1, pos),
b'.' => return true,
b'#' => return false,
_ => unreachable!(),
};
if step.abs() == 1 {
Self::check_obstacle(map, pos.wrapping_add_signed(step), step)
} else if map[left.wrapping_add_signed(step)] == b'[' {
Self::check_obstacle(map, left.wrapping_add_signed(step), step)
} else {
Self::check_obstacle(map, left.wrapping_add_signed(step), step)
&& Self::check_obstacle(map, right.wrapping_add_signed(step), step)
}
}
fn move_box(map: &mut [u8], pos: usize, step: isize) {
let (left, right) = match map[pos] {
b'[' => (pos, pos + 1),
b']' => (pos - 1, pos),
b'.' => return,
_ => unreachable!(),
};
if step.abs() == 1 {
Self::move_box(map, pos.wrapping_add_signed(step * 2), step);
} else {
Self::move_box(map, left.wrapping_add_signed(step), step);
Self::move_box(map, right.wrapping_add_signed(step), step);
}
map[left] = b'.';
map[right] = b'.';
map[left.wrapping_add_signed(step)] = b'[';
map[right.wrapping_add_signed(step)] = b']';
}
}
#[cfg(test)]
mod tests {
use super::*;
use indoc::indoc;
const SAMPLE1: &str = indoc! {"
##########
#..O..O.O#
#......O.#
#.OO..O.O#
#..O@..O.#
#O#..O...#
#O..O..O.#
#.OO.O.OO#
#....O...#
##########
<vv>^<v^>v>^vv^v>v<>v^v<v<^vv<<<^><<><>>v<vvv<>^v^>^<<<><<v<<<v^vv^v>^
vvv<<^>^v^^><<>>><>^<<><^vv^^<>vvv<>><^^v>^>vv<>v<<<<v<^v>^<^^>>>^<v<v
><>vv>v^v^<>><>>>><^^>vv>v<^^^>>v^v^<^^>v^^>v^<^v>v<>>v^v^<v>v^^<^^vv<
<<v<^>>^^^^>>>v^<>vvv^><v<<<>^^^vv^<vvv>^>v<^^^^v<>^>vvvv><>>v^<<^^^^^
^><^><>>><>^^<<^^v>>><^<v>^<vv>>v>>>^v><>^v><<<<v>>v<v<v>vvv>^<><<>^><
^>><>^v<><^vvv<^^<><v<<<<<><^v<<<><<<^^<v<^^^><^>>^<v^><<<^>>^v<v^v<v^
>^>>^v>vv>^<<^v<>><<><<v<<v><>v<^vv<<<>^^v^>^^>>><<^v>>v^v><^^>>^<>vv^
<><^^>^^^<><vvvvv^v<v<<>^v<v>v<<^><<><<><<<^^<<<^<<>><<><^^^>^^<>^>v<>
^^>vv<^v^v<vv>^<><v<^v>^^^>>>^^vvv^>vvv<>>>^<^>>>>>^<<^v>^vvv<>^<><<v>
v^^>>><<^^<>>^v^<v^vv<>v^<<>^<^v^v><^<<<><<^<v><v<>vv>>v><v^<vv<>v^<<^
"};
const SAMPLE2: &str = indoc! {"
########
#..O.O.#
##@.O..#
#...O..#
#.#.O..#
#...O..#
#......#
########
<^^>>>vv<v>>v<<
"};
#[test]
pub fn part1_example() {
assert_eq!(part1(SAMPLE1), 10092);
assert_eq!(part1(SAMPLE2), 2028);
}
#[test]
pub fn part2_example() {
assert_eq!(part2(SAMPLE1), 9021);
}
}