-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday_22.rs
259 lines (221 loc) Β· 6.46 KB
/
day_22.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
use hashbrown::HashSet;
use nd_vec::vector;
use std::collections::VecDeque;
use common::{solution, Answer};
solution!("Monkey Map", 22);
type Point = nd_vec::Vec2<isize>;
fn part_a(input: &str) -> Answer {
let mut world = World::parse(input);
world.run(wrap_2d);
world.password().into()
}
fn part_b(input: &str) -> Answer {
let mut world = World::parse(input);
world.run(wrap_3d);
world.password().into()
}
#[derive(Debug)]
struct World {
// == World ==
walls: HashSet<Point>,
open: HashSet<Point>,
side_len: usize,
// == Player ==
pos: Point,
dir: Direction,
instructions: VecDeque<Instruction>,
}
#[derive(Debug)]
enum Instruction {
Move(usize),
Turn(Direction),
}
#[derive(Debug, Copy, Clone)]
enum Direction {
Right,
Down,
Left,
Up,
}
impl World {
fn parse(raw: &str) -> Self {
let (map, instructions) = raw.split_once("\n\n").unwrap();
let mut walls = HashSet::new();
let mut open = HashSet::new();
let mut side_len = 0;
for (y, line) in map.lines().enumerate() {
for (x, c) in line.chars().enumerate() {
let (x, y) = (x as isize, y as isize);
if y == 0 && c != ' ' {
side_len += 1;
}
match c {
'#' => walls.insert(vector!(x, y)),
'.' => open.insert(vector!(x, y)),
' ' => continue,
_ => panic!("Invalid character: {}", c),
};
}
}
let start = open
.iter()
.filter(|p| p.y() == 0)
.min_by_key(|p| p.x())
.unwrap();
Self {
walls,
pos: *start,
side_len,
open,
dir: Direction::Right,
instructions: VecDeque::from(Instruction::parse(instructions)),
}
}
fn password(&self) -> usize {
(1000 * (self.pos.y() + 1) + 4 * (self.pos.x() + 1) + self.dir as isize) as usize
}
fn run(&mut self, wrap: fn(&Self, Point) -> Option<(Point, Direction)>) {
while let Some(i) = self.instructions.pop_front() {
match i {
Instruction::Turn(d) => self.dir = d.turn(&self.dir),
Instruction::Move(n) => {
for _ in 0..n {
let new_pos = self.dir.apply(self.pos);
if self.walls.contains(&new_pos) {
break;
}
if self.open.contains(&new_pos) {
self.pos = new_pos;
continue;
}
if let Some((new_pos, new_dir)) = wrap(self, new_pos) {
self.pos = new_pos;
self.dir = new_dir;
}
}
}
}
}
}
}
impl Instruction {
/// The directions from these instructions are relative to the current direction.
/// So if we're facing right, and we turn left, we're now facing up.
fn parse(raw: &str) -> Vec<Self> {
let mut out = Vec::new();
let mut working = String::new();
for i in raw.chars() {
match i {
i if i.is_ascii_digit() => working.push(i),
'L' => {
Self::flush(&mut working, &mut out);
out.push(Instruction::Turn(Direction::Left));
}
'R' => {
Self::flush(&mut working, &mut out);
out.push(Instruction::Turn(Direction::Right));
}
_ => continue,
}
}
Self::flush(&mut working, &mut out);
out
}
fn flush(working: &mut String, out: &mut Vec<Self>) {
if !working.is_empty() {
out.push(Instruction::Move(working.parse().unwrap()));
working.clear();
}
}
}
impl Direction {
fn from_index(i: usize) -> Self {
match i {
0 => Direction::Right,
1 => Direction::Down,
2 => Direction::Left,
3 => Direction::Up,
_ => panic!("Invalid direction index: {}", i),
}
}
fn apply(&self, pos: Point) -> Point {
match self {
Direction::Left => pos - vector!(1, 0),
Direction::Right => pos + vector!(1, 0),
Direction::Up => pos - vector!(0, 1),
Direction::Down => pos + vector!(0, 1),
}
}
fn turn(&self, dir: &Direction) -> Self {
Self::from_index(match self {
Direction::Right => (*dir as usize + 1) % 4,
Direction::Left => (*dir as usize + 3) % 4,
_ => panic!("Invalid turn direction: {:?}", self),
})
}
fn reverse(&self) -> Self {
match self {
Direction::Left => Direction::Right,
Direction::Right => Direction::Left,
Direction::Up => Direction::Down,
Direction::Down => Direction::Up,
}
}
}
fn wrap_2d(world: &World, mut pos: Point) -> Option<(Point, Direction)> {
loop {
pos = world.dir.reverse().apply(pos);
if !world.walls.contains(&pos) && !world.open.contains(&pos) {
let pos = world.dir.apply(pos);
if world.walls.contains(&pos) {
break None;
}
break Some((pos, world.dir));
}
}
}
fn wrap_3d(world: &World, mut _pos: Point) -> Option<(Point, Direction)> {
let _ = world.side_len;
todo!()
}
#[cfg(test)]
mod test {
use indoc::indoc;
const CASE: &str = indoc! {"
...#
.#..
#...
....
...#.......#
........#...
..#....#....
..........#.
...#....
.....#..
.#......
......#.
10R5L5R10L4R5L5
"};
#[test]
fn part_a() {
assert_eq!(super::part_a(CASE), 6032.into());
}
// #[test]
// fn part_b() {
// assert_eq!(super::part_b(CASE), 5031.into());
// }
}
/*
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
2 2 2 2 3 3 3 3 4 4 4 4
2 2 2 2 3 3 3 3 4 4 4 4
2 2 2 2 3 3 3 3 4 4 4 4
2 2 2 2 3 3 3 3 4 4 4 4
5 5 5 5 6 6 6 6
5 5 5 5 6 6 6 6
5 5 5 5 6 6 6 6
5 5 5 5 6 6 6 6
*/