Working on 2024/15 P2.

This commit is contained in:
FrederikBaerentsen 2024-12-15 21:19:15 +01:00
parent 960f4f4a06
commit 5d743b0529

View File

@ -238,7 +238,7 @@ def part2():
print(prepend+grid[idx][jdx], end=' ') print(prepend+grid[idx][jdx], end=' ')
else: else:
if grid[idx][jdx] == '.': if grid[idx][jdx] == '.':
print(prepend+colored(grid[idx][jdx],'blue',attrs=["concealed"]),end='') print(prepend+colored(grid[idx][jdx],'blue'),end='')
elif grid[idx][jdx] == 'O': elif grid[idx][jdx] == 'O':
print(prepend+colored(grid[idx][jdx],'red'),end='') print(prepend+colored(grid[idx][jdx],'red'),end='')
elif grid[idx][jdx] == '#': elif grid[idx][jdx] == '#':
@ -277,10 +277,7 @@ def part2():
start = () start = ()
for r,row in enumerate(grid):
for c, col in enumerate(row):
if grid[r][c] == '@':
start = (r,c)
# translate arrows to something get_value_in_direction can use # translate arrows to something get_value_in_direction can use
directions = { directions = {
@ -288,28 +285,71 @@ def part2():
'v':('down',(1, 0)), 'v':('down',(1, 0)),
'>':('right',(0, 1)), '>':('right',(0, 1)),
'<':('left',(0, -1)), '<':('left',(0, -1)),
'up-left': (-1, -1),
'up-right': (-1, 1),
'down-left': (1, -1),
'down-right': (1, 1),
'up': (-1, 0),
'down': (1, 0),
'left': (0, -1),
'right': (0, 1),
} }
pos = start
nprint(grid) nprint(grid)
#print(grid) #print(grid)
grid = resizeGrid(grid) grid = resizeGrid(grid)
for r,row in enumerate(grid):
for c, col in enumerate(row):
if grid[r][c] == '@':
start = (r,c)
pos = start
print() print()
nprint2(grid) nprint2(grid,pos)
#print(grid) #print(grid)
print() 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')
if get_value_in_direction(grid,pos) == '#':
return '#'
else:
if current in ['[',']']:
boxes2move.append(pos)
if current == '[':
if dir == 'up': #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
# boxes2move += canBeMoved(addTuples(pos,directions[dir+'-left']),dir,boxes2move)
# 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)
return boxes2move
return []
for idx, inst in enumerate(instructions): for idx, inst in enumerate(instructions):
#print('Move',inst,'(',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 == '#':
#nprint(grid,pos) nprint2(grid,pos)
#input() input()
continue continue
# If free space, move there # If free space, move there
@ -319,37 +359,113 @@ def part2():
grid[pos[0]][pos[1]] = '@' grid[pos[0]][pos[1]] = '@'
# If box, move the box and the stack of boxes. # If box, move the box and the stack of boxes.
if next == 'O': if next in ['[',']']: #part 2, also check the sides, if theres a full box [] move the whole box
#print('@',pos)
prev = pos if inst in ['<','>']:
next_chars = ['@'] #print('@',pos)
next_poss = [pos] prev = pos
skip = False next_chars = ['@']
while True: next_poss = [pos]
nextPos = addTuples(pos,directions[inst][1]) skip = False
nextChar = get_value_in_direction(grid,nextPos) while True:
#print(nextPos,nextChar) nextPos = addTuples(pos,directions[inst][1])
if nextChar == 'O': nextChar = get_value_in_direction(grid,nextPos)
next_chars.append(nextChar) #print(nextPos,nextChar)
next_poss.append(nextPos) if nextChar in ['[',']']:
pos = nextPos next_chars.append(nextChar)
if nextChar == '.': next_poss.append(nextPos)
next_chars.append(nextChar) pos = nextPos
next_poss.append(nextPos) if nextChar == '.':
break next_chars.append(nextChar)
if nextChar == '#': next_poss.append(nextPos)
skip = True break
pos = prev if nextChar == '#':
break skip = True
pos = prev
break
#input()
if not skip:
for ndx,n in enumerate(next_poss):
if ndx == 0:
grid[n[0]][n[1]] = '.'
pos = next_poss[ndx+1]
else:
grid[n[0]][n[1]] = next_chars[ndx-1]
else:
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)
# 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)
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 = ['@']
# next_poss = [pos]
# skip = False
# while True:
# nextPos = addTuples(pos,directions[inst][1])
# nextChar = get_value_in_direction(grid,nextPos)
# print(nextPos,nextChar)
# if nextChar == '[' or nextChar == ']':
# next_chars.append(nextChar)
# next_poss.append(nextPos)
# pos = nextPos
# if nextChar == '.':
# next_chars.append(nextChar)
# next_poss.append(nextPos)
# break
# if nextChar == '#':
# skip = True
# pos = prev
# break
# if not skip:
# for ndx,n in enumerate(next_poss):
# if ndx == 0:
# grid[n[0]][n[1]] = '.'
# pos = next_poss[ndx+1]
# else:
# grid[n[0]][n[1]] = next_chars[ndx-1]
nprint2(grid,pos)
input()
#input()
if not skip:
for ndx,n in enumerate(next_poss):
if ndx == 0:
grid[n[0]][n[1]] = '.'
pos = next_poss[ndx+1]
else:
grid[n[0]][n[1]] = next_chars[ndx-1]
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')