From d119956812eaa3418ef8b241532c047bc0c1906d Mon Sep 17 00:00:00 2001 From: FrederikBaerentsen Date: Sun, 15 Dec 2024 21:35:10 +0100 Subject: [PATCH] Solved 2024/15 P2 --- 2024/15/15.md | 12 ++++--- 2024/15/solution.py | 77 +++++++++++++++++++++++++++++++++++++-------- 2 files changed, 72 insertions(+), 17 deletions(-) diff --git a/2024/15/15.md b/2024/15/15.md index 1c57b91..b726e66 100644 --- a/2024/15/15.md +++ b/2024/15/15.md @@ -279,8 +279,6 @@ coordinates?* Your puzzle answer was `1568399`. -The first half of this puzzle is complete! It provides one gold star: \* - ## \-\-- Part Two \-\-- {#part2} 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 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). diff --git a/2024/15/solution.py b/2024/15/solution.py index 4cabbc6..07c2aa5 100644 --- a/2024/15/solution.py +++ b/2024/15/solution.py @@ -7,7 +7,7 @@ from termcolor import colored start_time = time.time() -input_f = 'test3' +input_f = 'input' def nprint(grid, cur: set = None, sign: str = None, positions:list = None): """ @@ -312,12 +312,12 @@ def part2(): nprint2(grid,pos) #print(grid) - input() + #input() def canBeMoved(pos,dir,boxes2move): if pos not in boxes2move: 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) == '#': return '#' else: @@ -329,6 +329,11 @@ def part2(): # 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 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 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['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 boxes2move return [] for idx, inst in enumerate(instructions): - print('Move',inst,'('+str(len(instructions)-idx)+')') + #print('Move',inst,'('+str(len(instructions)-idx)+')') dir = directions[inst][0] next = get_value_in_direction(grid,pos,dir) # If wall, don't do anything if next == '#': - nprint2(grid,pos) - input() + #nprint2(grid,pos) + #input() continue # If free space, move there @@ -397,7 +406,7 @@ def part2(): if dir == 'up': #Up works. Need to implement down too. 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.append(canBeMoved(addTuples(pos,directions[inst][1]),dir,boxes2move)) if next == '[': @@ -413,13 +422,13 @@ def part2(): continue boxes2move = [i for i in boxes2move if i is not None] boxes2move = sorted(boxes2move) - print(boxes2move) + #print(boxes2move) prevValues = {} newValues = {} for b in boxes2move: prevValues[b] = grid[b[0]][b[1]] - print(prevValues) + #print(prevValues) for b in boxes2move: tmp = addTuples(b,directions[inst][1]) @@ -432,7 +441,43 @@ def part2(): pos = addTuples(pos,directions[inst][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) # prev = pos # next_chars = ['@'] @@ -463,9 +508,15 @@ def part2(): # pos = next_poss[ndx+1] # else: # grid[n[0]][n[1]] = next_chars[ndx-1] - nprint2(grid,pos) - input() - + #nprint(grid,pos) + #input() + nprint2(grid,pos) + 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() print('Part 2:',part2(), '\t\t', round((time.time() - start_time)*1000), 'ms') \ No newline at end of file