-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday15.rs
More file actions
267 lines (238 loc) · 7.79 KB
/
day15.rs
File metadata and controls
267 lines (238 loc) · 7.79 KB
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
use std::collections::HashSet;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct Point {
row: usize,
col: usize,
}
impl Point {
pub fn move_dir(&self, dir: char) -> Option<Point> {
match dir {
'^' => Some(Point {
row: self.row.checked_sub(1)?,
col: self.col,
}),
'v' => Some(Point {
row: self.row + 1,
col: self.col,
}),
'>' => Some(Point {
row: self.row,
col: self.col + 1,
}),
'<' => Some(Point {
row: self.row,
col: self.col.checked_sub(1)?,
}),
_ => None,
}
}
pub fn gps(&self) -> usize {
100 * self.row + self.col
}
}
struct Grid {
walls: HashSet<Point>,
crates: HashSet<Point>,
robot: Point,
instructions: Vec<char>,
}
impl Grid {
pub fn from_input(input: &str, col_factor: usize) -> Self {
let mut walls = HashSet::new();
let mut crates = HashSet::new();
let mut robot = Point { row: 0, col: 0 };
let mut instructions = Vec::new();
let mut switch = false;
for (row, line) in input.lines().enumerate() {
if line.is_empty() {
switch = true;
continue;
}
if !switch {
for (col, ch) in line.trim().chars().enumerate() {
let point = Point {
row,
col: col * col_factor,
};
match ch {
'#' => {
let mut point = point;
for _ in 0..col_factor {
walls.insert(point);
point = point.move_dir('>').unwrap();
}
}
'@' => {
robot = point;
}
'O' => {
crates.insert(point);
}
'.' => {}
_ => {
unreachable!();
}
}
}
} else {
instructions.extend(line.chars());
}
}
Self {
walls,
crates,
robot,
instructions,
}
}
pub fn gps(&self) -> usize {
self.crates.iter().map(|point| point.gps()).sum()
}
pub fn run(&mut self) {
for &instruction in self.instructions.iter() {
let mut new_robot = self.robot.move_dir(instruction).unwrap();
if self.walls.contains(&new_robot) {
continue;
}
if self.crates.contains(&new_robot) {
let mut new_crate = new_robot.move_dir(instruction).unwrap();
while self.walls.contains(&new_crate) || self.crates.contains(&new_crate) {
if self.walls.contains(&new_crate) {
new_crate = new_robot.clone();
new_robot = self.robot.clone();
break;
}
new_crate = new_crate.move_dir(instruction).unwrap();
continue;
}
self.crates.remove(&new_robot);
self.crates.insert(new_crate);
}
self.robot = new_robot;
}
}
// TODO Refactor: Part as return type; part as reference
fn move_wide_crate(
&self,
crate_point: &Point,
direction: char,
remove: &mut HashSet<Point>,
insert: &mut HashSet<Point>,
) -> bool {
if !self.crates.contains(crate_point) {
return true;
}
let new_crate = crate_point.move_dir(direction).unwrap();
let new_crate_right = new_crate.move_dir('>').unwrap();
let new_crate_left = new_crate.move_dir('<').unwrap();
if self.walls.contains(&new_crate) || self.walls.contains(&new_crate_right) {
return false;
}
let move_possible = match direction {
'^' | 'v' => [new_crate, new_crate_right, new_crate_left].iter().fold(
true,
|move_possible, new_crate| {
move_possible
&& (!self.crates.contains(new_crate)
|| self.move_wide_crate(new_crate, direction, remove, insert))
},
),
'>' => {
!self.crates.contains(&new_crate_right)
|| self.move_wide_crate(&new_crate_right, direction, remove, insert)
}
'<' => {
!self.crates.contains(&new_crate_left)
|| self.move_wide_crate(&new_crate_left, direction, remove, insert)
}
_ => {
unreachable!();
}
};
if move_possible {
remove.insert(crate_point.clone());
insert.insert(new_crate);
}
move_possible
}
pub fn run_wide(&mut self) {
for &instruction in self.instructions.clone().iter() {
let new_robot = self.robot.move_dir(instruction).unwrap();
if self.walls.contains(&new_robot) {
continue;
}
let left = new_robot.move_dir('<').unwrap();
let mut remove = HashSet::new();
let mut insert = HashSet::new();
let move_possible = match instruction {
'^' | 'v' => {
(!self.crates.contains(&left)
|| self.move_wide_crate(&left, instruction, &mut remove, &mut insert))
&& (!self.crates.contains(&new_robot)
|| self.move_wide_crate(
&new_robot,
instruction,
&mut remove,
&mut insert,
))
}
'>' => {
!self.crates.contains(&new_robot)
|| self.move_wide_crate(&new_robot, instruction, &mut remove, &mut insert)
}
'<' => {
!self.crates.contains(&left)
|| self.move_wide_crate(&left, instruction, &mut remove, &mut insert)
}
_ => {
unreachable!();
}
};
if move_possible {
self.robot = new_robot;
// TODO beautify
for point in remove.iter() {
self.crates.remove(point);
}
for point in insert.iter() {
self.crates.insert(point.clone());
}
}
}
}
}
pub fn task01(input: &str) -> String {
let mut grid = Grid::from_input(input, 1);
grid.run();
grid.gps().to_string()
}
pub fn task02(input: &str) -> String {
let mut grid = Grid::from_input(input, 2);
grid.run_wide();
grid.gps().to_string()
}
#[cfg(test)]
mod tests {
use super::super::fs_utils::{read_example, read_input};
use super::*;
#[test]
fn test_task01() {
let input = read_example(15, 2);
assert_eq!(task01(&input), "10092");
}
#[test]
fn run_task01() {
let input = read_input(15);
assert_eq!(task01(&input), "1526673");
}
#[test]
fn test_task02() {
let input = read_example(15, 2);
assert_eq!(task02(&input), "9021");
}
#[test]
fn run_task02() {
let input = read_input(15);
assert_eq!(task02(&input), "1535509");
}
}