aoc2022/d14/part2.py

69 lines
1.4 KiB
Python
Raw Permalink Normal View History

2022-12-14 10:55:52 +00:00
import sys
W = 1000
H = 200
L = [[0 for _ in range(W)] for _ in range(H)]
def prPGM():
print("P2")
print("367 184")
print("2")
t = "201"
print("\n".join(" ".join(t[i] for i in l[317:684]) for l in L))
def pr():
if len(sys.argv)>1 and sys.argv[1] == "G":
prPGM()
else:
t = ".#o+"
print("\n".join("".join(t[i] for i in l[450:550]) for l in L))
def s2li(s,sep=','):
return list(map(int,s.split(sep)))
MAX_Y = 0
for l in sys.stdin.read().splitlines():
lp = l.split(" -> ")
for f,t in zip(lp,lp[1:]):
(a,b,c,d)= (*s2li(f),*s2li(t))
x0 = min(a,c)
y0 = min(b,d)
x1 = max(a,c)
y1 = max(b,d)
MAX_Y = max(MAX_Y, y1)
for x in range(x0,x1+1):
for y in range(y0,y1+1):
L[y][x] = 1
for x in range(W):
L[MAX_Y+2][x] = 1
L = L[:MAX_Y+3]
stop = False
N = 0
while not stop:
N += 1
y = 0
x = 500
more = True # can all left or right
while more and not stop:
more = False
while L[y][x]==0 : #and not stop: # if only air
#print(N, x,y)
y+=1
stop = y == MAX_Y+2
#print(N, x,y)
if L[y][x-1]==0: # can fall left
x -= 1
more = True
elif L[y][x+1]==0: # can fall right
x += 1
more = True
else:
stop = y == 1 # can't put more sand
L[y-1][x] = 2 # mark sand
pr()
print(N)