Skip to content

Commit 333d4e7

Browse files
committed
Don't move robot
1 parent dfb46f9 commit 333d4e7

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro
8686
| 12 | [Garden Groups](https://adventofcode.com/2024/day/12) | [Source](src/year2024/day12.rs) | 289 |
8787
| 13 | [Claw Contraption](https://adventofcode.com/2024/day/13) | [Source](src/year2024/day13.rs) | 14 |
8888
| 14 | [Restroom Redoubt](https://adventofcode.com/2024/day/14) | [Source](src/year2024/day14.rs) | 74 |
89-
| 15 | [Warehouse Woes](https://adventofcode.com/2024/day/15) | [Source](src/year2024/day15.rs) | 332 |
89+
| 15 | [Warehouse Woes](https://adventofcode.com/2024/day/15) | [Source](src/year2024/day15.rs) | 303 |
9090

9191
## 2023
9292

src/year2024/day15.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ pub fn parse(input: &str) -> Input<'_> {
3535
pub fn part1(input: &Input<'_>) -> i32 {
3636
let (grid, moves) = input;
3737

38+
// We don't need to move the robot symbol so mark as empty space once located.
3839
let mut grid = grid.clone();
3940
let mut position = grid.find(b'@').unwrap();
41+
grid[position] = b'.';
4042

4143
// Treat moves as a single string ignoring any newline characters.
4244
for b in moves.bytes() {
@@ -57,6 +59,7 @@ pub fn part2(input: &Input<'_>) -> i32 {
5759

5860
let mut grid = stretch(grid);
5961
let mut position = grid.find(b'@').unwrap();
62+
grid[position] = b'.';
6063

6164
// Reuse to minimize allocations.
6265
let mut todo = Vec::new();
@@ -78,7 +81,7 @@ pub fn part2(input: &Input<'_>) -> i32 {
7881

7982
fn narrow(grid: &mut Grid<u8>, start: &mut Point, direction: Point) {
8083
let mut position = *start + direction;
81-
let mut size = 2;
84+
let mut size = 1;
8285

8386
// Search for the next wall or open space.
8487
while grid[position] != b'.' && grid[position] != b'#' {
@@ -89,7 +92,7 @@ fn narrow(grid: &mut Grid<u8>, start: &mut Point, direction: Point) {
8992
// Move items one space in direction.
9093
if grid[position] == b'.' {
9194
let mut previous = b'.';
92-
let mut position = *start;
95+
let mut position = *start + direction;
9396

9497
for _ in 0..size {
9598
swap(&mut previous, &mut grid[position]);
@@ -110,12 +113,7 @@ fn wide(
110113
id: usize,
111114
) {
112115
// Short circuit if path in front of robot is empty.
113-
let position = *start;
114-
let next = position + direction;
115-
116-
if grid[next] == b'.' {
117-
grid[position] = b'.';
118-
grid[next] = b'@';
116+
if grid[*start + direction] == b'.' {
119117
*start += direction;
120118
return;
121119
}
@@ -151,8 +149,8 @@ fn wide(
151149
}
152150
}
153151

154-
// Move boxes in reverse order.
155-
for &point in todo.iter().rev() {
152+
// Move boxes in reverse order (skipping the robot as an optimization).
153+
for &point in todo[1..].iter().rev() {
156154
grid[point + direction] = grid[point];
157155
grid[point] = b'.';
158156
}

0 commit comments

Comments
 (0)