-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patht17.py
More file actions
153 lines (119 loc) · 4.43 KB
/
t17.py
File metadata and controls
153 lines (119 loc) · 4.43 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!python3
import sys
from attr import attrs, attrib
@attrs
class Water_runner:
field = attrib(default=None)
work_q = attrib(default=None)
min_x = attrib(default=None)
min_y = attrib(default=None)
max_x = attrib(default=None)
max_y = attrib(default=None)
def print_field(self, what):
for x in range(self.min_x, self.max_x + 2):
row = ''
for y in range(self.min_y, self.max_y + 2):
row += str(what.get((x, y), ' '))
print(row)
return 0
def check_still(self, x, y):
if self.field[x, y] == "|":
mod_water = dict()
ok_water = 1
pos_y = y
# LEFT
while ok_water:
if self.field.get((x, pos_y), '.') == '.':
return 1
if self.field.get((x, pos_y), '.') == '#':
break
mod_water[x, pos_y] = '~'
pos_y -= 1
pos_y = y
# RIGHT
while ok_water:
if self.field.get((x, pos_y), '.') == '.':
return 1
if self.field.get((x, pos_y), '.') == '#':
break
mod_water[x, pos_y] = '~'
pos_y += 1
if ok_water:
for a, b in mod_water:
self.field[a, b] = '~'
if self.work_q.get((a, b), '') != '':
del self.work_q[a, b]
if self.field.get((a-1, b), '') == '|':
self.work_q[a-1, b] = 1
return 1
def load_field(self, inp):
self.max_x = self.max_y = 0
self.min_x = self.min_y = 99999
self.field = dict()
for row in inp:
first, second = row.split(', ')
f1, f2 = first.split('=')
s1, s2 = second.split('=')
f2 = int(f2)
sfr, sto = list(map(int, s2.split('..')))
if f1 == 'y':
for i in range(sfr, sto+1):
self.field[f2, i] = '#'
else:
for i in range(int(sfr), sto+1):
self.field[i, f2] = '#'
self.min_x = min([square[0] for square in self.field])
self.max_x = max([square[0] for square in self.field])
self.min_y = min([square[1] for square in self.field])
self.max_y = max([square[1] for square in self.field])
return 1
def run_round(self):
for x, y in list(self.work_q):
if self.work_q.get((x, y), '') == '':
continue
if x >= self.max_x:
del self.work_q[x, y]
continue
what_below = str(self.field.get((x+1, y), '.'))
# drop down
if what_below == '.':
self.field[x+1, y] = '|'
self.work_q[x+1, y] = 1
if self.work_q.get((x, y), '') != '':
del self.work_q[x, y]
continue
if what_below == '#' or what_below == "~":
# left
if self.field.get((x, y-1), '.') == '.':
self.field[x, y-1] = '|'
self.work_q[x, y-1] = 1
# right
elif self.field.get((x, y+1), '.') == '.':
self.field[x, y+1] = '|'
self.work_q[x, y+1] = 1
if self.field.get((x, y-1), '') in ['|', '#'] and self.field.get((x, y+1), '') in ['|', '#']:
del self.work_q[x, y]
# check if the row is still
self.check_still(x, y)
return 1
def main(self):
assert len(sys.argv) == 2
inp = open(sys.argv[1]).read().strip().split('\n')
self.load_field(inp)
self.field[0, 500] = '|'
self.work_q = dict()
self.work_q[0, 500] = '|'
i = 0
while len(self.work_q) != 0:
i += 1
prev_q = list(self.work_q)
self.run_round()
after_q = list(self.work_q)
if prev_q == after_q:
break
cur_run = len([square for square in self.field if square[0] <= self.max_x and square[0] >= self.min_x and self.field[square] in ['|', '~']])
curr_still = len([square for square in self.field if self.field[square] == '~'])
print("water : ", cur_run, 'still ', curr_still)
if __name__ == '__main__':
wr = Water_runner()
wr.main()