-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path17.py
89 lines (79 loc) · 2.67 KB
/
17.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
80
81
82
83
84
85
86
87
88
89
import fileinput
import re
class Model:
def __init__(self, lines):
self.clay = set()
self.still = set()
self.flowing = set()
for line in lines:
a, b0, b1 = map(int, re.findall(r'\d+', line))
for b in range(b0, b1 + 1):
self.clay.add((a, b) if line[0] == 'x' else (b, a))
self.x0 = min(x for x, y in self.clay)
self.x1 = max(x for x, y in self.clay)
self.y0 = min(y for x, y in self.clay)
self.y1 = max(y for x, y in self.clay)
self.queue = []
def run(self, x, y):
self.queue.append((self.fall, x, y))
while self.queue:
func, *args = self.queue.pop()
func(*args)
def count_all(self):
return sum(1 for x, y in self.still | self.flowing
if y >= self.y0 and y <= self.y1)
def count_still(self):
return sum(1 for x, y in self.still
if y >= self.y0 and y <= self.y1)
def stop(self, x, y):
return (x, y) in self.clay
def pile(self, x, y):
return (x, y) in self.clay or (x, y) in self.still
def fall(self, x, y):
while y <= self.y1 and not self.pile(x, y + 1):
self.flowing.add((x, y))
y += 1
if y <= self.y1:
self.flowing.add((x, y))
self.queue.append((self.scan, x, y))
def scan(self, x, y):
x0 = x
while self.pile(x0, y + 1) and not self.stop(x0 - 1, y):
x0 -= 1
x1 = x
while self.pile(x1, y + 1) and not self.stop(x1 + 1, y):
x1 += 1
stop0 = self.stop(x0 - 1, y)
stop1 = self.stop(x1 + 1, y)
if stop0 and stop1:
for i in range(x0, x1 + 1):
self.still.add((i, y))
self.queue.append((self.scan, x, y - 1))
else:
for i in range(x0, x1 + 1):
self.flowing.add((i, y))
if not stop0:
self.queue.append((self.fall, x0, y))
if not stop1:
self.queue.append((self.fall, x1, y))
def __str__(self):
x0, y0, x1, y1 = self.x0, self.y0, self.x1, self.y1
rows = []
for y in range(y0, y1 + 1):
row = []
for x in range(x0, x1 + 1):
c = '.'
if (x, y) in self.clay:
c = '#'
if (x, y) in self.flowing:
c = '|'
if (x, y) in self.still:
c = '~'
row.append(c)
rows.append(''.join(row))
return '\n'.join(rows)
model = Model(fileinput.input())
model.run(500, 0)
print(model)
print(model.count_all())
print(model.count_still())