Solved 2024/18 P2

This commit is contained in:
2024-12-19 18:04:23 +01:00
parent 2674f6e723
commit 2f5a9630d2
8 changed files with 353 additions and 49 deletions
+8 -4
View File
@@ -96,8 +96,6 @@ 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
@@ -137,8 +135,14 @@ Simulate more of the bytes that are about to corrupt your memory space.
from being reachable from your starting position?* (Provide the answer
as two integers separated by a comma with no other characters.)
Answer:
Your puzzle answer was `31,22`.
Although it hasn\'t changed, you can still [get your puzzle
Both parts of this puzzle are complete! They provide two gold stars:
\*\*
At this point, you should [return to your Advent calendar](/2024) and
try another puzzle.
If you still want to see it, you can [get your puzzle
input](18/input).
+51 -43
View File
@@ -12,56 +12,58 @@ input_f = 'input'
# Part 1 #
# #
#########################################
def part1():
instructions = []
grid = []
if input_f == 'test':
w = 6
h = 6
size = 12
else:
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):
size = 1024
end = (h,w)
start = (0,0)
instructions = []
grid = []
with open(input_f) as file:
for line in file:
l = list2int(line.rstrip().split(','))
instructions.append((l[1],l[0]))
grid = [[ '.' for x in range(0,w+1)] for y in range(0,h+1)]
def is_goal(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 = []
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 neighbors
def part1():
for i in range(size):
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
return len(path[goal_nodes[0]])-1, path
goal_nodes, path = part1()
start_time = time.time()
print('Part 1:',part1(), '\t\t', round((time.time() - start_time)*1000), 'ms')
print('Part 1:',goal_nodes, '\t\t', round((time.time() - start_time)*1000), 'ms')
#########################################
@@ -70,7 +72,13 @@ print('Part 1:',part1(), '\t\t', round((time.time() - start_time)*1000), 'ms')
# #
#########################################
def part2():
return
new_path = path
for i in range(size,len(instructions)):
x = instructions[i]
grid[x[0]][x[1]] = '#'
goal_nodes, new_path = bfs((0,0),is_goal,get_neighbors)
if not goal_nodes:
return x[1],x[0]
start_time = time.time()
print('Part 2:',part2(), '\t\t', round((time.time() - start_time)*1000), 'ms')