summaryrefslogtreecommitdiff
path: root/14_2022.py
blob: 1a56eab8a97aa3f20faf16d65f2069b3151241ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import sys

infile = sys.argv[1] if len(sys.argv) > 1 else "data/14_2022.txt"
data = open(infile).read().strip()
lines = [x for x in data.split("\n")]

r = set()
for l in lines:
    prev = None
    for p in l.split("->"):
        x, y = p.split(",")
        x, y = int(x), int(y)
        if prev is not None:
            dx = x - prev[0]
            dy = y - prev[1]
            len_ = max(abs(dx), abs(dy))
            for i in range(len_ + 1):
                xx = prev[0] + i * (1 if dx > 0 else (-1 if dx < 0 else 0))
                yy = prev[1] + i * (1 if dy > 0 else (-1 if dy < 0 else 0))
                r.add((xx, yy))
        prev = (x, y)

floor = 2 + max(i[1] for i in r)
low_x = min(i[0] for i in r) - 2000
high_x = max(i[0] for i in r) + 2000
for x in range(low_x, high_x):
    r.add((x, floor))

did_p1 = False
for t in range(1000000):
    rock = (500, 0)
    while True:
        if rock[1] + 1 >= floor and (not did_p1):
            did_p1 = True
            print(t)
        if (rock[0], rock[1] + 1) not in r:
            rock = (rock[0], rock[1] + 1)
        elif (rock[0] - 1, rock[1] + 1) not in r:
            rock = (rock[0] - 1, rock[1] + 1)
        elif (rock[0] + 1, rock[1] + 1) not in r:
            rock = (rock[0] + 1, rock[1] + 1)
        else:
            break
    if rock == (500, 0):
        print(t + 1)
        break
    r.add(rock)