Repository navigation
Expand file tree
/
Copy pathmain.rs
More file actions
249 lines (214 loc) Β· 7.14 KB
/
Copy pathmain.rs
File metadata and controls
249 lines (214 loc) Β· 7.14 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
use std::fs;
#[cfg(feature = "visualize")]
use screen::Screen;
fn is_movable_vertical(grid: &[u8], w: usize, b: (usize, usize), y: usize, dy: isize) -> bool {
let ny = y.checked_add_signed(dy).unwrap();
// check if this box can be moved or if a wall is in the way
if grid[ny * w + b.0] == b'#' || grid[ny * w + b.1] == b'#' {
return false;
}
// check if there is a small box in the way, and if so, if it can be moved
if grid[ny * w + b.0] == b'O' && !is_movable_vertical(grid, w, (b.0, b.0), ny, dy) {
return false;
}
if grid[ny * w + b.1] == b'O' && !is_movable_vertical(grid, w, (b.1, b.1), ny, dy) {
return false;
}
// check if there is a large box directly in the way and if it can be moved
if grid[ny * w + b.0] == b'[' && !is_movable_vertical(grid, w, (b.0, b.0 + 1), ny, dy) {
return false;
}
// check if there is a large box slightly to the left
if grid[ny * w + b.0] == b']' && !is_movable_vertical(grid, w, (b.0 - 1, b.0), ny, dy) {
return false;
}
// check if there is a large box slightly to the right
if grid[ny * w + b.1] == b'[' && !is_movable_vertical(grid, w, (b.1, b.1 + 1), ny, dy) {
return false;
}
true
}
fn move_vertical(grid: &mut [u8], w: usize, b: (usize, usize), y: usize, dy: isize) {
let ny = y.checked_add_signed(dy).unwrap();
// if there is a small box in the way, move it first
if grid[ny * w + b.0] == b'O' {
move_vertical(grid, w, (b.0, b.0), ny, dy);
}
if grid[ny * w + b.1] == b'O' {
move_vertical(grid, w, (b.1, b.1), ny, dy);
}
// if there is a large box directly in the way, move it first
if grid[ny * w + b.0] == b'[' {
move_vertical(grid, w, (b.0, b.0 + 1), ny, dy);
}
// if there is a large box slightly to the left, move it first
if grid[ny * w + b.0] == b']' {
move_vertical(grid, w, (b.0 - 1, b.0), ny, dy);
}
// if there is a large box slightly to the right, move it first
if grid[ny * w + b.1] == b'[' {
move_vertical(grid, w, (b.1, b.1 + 1), ny, dy);
}
// move this box
grid[ny * w + b.0] = grid[y * w + b.0];
grid[ny * w + b.1] = grid[y * w + b.1];
// clear previous position
grid[y * w + b.0] = b'.';
grid[y * w + b.1] = b'.';
}
fn move_horizontal(grid: &mut [u8], w: usize, pos: (usize, usize), dx: isize) {
let sx = pos.1 * w + pos.0.checked_add_signed(dx).unwrap();
let mut i = sx;
while grid[i] == b'O' || grid[i] == b'[' || grid[i] == b']' {
i = i.checked_add_signed(dx).unwrap();
}
if i != sx && grid[i] == b'.' {
if i < sx {
grid.copy_within(i + 1..=sx + 1, i);
} else {
grid.copy_within(sx - 1..i, sx);
}
}
}
fn run_instructions(
mut pos: (usize, usize),
instructions: Vec<u8>,
mut grid: Vec<u8>,
w: usize,
h: usize,
#[cfg(feature = "visualize")] screen: &mut Screen,
) -> usize {
for instr in instructions {
match instr {
// move right
b'>' => {
move_horizontal(&mut grid, w, pos, 1);
if grid[pos.1 * w + pos.0 + 1] == b'.' {
pos.0 += 1;
}
}
// move left
b'<' => {
move_horizontal(&mut grid, w, pos, -1);
if grid[pos.1 * w + pos.0 - 1] == b'.' {
pos.0 -= 1;
}
}
// move up or down
b'^' | b'v' => {
let (new_y, dy) = if instr == b'^' {
(pos.1 - 1, -1)
} else {
(pos.1 + 1, 1)
};
let b = match grid[new_y * w + pos.0] {
b'O' => Some((pos.0, pos.0)),
b'[' => Some((pos.0, pos.0 + 1)),
b']' => Some((pos.0 - 1, pos.0)),
_ => None,
};
if let Some(b) = b
&& is_movable_vertical(&grid, w, b, new_y, dy)
{
move_vertical(&mut grid, w, b, new_y, dy);
}
if grid[new_y * w + pos.0] == b'.' {
pos.1 = new_y;
}
}
_ => panic!("Unknown instruction: {}", instr),
}
#[cfg(feature = "visualize")]
visualize(pos, &grid, screen);
}
let mut total = 0;
for y in 0..h {
for x in 0..w {
let c = grid[y * w + x];
if c == b'O' || c == b'[' {
total += 100 * y + x;
}
}
}
total
}
#[cfg(feature = "visualize")]
fn visualize(pos: (usize, usize), grid: &[u8], screen: &mut Screen) {
let mut new_grid = grid
.iter()
.map(|b| match b {
b'.' => ' ',
b'#' => 'β',
_ => *b as char,
})
.collect::<Vec<_>>();
new_grid[pos.1 * screen.width() + pos.0] = '@';
screen.update(new_grid);
}
fn main() {
for part1 in [true, false] {
let input = fs::read_to_string("input.txt").expect("Could not read file");
let (grid, instructions) = input.split_once("\n\n").unwrap();
let grid_lines = grid.lines().collect::<Vec<_>>();
let mut width = grid_lines[0].len();
let height = grid_lines.len();
let mut grid = grid_lines
.iter()
.flat_map(|l| l.as_bytes())
.copied()
.collect::<Vec<_>>();
let instructions = instructions
.lines()
.flat_map(|l| l.as_bytes())
.copied()
.collect::<Vec<_>>();
if !part1 {
// make grid twice as wide
width *= 2;
let mut wider_grid = vec![b'.'; width * height];
for (i, c) in grid.into_iter().enumerate() {
match c {
b'#' | b'.' => {
wider_grid[i * 2] = c;
wider_grid[i * 2 + 1] = c;
}
b'O' => {
wider_grid[i * 2] = b'[';
wider_grid[i * 2 + 1] = b']';
}
b'@' => {
wider_grid[i * 2] = b'@';
wider_grid[i * 2 + 1] = b'.';
}
_ => panic!("Unknown character in grid: {}", c),
}
}
grid = wider_grid;
}
#[cfg(feature = "visualize")]
let mut screen = Screen::new(width, height, 200);
// find robot
let mut pos = (0, 0);
'outer: for y in 0..height {
for x in 0..width {
if grid[y * width + x] == b'@' {
pos = (x, y);
break 'outer;
}
}
}
grid[pos.1 * width + pos.0] = b'.';
let total = run_instructions(
pos,
instructions,
grid,
width,
height,
#[cfg(feature = "visualize")]
&mut screen,
);
#[cfg(feature = "visualize")]
drop(screen);
println!("{total}");
}
}