aoc2022/d14/part1.py

48 lines
1004 B
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 pr():
t = ".#o+"
print("\n".join("".join(t[i] for i in l[489:489+92]) for l in L))
def s2li(s,sep=','):
return list(map(int,s.split(sep)))
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)
for x in range(x0,x1+1):
for y in range(y0,y1+1):
L[y][x] = 1
stop = False
N = 0
while not stop:
N += 1
y = 0
x = 500
more = True
while more and not stop:
more = False
while L[y][x]==0 and not stop: # if only air
y+=1
stop = y >= H-3
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
L[y-1][x] = 2 # mark sand
pr()
print(N-1)