Skip to content

Commit 2ec8769

Browse files
committed
Day 18 Part 1
1 parent 58b437d commit 2ec8769

File tree

4 files changed

+161
-0
lines changed

4 files changed

+161
-0
lines changed

Day_18/README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
## \--- Day 18: RAM Run ---
2+
3+
You and The Historians look a lot more pixelated than you remember. You're [inside a computer](https://adventofcode.com/2017/day/2) at the North Pole!
4+
5+
Just as you're about to check out your surroundings, a program runs up to you. "This region of memory isn't safe! The User misunderstood what a [pushdown automaton](https://en.wikipedia.org/wiki/Pushdown_automaton) is and their algorithm is pushing whole _bytes_ down on top of us! Run!"
6+
7+
The algorithm is fast - it's going to cause a byte to fall into your memory space once every [nanosecond](https://www.youtube.com/watch?v=9eyFDBPk4Yw)! Fortunately, you're _faster_, and by quickly scanning the algorithm, you create a _list of which bytes will fall_ (your puzzle input) in the order they'll land in your memory space.
8+
9+
Your memory space is a two-dimensional grid with coordinates that range from `0` to `70` both horizontally and vertically. However, for the sake of example, suppose you're on a smaller grid with coordinates that range from `0` to `6` and the following list of incoming byte positions:
10+
11+
```
12+
5,4
13+
4,2
14+
4,5
15+
3,0
16+
2,1
17+
6,3
18+
2,4
19+
1,5
20+
0,6
21+
3,3
22+
2,6
23+
5,1
24+
1,2
25+
5,5
26+
2,5
27+
6,5
28+
1,4
29+
0,4
30+
6,4
31+
1,1
32+
6,1
33+
1,0
34+
0,5
35+
1,6
36+
2,0
37+
```
38+
39+
Each byte position is given as an `X,Y` coordinate, where `X` is the distance from the left edge of your memory space and `Y` is the distance from the top edge of your memory space.
40+
41+
You and The Historians are currently in the top left corner of the memory space (at `0,0`) and need to reach the exit in the bottom right corner (at `70,70` in your memory space, but at `6,6` in this example). You'll need to simulate the falling bytes to plan out where it will be safe to run; for now, simulate just the first few bytes falling into your memory space.
42+
43+
As bytes fall into your memory space, they make that coordinate _corrupted_. Corrupted memory coordinates cannot be entered by you or The Historians, so you'll need to plan your route carefully. You also cannot leave the boundaries of the memory space; your only hope is to reach the exit.
44+
45+
In the above example, if you were to draw the memory space after the first `12` bytes have fallen (using `.` for safe and `#` for corrupted), it would look like this:
46+
47+
```
48+
...#...
49+
..#..#.
50+
....#..
51+
...#..#
52+
..#..#.
53+
.#..#..
54+
#.#....
55+
```
56+
57+
You can take steps up, down, left, or right. After just 12 bytes have corrupted locations in your memory space, the shortest path from the top left corner to the exit would take `22` steps. Here (marked with `O`) is one such path:
58+
59+
```
60+
OO.#OOO
61+
.O#OO#O
62+
.OOO#OO
63+
...#OO#
64+
..#OO#.
65+
.#.O#..
66+
#.#OOOO
67+
```
68+
69+
Simulate the first kilobyte (`1024` bytes) falling onto your memory space. Afterward, _what is the minimum number of steps needed to reach the exit?_
70+
71+
Your puzzle answer was `[REDACTED]`.
72+
73+
The first half of this puzzle is complete! It provides one gold star: ⭐

Day_18/example_18a.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
5,4
2+
4,2
3+
4,5
4+
3,0
5+
2,1
6+
6,3
7+
2,4
8+
1,5
9+
0,6
10+
3,3
11+
2,6
12+
5,1
13+
1,2
14+
5,5
15+
2,5
16+
6,5
17+
1,4
18+
0,4
19+
6,4
20+
1,1
21+
6,1
22+
1,0
23+
0,5
24+
1,6
25+
2,0

Day_18/task_18a.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import collections
2+
3+
4+
def load_data(filename):
5+
with open(filename) as file:
6+
data = file.read().splitlines()
7+
return [list(map(int, row.split(","))) for row in data]
8+
9+
10+
def simulate_memory_failure(grid, byte_positions, num_bytes):
11+
for i in range(num_bytes):
12+
print(byte_positions[i][1], byte_positions[i][0])
13+
grid[byte_positions[i][1]][byte_positions[i][0]] = "#"
14+
return grid
15+
16+
17+
# https://stackoverflow.com/questions/47896461/get-shortest-path-to-a-cell-in-a-2d-array-in-python
18+
def bfs(grid, start, end):
19+
height = len(grid)
20+
width = len(grid[0])
21+
end_x, end_y = end
22+
23+
queue = collections.deque([[start]])
24+
seen = set([start])
25+
while queue:
26+
path = queue.popleft()
27+
x, y = path[-1]
28+
if x == end_x and y == end_y:
29+
return path
30+
for x2, y2 in ((x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)):
31+
if (
32+
0 <= x2 < width
33+
and 0 <= y2 < height
34+
and grid[y2][x2] != "#"
35+
and (x2, y2) not in seen
36+
):
37+
queue.append(path + [(x2, y2)])
38+
seen.add((x2, y2))
39+
return path
40+
41+
42+
if __name__ == "__main__":
43+
width = 71
44+
height = 71
45+
grid = [["." for _ in range(width)] for _ in range(height)]
46+
47+
byte_positions = load_data("Day_18/puzzle_input.txt")
48+
grid = simulate_memory_failure(grid, byte_positions, 1024)
49+
50+
path = bfs(grid, (0, 0), (height - 1, width - 1))
51+
print(len(path) - 1)

Day_18/test_task_18a.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from task_18a import load_data, simulate_memory_failure, bfs
2+
3+
4+
def test_simulate_memory_failure():
5+
width = 7
6+
height = 7
7+
grid = [["." for _ in range(width)] for _ in range(height)]
8+
9+
byte_positions = load_data("Day_18/example_18a.txt")
10+
grid = simulate_memory_failure(grid, byte_positions, 12)
11+
path = bfs(grid, (0, 0), (height - 1, width - 1))
12+
assert len(path) - 1 == 22

0 commit comments

Comments
 (0)