Files
python-aoc-2022/day14/part2.py
Sebastian Seedorf b522c7fd11 Day 14
2022-12-15 11:08:00 +01:00

38 lines
874 B
Python

#!/usr/bin/env python3
from itertools import count
lines = ([[int(z) for z in y.split(',')] for y in x.strip().split(" -> ")] for x in open("input.txt"))
blocked = set()
bottom = 0
for line in lines:
for a, b in zip(line[:-1], line[1:]):
for x in range(min(a[0], b[0]), max(a[0], b[0])+1):
for y in range(min(a[1], b[1]), max(a[1], b[1])+1):
blocked.add((x, y))
bottom = max(bottom, y)
def drop():
x, y = 500, 0
if (x, y) in blocked:
return False
while y-1 != bottom:
for nxt in ((x, y+1), (x-1, y+1), (x+1, y+1)):
if nxt not in blocked:
x, y = nxt
break
else:
return x, y
return x, y
for cnt in count():
pos = drop()
if not pos:
print("Start is occupied!", cnt)
break
blocked.add(pos)