-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart_a.py
executable file
·198 lines (181 loc) · 5.78 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
#!/usr/bin/env python3
import functools
import utils
class Challenge(utils.BaseChallenge):
def solve(self, _input, debug=False):
"""
>>> Challenge().default_solve()
390
"""
return len(PointList.from_points_text(_input).get_constellations())
class PointList:
@classmethod
def from_points_text(cls, points_text):
"""
>>> point_list_a = PointList.from_points_text(
... " 0,0,0,0\\n"
... " 3,0,0,0\\n"
... " 0,3,0,0\\n"
... " 0,0,3,0\\n"
... " 0,0,0,3\\n"
... " 0,0,0,6\\n"
... " 9,0,0,0\\n"
... "12,0,0,0\\n"
... )
>>> len(point_list_a.points)
8
>>> point_list_a.points[-1]
Point(x=12, y=0, z=0, t=0)
"""
non_empty_lines = filter(None, points_text.splitlines())
return cls(list(map(Point.from_comma_delimited_text, non_empty_lines)))
def __init__(self, points):
"""
>>> len(PointList.from_points_text(
... "1,-1,-1,-2\\n"
... "-2,-2,0,1\\n"
... "0,2,1,3\\n"
... "-2,3,-2,1\\n"
... "0,2,3,-2\\n"
... "-1,-1,1,-2\\n"
... "0,-2,-1,0\\n"
... "-2,2,3,-1\\n"
... "1,2,2,0\\n"
... "-1,-2,0,-2\\n"
... ).get_constellations())
8
"""
self.points = points
def get_constellations(self):
"""
>>> point_list_a = PointList.from_points_text(
... " 0,0,0,0\\n"
... " 3,0,0,0\\n"
... " 0,3,0,0\\n"
... " 0,0,3,0\\n"
... " 0,0,0,3\\n"
... " 0,0,0,6\\n"
... " 9,0,0,0\\n"
... "12,0,0,0\\n"
... )
>>> constellations_a = point_list_a.get_constellations()
>>> tuple(
... len(_constellation.points)
... for _constellation in constellations_a
... )
(6, 2)
>>> Point(x=12, y=0, z=0, t=0) in constellations_a[1].points
True
>>> point_list_b = PointList.from_points_text(
... " 0,0,0,0\\n"
... " 3,0,0,0\\n"
... " 0,3,0,0\\n"
... " 0,0,3,0\\n"
... " 0,0,0,3\\n"
... " 0,0,0,6\\n"
... " 9,0,0,0\\n"
... "12,0,0,0\\n"
... "6,0,0,0\\n"
... )
>>> constellations_b = point_list_b.get_constellations()
>>> tuple(
... len(_constellation.points)
... for _constellation in constellations_b
... )
(9,)
>>> len(PointList.from_points_text(
... "-1,2,2,0\\n"
... "0,0,2,-2\\n"
... "0,0,0,-2\\n"
... "-1,2,0,0\\n"
... "-2,-2,-2,2\\n"
... "3,0,2,-1\\n"
... "-1,3,2,2\\n"
... "-1,0,-1,0\\n"
... "0,2,1,-2\\n"
... "3,0,0,0\\n"
... ).get_constellations())
4
>>> len(PointList.from_points_text(
... "1,-1,0,1\\n"
... "2,0,-1,0\\n"
... "3,2,-1,0\\n"
... "0,0,3,1\\n"
... "0,0,-1,-1\\n"
... "2,3,-2,0\\n"
... "-2,2,0,0\\n"
... "2,-2,0,-1\\n"
... "1,-1,0,-1\\n"
... "3,2,0,2\\n"
... ).get_constellations())
3
>>> len(PointList.from_points_text(
... "1,-1,-1,-2\\n"
... "-2,-2,0,1\\n"
... "0,2,1,3\\n"
... "-2,3,-2,1\\n"
... "0,2,3,-2\\n"
... "-1,-1,1,-2\\n"
... "0,-2,-1,0\\n"
... "-2,2,3,-1\\n"
... "1,2,2,0\\n"
... "-1,-2,0,-2\\n"
... ).get_constellations())
8
"""
constellations = []
for point in self.points:
matching_constellations = [
constellation
for constellation in constellations
if constellation.can_add(point)
]
if not matching_constellations:
constellation = Constellation((point,))
constellations.append(constellation)
elif len(matching_constellations) == 1:
constellation, = matching_constellations
constellation.add(point)
else:
first_constellation, *rest = matching_constellations
first_constellation.add(point)
for other in rest:
first_constellation.absorb(other)
constellations.remove(other)
return constellations
class Constellation:
def __init__(self, points=None):
if points:
self.points = set(points)
else:
self.points = set()
self.neighbourhood = functools.reduce(set.__or__, (
set(point.get_stellar_neighbourhood())
for point in self.points
), set())
def can_add(self, point):
"""
>>> Constellation().can_add(Point(0, 0, 0, 0))
True
>>> Constellation((Point(0, 0, 0, 0),)).can_add(Point(0, 0, 0, 0))
True
"""
return (not self.points) or (point in self.neighbourhood)
def add(self, point):
self.points.add(point)
self.neighbourhood.update(point.get_stellar_neighbourhood())
def absorb(self, other):
self.points.update(other.points)
self.neighbourhood.update(other.neighbourhood)
class Point(utils.Point4D):
def get_stellar_neighbourhood(self):
"""
>>> Point(0, 0, 0, 0) in {Point(0, 0, 0, 0)}
True
>>> Point(0, 0, 0, 0) \\
... in set(Point(0, 0, 0, 0).get_stellar_neighbourhood())
True
"""
return self.get_manhattan_neighbourhood(3)
Challenge.main()
challenge = Challenge()