-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path25.py
More file actions
36 lines (25 loc) · 878 Bytes
/
25.py
File metadata and controls
36 lines (25 loc) · 878 Bytes
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
# Import data
data = [line.strip() for line in open('25.in', 'r').readlines()]
points = [map(int, line.split(',')) for line in data]
constellations = []
def within_range(p1, p2):
return sum([abs(a - b) for (a, b) in zip(p1, p2)]) <= 3
for p in points:
constellation_found = False
added = -1
to_rm = []
for i in range(len(constellations)):
c = constellations[i]
within_c = len([x for x in c if within_range(p, x)]) > 0
if within_c and added >= 0:
constellations[added] += constellations[i]
to_rm.append(i)
elif within_c:
constellation_found = True
constellations[i].append(p)
added = i
for rm in to_rm:
del constellations[rm]
if not constellation_found:
constellations.append([p])
print('Part 1 solution: %d' % len(constellations))