-
Notifications
You must be signed in to change notification settings - Fork 0
/
day14.py
executable file
·79 lines (61 loc) · 1.59 KB
/
day14.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
# [Day 14: Regolith Reservoir](https://adventofcode.com/2022/day/14)
import re
import sys
from pathlib import Path
filename = ("test.txt" if sys.argv[1] == "-t" else sys.argv[1]) if len(sys.argv) > 1 else "input.txt"
data = Path(filename).read_text()
lines = data.splitlines()
wall = set()
floor = 0
for line in lines:
if not line:
continue
points = list(tuple(map(int, re.match(r"^(\d+),(\d+)$", p).groups())) for p in line.split(" -> "))
for a, b in zip(points, points[1:]):
x1, y1 = a
x2, y2 = b
floor = max(y1, y2, floor)
if x1 == x2:
if y1 > y2:
y1, y2 = y2, y1
for y in range(y1, y2 + 1):
wall.add((x1, y))
elif y1 == y2:
if x1 > x2:
x1, x2 = x2, x1
for x in range(x1, x2 + 1):
wall.add((x, y1))
else:
raise ValueError
floor += 2
def fall(part2):
x, y = 500, 0
while True:
if y + 1 >= floor:
if part2:
break
else:
return False
if (x, y + 1) not in wall:
y += 1
elif (x - 1, y + 1) not in wall:
y += 1
x -= 1
elif (x + 1, y + 1) not in wall:
y += 1
x += 1
else:
break
wall.add((x, y))
if (x, y) == (500, 0):
return False
return True
for i in range(100000):
if not fall(False):
print(i)
break
for j in range(100000):
if not fall(True):
print(i + j + 1)
break