-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.py
173 lines (133 loc) · 4.1 KB
/
script.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import sys
import itertools
inputs = (line.rstrip('\n') for line in open(sys.argv[1]))
def pairwise(iterable):
it = iter(iterable)
a = next(it, None)
for b in it:
yield (a, b)
a = b
def clamp1(n):
return max(min(n, 1), -1)
class Grid:
def __init__(self, chains):
self.chains = []
for chain in chains:
self.chains.append([])
links = chain.split(' -> ')
for link in links:
x, y = map(int, link.split(','))
self.chains[-1].append((x, y))
bounds = self.find_bounds()
self.left, self.right, self.up, self.down = bounds
self.width = self.right - self.left + 1
self.height = self.down - self.up + 1
self.grid = [[0 for x in range(self.width)]
for y in range(self.height)]
self.build_walls()
self.expands = False
# self.grid[0][500-self.left] = 3
def find_bounds(self):
all_links = list(itertools.chain.from_iterable(self.chains))
all_links.append((500, 0))
left = min(all_links, key=lambda link: link[0])[0]
right = max(all_links, key=lambda link: link[0])[0]
up = min(all_links, key=lambda link: link[1])[1]
down = max(all_links, key=lambda link: link[1])[1]
return left, right, up, down
def build_walls(self):
for chain in self.chains:
self.build_wall(chain)
def build_wall(self, chain):
for start, end in pairwise(chain):
dx = clamp1(end[0] - start[0])
dy = clamp1(end[1] - start[1])
x, y = start
x -= self.left
y -= self.up
while (x+self.left, y+self.up) != (end[0]+dx, end[1]+dy):
self.grid[y][x] = 1
x += dx
y += dy
def char(self, cell):
return {0: '.',
1: '#',
2: 'o',
3: '+'}[cell]
def __repr__(self):
return '\n'.join(''.join(map(self.char, row)) for row in self.grid)
def cell(self, coord):
x, y = coord
if self.expands:
if x < 0:
self.expand_left()
x += 1
elif x >= self.width:
self.expand_right()
if 0 <= x < self.width and 0 <= y < self.height:
return self.grid[y][x]
else:
return -1
def below(self, x, y):
x -= self.left
y -= self.up
search = [(x, y+1), (x-1, y+1), (x+1, y+1)]
search = [self.cell(coord) for coord in search]
return search
def write_sand(self, x, y):
self.grid[y - self.up][x - self.left] = 2
def count_sand(self):
return sum(row.count(2) for row in self.grid)
def clean(self):
self.grid = [[0 if c == 2 else c for c in row] for row in self.grid]
def add_floor(self):
self.grid.append([0 for x in range(self.width)])
self.grid.append([1 for x in range(self.width)])
self.height += 2
def expand_left(self):
for row in self.grid:
row.insert(0, 0)
self.grid[-1][0] = 1
self.width += 1
self.left -= 1
def expand_right(self):
for row in self.grid:
row.append(0)
self.grid[-1][-1] = 1
self.width += 1
self.right += 1
grid = Grid(inputs)
def step(x, y):
for i, d in enumerate(grid.below(x, y)):
if d == 0:
y += 1
x += {0: 0, 1: -1, 2: 1}[i]
return True, x, y
elif d == -1:
return None, x, y
else:
return False, x, y
def simulate():
x, y = 500, 0
falling = True
while falling:
falling, x, y = step(x, y)
if not falling:
grid.write_sand(x, y)
if (x, y) == (500, 0):
return False
x, y = 500, 0
return True
elif falling is None:
return False
while simulate():
pass
# print(grid)
print(grid.count_sand())
grid.clean()
grid.add_floor()
grid.expands = True
while simulate():
pass
# print(grid)
print(grid.count_sand())