Solved 2024/15 P2

This commit is contained in:
FrederikBaerentsen 2024-12-15 21:35:10 +01:00
parent 5d743b0529
commit d119956812
2 changed files with 72 additions and 17 deletions

View File

@ -279,8 +279,6 @@ coordinates?*
Your puzzle answer was `1568399`. Your puzzle answer was `1568399`.
The first half of this puzzle is complete! It provides one gold star: \*
## \-\-- Part Two \-\-- {#part2} ## \-\-- Part Two \-\-- {#part2}
The lanternfish use your information to find a safe moment to swim in The lanternfish use your information to find a safe moment to swim in
@ -471,8 +469,14 @@ The sum of these boxes\' GPS coordinates is `9021`.
Predict the motion of the robot and boxes in this new, scaled-up Predict the motion of the robot and boxes in this new, scaled-up
warehouse. *What is the sum of all boxes\' final GPS coordinates?* warehouse. *What is the sum of all boxes\' final GPS coordinates?*
Answer: Your puzzle answer was `1575877`.
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](15/input). input](15/input).

View File

@ -7,7 +7,7 @@ from termcolor import colored
start_time = time.time() start_time = time.time()
input_f = 'test3' input_f = 'input'
def nprint(grid, cur: set = None, sign: str = None, positions:list = None): def nprint(grid, cur: set = None, sign: str = None, positions:list = None):
""" """
@ -312,12 +312,12 @@ def part2():
nprint2(grid,pos) nprint2(grid,pos)
#print(grid) #print(grid)
input() #input()
def canBeMoved(pos,dir,boxes2move): def canBeMoved(pos,dir,boxes2move):
if pos not in boxes2move: if pos not in boxes2move:
current = get_value_in_direction(grid,pos) current = get_value_in_direction(grid,pos)
print('Checking if',current,pos,'in direction',dir,'can move') #print('Checking if',current,pos,'in direction',dir,'can move')
if get_value_in_direction(grid,pos) == '#': if get_value_in_direction(grid,pos) == '#':
return '#' return '#'
else: else:
@ -329,6 +329,11 @@ def part2():
# boxes2move += canBeMoved(addTuples(pos,directions[dir]),dir,boxes2move) # boxes2move += canBeMoved(addTuples(pos,directions[dir]),dir,boxes2move)
# boxes2move += canBeMoved(addTuples(pos,directions['right']),dir,boxes2move) # boxes2move += canBeMoved(addTuples(pos,directions['right']),dir,boxes2move)
return canBeMoved(addTuples(pos,directions[dir+'-right']),dir,boxes2move) or canBeMoved(addTuples(pos,directions[dir]),dir,boxes2move) or canBeMoved(addTuples(pos,directions['right']),dir,boxes2move) return canBeMoved(addTuples(pos,directions[dir+'-right']),dir,boxes2move) or canBeMoved(addTuples(pos,directions[dir]),dir,boxes2move) or canBeMoved(addTuples(pos,directions['right']),dir,boxes2move)
if dir == 'down': #left side of box
# boxes2move += canBeMoved(addTuples(pos,directions[dir+'-right']),dir,boxes2move)
# boxes2move += canBeMoved(addTuples(pos,directions[dir]),dir,boxes2move)
# boxes2move += canBeMoved(addTuples(pos,directions['right']),dir,boxes2move)
return canBeMoved(addTuples(pos,directions[dir+'-right']),dir,boxes2move) or canBeMoved(addTuples(pos,directions[dir]),dir,boxes2move) or canBeMoved(addTuples(pos,directions['right']),dir,boxes2move)
if current == ']': if current == ']':
if dir == 'up': #left side of box if dir == 'up': #left side of box
@ -336,20 +341,24 @@ def part2():
# boxes2move += canBeMoved(addTuples(pos,directions[dir]),dir,boxes2move) # boxes2move += canBeMoved(addTuples(pos,directions[dir]),dir,boxes2move)
# boxes2move += canBeMoved(addTuples(pos,directions['left']),dir,boxes2move) # boxes2move += canBeMoved(addTuples(pos,directions['left']),dir,boxes2move)
return canBeMoved(addTuples(pos,directions[dir+'-left']),dir,boxes2move) or canBeMoved(addTuples(pos,directions[dir]),dir,boxes2move) or canBeMoved(addTuples(pos,directions['left']),dir,boxes2move)
if dir == 'down': #left side of box
return canBeMoved(addTuples(pos,directions[dir+'-left']),dir,boxes2move) or canBeMoved(addTuples(pos,directions[dir]),dir,boxes2move) or canBeMoved(addTuples(pos,directions['left']),dir,boxes2move) return canBeMoved(addTuples(pos,directions[dir+'-left']),dir,boxes2move) or canBeMoved(addTuples(pos,directions[dir]),dir,boxes2move) or canBeMoved(addTuples(pos,directions['left']),dir,boxes2move)
return boxes2move return boxes2move
return [] return []
for idx, inst in enumerate(instructions): for idx, inst in enumerate(instructions):
print('Move',inst,'('+str(len(instructions)-idx)+')') #print('Move',inst,'('+str(len(instructions)-idx)+')')
dir = directions[inst][0] dir = directions[inst][0]
next = get_value_in_direction(grid,pos,dir) next = get_value_in_direction(grid,pos,dir)
# If wall, don't do anything # If wall, don't do anything
if next == '#': if next == '#':
nprint2(grid,pos) #nprint2(grid,pos)
input() #input()
continue continue
# If free space, move there # If free space, move there
@ -397,7 +406,7 @@ def part2():
if dir == 'up': #Up works. Need to implement down too. if dir == 'up': #Up works. Need to implement down too.
boxes2move = [] #list of boxes (set coords) to move boxes2move = [] #list of boxes (set coords) to move
print('next is',next,'im at',pos) #print('next is',next,'im at',pos)
# boxes2move += canBeMoved(addTuples(pos,directions[inst][1]),dir,boxes2move) # boxes2move += canBeMoved(addTuples(pos,directions[inst][1]),dir,boxes2move)
boxes2move.append(canBeMoved(addTuples(pos,directions[inst][1]),dir,boxes2move)) boxes2move.append(canBeMoved(addTuples(pos,directions[inst][1]),dir,boxes2move))
if next == '[': if next == '[':
@ -413,13 +422,13 @@ def part2():
continue continue
boxes2move = [i for i in boxes2move if i is not None] boxes2move = [i for i in boxes2move if i is not None]
boxes2move = sorted(boxes2move) boxes2move = sorted(boxes2move)
print(boxes2move) #print(boxes2move)
prevValues = {} prevValues = {}
newValues = {} newValues = {}
for b in boxes2move: for b in boxes2move:
prevValues[b] = grid[b[0]][b[1]] prevValues[b] = grid[b[0]][b[1]]
print(prevValues) #print(prevValues)
for b in boxes2move: for b in boxes2move:
tmp = addTuples(b,directions[inst][1]) tmp = addTuples(b,directions[inst][1])
@ -432,7 +441,43 @@ def part2():
pos = addTuples(pos,directions[inst][1]) pos = addTuples(pos,directions[inst][1])
grid[pos[0]][pos[1]] = '@' grid[pos[0]][pos[1]] = '@'
if dir == 'down': #Up works. Need to implement down too.
boxes2move = [] #list of boxes (set coords) to move
#print('next is',next,'im at',pos)
# boxes2move += canBeMoved(addTuples(pos,directions[inst][1]),dir,boxes2move)
boxes2move.append(canBeMoved(addTuples(pos,directions[inst][1]),dir,boxes2move))
if next == '[':
# boxes2move += canBeMoved(addTuples(pos,directions[dir+'-right']),dir,boxes2move)
boxes2move.append(canBeMoved(addTuples(pos,directions[dir+'-right']),dir,boxes2move))
if next == ']':
# boxes2move += canBeMoved(addTuples(pos,directions[dir+'-left']),dir,boxes2move)
boxes2move.append(canBeMoved(addTuples(pos,directions[dir+'-left']),dir,boxes2move))
# boxes2move += canBeMoved(addTuples(pos,directions[inst][1]),dir,boxes2move)
boxes2move.append(canBeMoved(addTuples(pos,directions[inst][1]),dir,boxes2move))
#boxes2move = list(set(boxes2move))
if '#' in boxes2move:
continue
boxes2move = [i for i in boxes2move if i is not None]
boxes2move = sorted(boxes2move,reverse=True)
#print(boxes2move)
prevValues = {}
newValues = {}
for b in boxes2move:
prevValues[b] = grid[b[0]][b[1]]
#print(prevValues)
for b in boxes2move:
tmp = addTuples(b,directions[inst][1])
grid[b[0]][b[1]] = '.'
grid[tmp[0]][tmp[1]] = prevValues[b]
#nprint2(grid,pos)
#input()
grid[pos[0]][pos[1]] = '.'
pos = addTuples(pos,directions[inst][1])
grid[pos[0]][pos[1]] = '@'
# print('@',pos) # print('@',pos)
# prev = pos # prev = pos
# next_chars = ['@'] # next_chars = ['@']
@ -463,9 +508,15 @@ def part2():
# pos = next_poss[ndx+1] # pos = next_poss[ndx+1]
# else: # else:
# grid[n[0]][n[1]] = next_chars[ndx-1] # grid[n[0]][n[1]] = next_chars[ndx-1]
#nprint(grid,pos)
#input()
nprint2(grid,pos) nprint2(grid,pos)
input() result = 0
for r,row in enumerate(grid):
for c,char in enumerate(row):
if char == '[':
result += (100*r+c)
return result
start_time = time.time() start_time = time.time()
print('Part 2:',part2(), '\t\t', round((time.time() - start_time)*1000), 'ms') print('Part 2:',part2(), '\t\t', round((time.time() - start_time)*1000), 'ms')