Files
aoc/2022/14/n.py
2023-11-30 19:20:34 +02:00

72 lines
1.6 KiB
Python

import os
input = open(os.path.dirname(__file__) +
"/input.txt", "r").readlines()
cave = set()
for l in input:
points = l.strip().split(' -> ')
prev = (0, 0)
for p in points:
x, y = p.split(',')
x = int(x)
y = int(y)
if prev == (0, 0):
prev = (y, x)
cave.add(prev)
else:
dy = y-prev[0]
dx = x-prev[1]
tmp = set()
dir = 1
if dy < 0 or dx < 0:
dir = -1
if dy != 0:
for i in range(prev[0], prev[0]+dy+dir, dir):
tmp.add((i, prev[1]))
if dx != 0:
for i in range(prev[1], prev[1]+dx+dir, dir):
tmp.add((prev[0], i))
cave.update(tmp)
prev = (y, x)
floor = max(r[0] for r in cave)+2
for i in range(-20000, 20000):
cave.add((floor, i))
p1 = False
for s in range(10000000):
X = 500
Y = 0
current = (Y, X)
# print(current)
while (True):
down = (current[0]+1, current[1])
left = (current[0]+1, current[1]-1)
right = (current[0]+1, current[1]+1)
if (current[0]+1 >= floor and not p1):
print("p1:", s)
p1 = True
if down not in cave:
current = down
if current[0] >= floor:
break
elif left not in cave:
current = left
elif right not in cave:
current = right
else:
break
if current == (0, 500):
print("p2:", s+1)
break
cave.add(current)