-
Notifications
You must be signed in to change notification settings - Fork 0
/
14.py
39 lines (30 loc) · 904 Bytes
/
14.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
from lib import *
input = read_input(2022, 14)
rock = set()
maxy = 0
for line in input.splitlines():
for a, b in sliding_window(line.split(" -> ")):
for x, y in iter_line(*map(int, a.split(",")), *map(int, b.split(","))):
rock.add((x, y))
maxy = max(y, maxy)
def simulate(x, y, air, maxy):
while y <= maxy:
if air(x, y + 1):
y += 1
elif air(x - 1, y + 1):
x -= 1
y += 1
elif air(x + 1, y + 1):
x += 1
y += 1
else:
return x, y
return None
sand = set()
while s := simulate(500, 0, lambda x, y: (x, y) not in rock and (x, y) not in sand, maxy):
sand.add(s)
print(len(sand))
sand = set()
while (500, 0) not in sand:
sand.add(simulate(500, 0, lambda x, y: y < maxy + 2 and (x, y) not in rock and (x, y) not in sand, maxy + 2))
print(len(sand))