-
Notifications
You must be signed in to change notification settings - Fork 0
/
part_a.py
executable file
·333 lines (308 loc) · 9.3 KB
/
part_a.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/usr/bin/env python3
import itertools
import utils
class Challenge(utils.BaseChallenge):
def solve(self, _input, debug=False):
"""
>>> Challenge().default_solve()
644640
"""
return Area.from_area_text(_input).step_many(10).get_hash()
class Area:
TREE = 'tree'
CAMP = 'camp'
OPEN = 'open'
PARSE_MAP = {
'.': OPEN,
'|': TREE,
'#': CAMP,
}
@classmethod
def from_area_text(cls, area_text, width=None, height=None):
"""
>>> len(Area.from_area_text(
... ".#.#...|#.\\n"
... ".....#|##|\\n"
... ".|..|...#.\\n"
... "..|#.....#\\n"
... "#.#|||#|#|\\n"
... "...#.||...\\n"
... ".|....|...\\n"
... "||...#|.#|\\n"
... "|.||||..|.\\n"
... "...#.|..|.\\n"
... ).contents)
44
"""
non_empty_lines = list(filter(None, area_text.splitlines()))
if height is None:
height = len(non_empty_lines)
if width is None:
width = len(non_empty_lines[0])
contents = {
(x, y): cls.PARSE_MAP[spot]
for y, line in enumerate(non_empty_lines)
for x, spot in enumerate(line)
if cls.PARSE_MAP[spot] != cls.OPEN
}
return cls(contents, width, height)
def __init__(self, contents, width, height):
self.contents = contents
self.width = width
self.height = height
def get_hash(self):
"""
>>> Area.from_area_text(
... ".||##.....\\n"
... "||###.....\\n"
... "||##......\\n"
... "|##.....##\\n"
... "|##.....##\\n"
... "|##....##|\\n"
... "||##.####|\\n"
... "||#####|||\\n"
... "||||#|||||\\n"
... "||||||||||\\n"
... ).get_hash()
1147
"""
counts = self.get_counts()
return counts['tree'] * counts['camp']
def get_counts(self):
"""
>>> Area.from_area_text(
... ".||##.....\\n"
... "||###.....\\n"
... "||##......\\n"
... "|##.....##\\n"
... "|##.....##\\n"
... "|##....##|\\n"
... "||##.####|\\n"
... "||#####|||\\n"
... "||||#|||||\\n"
... "||||||||||\\n"
... ).get_counts()
{'tree': 37, 'camp': 31}
"""
return self.aggregate_counts(self.contents.values())
SHOW_MAP = {
content: spot
for spot, content in PARSE_MAP.items()
}
def show(self):
"""
>>> print(Area.from_area_text(
... ".#.#...|#.\\n"
... ".....#|##|\\n"
... ".|..|...#.\\n"
... "..|#.....#\\n"
... "#.#|||#|#|\\n"
... "...#.||...\\n"
... ".|....|...\\n"
... "||...#|.#|\\n"
... "|.||||..|.\\n"
... "...#.|..|.\\n"
... ).show())
.#.#...|#.
.....#|##|
.|..|...#.
..|#.....#
#.#|||#|#|
...#.||...
.|....|...
||...#|.#|
|.||||..|.
...#.|..|.
"""
return "\n".join(
"".join(
self.SHOW_MAP[self.contents.get((x, y), self.OPEN)]
for x in range(self.width)
)
for y in range(self.height)
)
def step_many(self, count):
"""
>>> print("!" + Area.from_area_text(
... ".#.#...|#.\\n"
... ".....#|##|\\n"
... ".|..|...#.\\n"
... "..|#.....#\\n"
... "#.#|||#|#|\\n"
... "...#.||...\\n"
... ".|....|...\\n"
... "||...#|.#|\\n"
... "|.||||..|.\\n"
... "...#.|..|.\\n"
... ).step_many(10).show()[1:])
!||##.....
||###.....
||##......
|##.....##
|##.....##
|##....##|
||##.####|
||#####|||
||||#|||||
||||||||||
"""
for _ in range(count):
self.step()
return self
def step(self):
"""
>>> print("!" + Area.from_area_text(
... ".#.#...|#.\\n"
... ".....#|##|\\n"
... ".|..|...#.\\n"
... "..|#.....#\\n"
... "#.#|||#|#|\\n"
... "...#.||...\\n"
... ".|....|...\\n"
... "||...#|.#|\\n"
... "|.||||..|.\\n"
... "...#.|..|.\\n"
... ).step().show()[1:])
!......##.
......|###
.|..|...#.
..|#||...#
..##||.|#|
...#||||..
||...|||..
|||||.||.|
||||||||||
....||..|.
"""
points = set(self.contents) | {
neighbour
for point in self.contents
for neighbour in self.get_neighbours(point)
}
self.contents = {
point: next_spot
for point, next_spot in (
(point, self.get_next_spot_at_point(point))
for point in points
)
if next_spot != self.OPEN
}
return self
def get_next_spot_at_point(self, point):
"""
>>> Area({(0, 0): 'camp'}, 10, 10).get_next_spot_at_point((0, 0))
'open'
>>> Area({(0, 0): 'camp', (0, 1): 'camp', (1, 0): 'tree'}, 10, 10)\\
... .get_next_spot_at_point((0, 0))
'camp'
"""
neighbour_counts = self.get_neighbour_counts(point)
spot = self.contents.get(point, self.OPEN)
return self.get_next_spot(spot, neighbour_counts)
def get_next_spot(self, spot, neighbour_counts):
"""
>>> Area({}, 1, 1).get_next_spot('camp', {'tree': 0, 'camp': 0})
'open'
>>> Area({}, 1, 1).get_next_spot('camp', {'tree': 0, 'camp': 1})
'open'
>>> Area({}, 1, 1).get_next_spot('camp', {'tree': 2, 'camp': 0})
'open'
>>> Area({}, 1, 1).get_next_spot('camp', {'tree': 2, 'camp': 1})
'camp'
>>> Area({}, 1, 1).get_next_spot('tree', {'tree': 0, 'camp': 0})
'tree'
>>> Area({}, 1, 1).get_next_spot('tree', {'tree': 3, 'camp': 0})
'tree'
>>> Area({}, 1, 1).get_next_spot('tree', {'tree': 0, 'camp': 3})
'camp'
>>> Area({}, 1, 1).get_next_spot('tree', {'tree': 3, 'camp': 3})
'camp'
>>> Area({}, 1, 1).get_next_spot('open', {'tree': 0, 'camp': 3})
'open'
>>> Area({}, 1, 1).get_next_spot('open', {'tree': 3, 'camp': 0})
'tree'
>>> Area({}, 1, 1).get_next_spot('open', {'tree': 3, 'camp': 3})
'tree'
"""
if spot == self.TREE:
if neighbour_counts[self.CAMP] >= 3:
return self.CAMP
else:
return self.TREE
elif spot == self.CAMP:
if neighbour_counts[self.TREE] < 1 \
or neighbour_counts[self.CAMP] < 1:
return self.OPEN
else:
return self.CAMP
elif spot == self.OPEN:
if neighbour_counts[self.TREE] >= 3:
return self.TREE
else:
return self.OPEN
raise Exception(f"Invalid spot '{spot}'")
def get_neighbour_counts(self, point):
"""
>>> Area({}, 1, 1).get_neighbour_counts((0, 0))
{'tree': 0, 'camp': 0}
>>> Area({(1, 1): 'tree', (3, 5): 'tree'}, 10, 10)\\
... .get_neighbour_counts((1, 1))
{'tree': 0, 'camp': 0}
>>> Area({
... (2, 2): 'tree', (3, 5): 'tree', (2, 1): 'camp',
... (1, 2): 'camp',
... }, 10, 10).get_neighbour_counts((1, 1))
{'tree': 1, 'camp': 2}
"""
neighbours = self.get_neighbours(point)
return self.aggregate_counts(
self.contents.get(neighbour, self.OPEN)
for neighbour in neighbours
)
def aggregate_counts(self, spots):
"""
>>> Area({}, 1, 1).aggregate_counts([
... 'tree', 'camp', 'tree', 'camp', 'open', 'tree'])
{'tree': 3, 'camp': 2}
"""
return {
self.TREE: 0,
self.CAMP: 0,
**{
spot: len(list(items))
for spot, items in itertools.groupby(sorted(spots))
if spot != self.OPEN
},
}
NEIGHBOUR_OFFSETS = [
(d_x, d_y)
for d_x in range(-1, 2)
for d_y in range(-1, 2)
if (d_x, d_y) != (0, 0)
]
def get_neighbours(self, point):
"""
>>> sorted(Area({}, 10, 10).get_neighbours((2, -3)))
[]
>>> sorted(Area({}, 10, 10).get_neighbours((0, 0)))
[(0, 1), (1, 0), (1, 1)]
>>> sorted(Area({}, 10, 10).get_neighbours((2, 4)))
[(1, 3), (1, 4), (1, 5), (2, 3), (2, 5), (3, 3), (3, 4), (3, 5)]
"""
x, y = point
return (
neighbour
for neighbour in (
(x + d_x, y + d_y)
for d_x, d_y in self.NEIGHBOUR_OFFSETS
)
if self.is_within_area(neighbour)
)
def is_within_area(self, point):
x, y = point
return (
0 <= x < self.width
and 0 <= y < self.height
)
Challenge.main()
challenge = Challenge()