-
Notifications
You must be signed in to change notification settings - Fork 0
/
14-1.py
52 lines (44 loc) · 1.14 KB
/
14-1.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
data = []
with open(f"14.txt", "r") as file:
data = file.read().splitlines()
def parsePoints(array):
out = []
for row in array:
out.append([int(e) for e in row.split(",")])
return out
for i, row in enumerate(data):
data[i] = row.split(" -> ")
for i, row in enumerate(data):
data[i] = [(int(x), int(y)) for x, y in [e.split(",") for e in row]]
filled = set()
for row in data:
for i in range(1, len(row)):
px, py = row[i-1]
cx, cy = row[i]
if cx == px:
for y in range(min(py, cy), max(py, cy) + 1):
filled.add((cx, y))
if cy == py:
for x in range(min(px, cx), max(px, cx) + 1):
filled.add((x, cy))
m = max([e[1] for e in filled])
def simulate_sand(m):
x, y = 500, 0
while y < m:
if (x, y+1) not in filled:
y += 1
elif (x-1, y+1) not in filled:
x -= 1
elif (x+1, y+1) not in filled:
x += 1
else:
filled.add((x, y))
return True
return False
ans = 0
while True:
if simulate_sand(m):
ans += 1
else:
break
print(ans)