Skip to content

Instantly share code, notes, and snippets.

@Transfusion
Created December 24, 2022 06:54
Show Gist options
  • Save Transfusion/6e09ddb2e9667c7bb7c62f365b903c02 to your computer and use it in GitHub Desktop.
Save Transfusion/6e09ddb2e9667c7bb7c62f365b903c02 to your computer and use it in GitHub Desktop.
# aoc_2022_day_24_tf.py
from collections import *
import math, sys, copy, functools, json, re
# In one minute, each blizzard moves one position in the direction it is pointing:
DIRS = [(0,-1),(-1,0),(0,1),(1,0)]
blizzards = defaultdict(list) # pos to direction
# f = open("day24.input")
f = open("day24_2.input")
grid = []
for line in f:
line = line.strip()
grid.append(list(line))
print(line)
f.close()
BZ_TO_DIR = {
'<': (0,-1),
'>': (0,1),
'^': (-1, 0),
'v': (1,0)
}
m,n = len(grid), len(grid[0])
def inb(r, c):
return 1<=r<m-1 and 1<=c<n-1
def dbg_grid_2():
print("---dbg2 s")
for i in range(m):
row = ""
for j in range(n):
if inb(i, j):
row += str(len(blizzards[(i,j)])) if blizzards[(i,j)] else '.'
else:
row += grid[i][j]
print(row)
print("---dbg2 end")
for i in range(m):
for j in range(n):
if grid[i][j] in '<>^v':
dr, dc = BZ_TO_DIR[grid[i][j]]
blizzards[ (i,j) ].append( (dr, dc) )
def move_blizzards():
# global blizzards
bzx = defaultdict(list)
for r, c in list(blizzards.keys()):
for dr, dc in blizzards[(r, c)]:
nr = ( ( (r -1 ) + dr ) % ( (m) - 2) ) + 1
nc = ( ( (c -1 ) + dc ) % ( (n) - 2) ) + 1
bzx[(nr,nc)].append( (dr, dc) )
# blizzards = bzx
return bzx
def can_wait(pr, pc, nbz):
for r, c in nbz.keys():
if (pr, pc) == (r,c):
return False
return True
def test(sr, sc, tr, tc):
global blizzards
visited = set()
visited.add( (sr, sc, 0) )
q = deque()
q.append( (sr, sc, 0) )
moves = 0
while q:
print("moves!", moves, end="\r")
# print("curr!!", moves, q)
# print("size of q", len(q), moves, end="\r")
# print(blizzards)
nbz = move_blizzards()
for _ in range(len(q)):
r, c, min = q.popleft()
if (r, c) == (tr, tc):
print('reached', (r, c), moves, min)
# dbg_grid_2()
return moves
# sys.exit()
for dr, dc in DIRS:
nr, nc = r+dr, c+dc
if (nr, nc) == (tr, tc):
q.append((nr, nc, min+1)) # special case
break # no need to cont
if not inb(nr, nc): continue
if not can_wait(nr, nc, nbz):
continue
if (nr, nc, min + 1) not in visited:
visited.add((nr, nc, min + 1))
q.append( (nr, nc, min + 1) )
# appended = True
if can_wait(r, c, nbz):
# check if you can wait / there is a blizzard headed towards you!!!
# you cannot share a position with a blizzard
if (r, c, min + 1) not in visited:
visited.add((r, c, min + 1))
q.append( (r, c, min + 1) )
blizzards = nbz
moves += 1
a = (test(0,1, m-1, n-2))
b = (test(m-1, n-2, 0,1))
c = (test(0,1, m-1, n-2))
print(a,b,c, a+b+c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment