Solved 2024/18 P1
This commit is contained in:
+144
@@ -0,0 +1,144 @@
|
||||
## \-\-- Day 18: RAM Run \-\--
|
||||
|
||||
You and The Historians look a lot more pixelated than you remember.
|
||||
You\'re [inside a computer](/2017/day/2) at the North Pole!
|
||||
|
||||
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!\"
|
||||
|
||||
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.
|
||||
|
||||
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:
|
||||
|
||||
5,4
|
||||
4,2
|
||||
4,5
|
||||
3,0
|
||||
2,1
|
||||
6,3
|
||||
2,4
|
||||
1,5
|
||||
0,6
|
||||
3,3
|
||||
2,6
|
||||
5,1
|
||||
1,2
|
||||
5,5
|
||||
2,5
|
||||
6,5
|
||||
1,4
|
||||
0,4
|
||||
6,4
|
||||
1,1
|
||||
6,1
|
||||
1,0
|
||||
0,5
|
||||
1,6
|
||||
2,0
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
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:
|
||||
|
||||
...#...
|
||||
..#..#.
|
||||
....#..
|
||||
...#..#
|
||||
..#..#.
|
||||
.#..#..
|
||||
#.#....
|
||||
|
||||
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:
|
||||
|
||||
OO.#OOO
|
||||
.O#OO#O
|
||||
.OOO#OO
|
||||
...#OO#
|
||||
..#OO#.
|
||||
.#.O#..
|
||||
#.#OOOO
|
||||
|
||||
Simulate the first kilobyte (`1024` bytes) falling onto your memory
|
||||
space. Afterward, *what is the minimum number of steps needed to reach
|
||||
the exit?*
|
||||
|
||||
Your puzzle answer was `294`.
|
||||
|
||||
The first half of this puzzle is complete! It provides one gold star: \*
|
||||
|
||||
## \-\-- Part Two \-\-- {#part2}
|
||||
|
||||
The Historians aren\'t as used to moving around in this pixelated
|
||||
universe as you are. You\'re afraid they\'re not going to be fast enough
|
||||
to make it to the exit before the path is completely blocked.
|
||||
|
||||
To determine how fast everyone needs to go, you need to determine *the
|
||||
first byte that will cut off the path to the exit*.
|
||||
|
||||
In the above example, after the byte at `1,1` falls, there is still a
|
||||
path to the exit:
|
||||
|
||||
O..#OOO
|
||||
O##OO#O
|
||||
O#OO#OO
|
||||
OOO#OO#
|
||||
###OO##
|
||||
.##O###
|
||||
#.#OOOO
|
||||
|
||||
However, after adding the very next byte (at `6,1`), there is no longer
|
||||
a path to the exit:
|
||||
|
||||
...#...
|
||||
.##..##
|
||||
.#..#..
|
||||
...#..#
|
||||
###..##
|
||||
.##.###
|
||||
#.#....
|
||||
|
||||
So, in this example, the coordinates of the first byte that prevents the
|
||||
exit from being reachable are `6,1`.
|
||||
|
||||
Simulate more of the bytes that are about to corrupt your memory space.
|
||||
*What are the coordinates of the first byte that will prevent the exit
|
||||
from being reachable from your starting position?* (Provide the answer
|
||||
as two integers separated by a comma with no other characters.)
|
||||
|
||||
Answer:
|
||||
|
||||
Although it hasn\'t changed, you can still [get your puzzle
|
||||
input](18/input).
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
#!/bin/python3
|
||||
import sys,time,re
|
||||
from pprint import pprint
|
||||
sys.path.insert(0, '../../')
|
||||
from fred import list2int,get_re,nprint,lprint,loadFile,bfs,get_value_in_direction,addTuples,grid_valid
|
||||
start_time = time.time()
|
||||
|
||||
input_f = 'input'
|
||||
|
||||
#########################################
|
||||
# #
|
||||
# Part 1 #
|
||||
# #
|
||||
#########################################
|
||||
def part1():
|
||||
instructions = []
|
||||
grid = []
|
||||
w = 70
|
||||
h = 70
|
||||
end = (h,w)
|
||||
start = (0,0)
|
||||
with open(input_f) as file:
|
||||
for line in file:
|
||||
l = list2int(line.rstrip().split(','))
|
||||
instructions.append((l[0],l[1]))
|
||||
#print(instructions)
|
||||
|
||||
grid = [[ '.' for x in range(0,w+1)] for y in range(0,h+1)]
|
||||
|
||||
for i in range(1024):
|
||||
x = instructions[i]
|
||||
grid[x[0]][x[1]] = '#'
|
||||
|
||||
|
||||
|
||||
def is_goal(node):
|
||||
#print(node)
|
||||
|
||||
return True if node == end else False
|
||||
|
||||
def get_neighbors(node):
|
||||
directions = ['up','down','left','right']
|
||||
offsets = {
|
||||
'up': (-1, 0),
|
||||
'down': (1, 0),
|
||||
'left': (0, -1),
|
||||
'right': (0, 1),
|
||||
}
|
||||
neighbors = []
|
||||
|
||||
# Loop through all the directions
|
||||
for d in directions:
|
||||
tmp = addTuples(offsets[d],node)
|
||||
if get_value_in_direction(grid,node,d) != '#' and grid_valid(tmp[0],tmp[1],grid):
|
||||
neighbors.append((tmp[0],tmp[1]))
|
||||
# Return the list of valid neighbors
|
||||
return neighbors
|
||||
|
||||
goal_nodes, path = bfs((0,0),is_goal,get_neighbors)
|
||||
print(goal_nodes)
|
||||
return len(path[goal_nodes[0]])-1
|
||||
|
||||
start_time = time.time()
|
||||
print('Part 1:',part1(), '\t\t', round((time.time() - start_time)*1000), 'ms')
|
||||
|
||||
|
||||
#########################################
|
||||
# #
|
||||
# Part 2 #
|
||||
# #
|
||||
#########################################
|
||||
def part2():
|
||||
return
|
||||
|
||||
start_time = time.time()
|
||||
print('Part 2:',part2(), '\t\t', round((time.time() - start_time)*1000), 'ms')
|
||||
Reference in New Issue
Block a user