-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday08.py
More file actions
57 lines (47 loc) · 1.67 KB
/
day08.py
File metadata and controls
57 lines (47 loc) · 1.67 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
def main():
lines = open('data/day08.txt', 'r').readlines()
points = []
for line in lines:
points.append(tuple([int(x) for x in line.strip().split(',')]))
dists = {}
for i in range(0, len(points)):
for j in range(i+1, len(points)):
(x1, y1, z1) = points[i]
(x2, y2, z2) = points[j]
dists[(points[i], points[j])] = (x2-x1)**2 + (y2-y1)**2 + (z2-z1)**2
sortedPairs = sorted(dists.keys(), key=lambda p: dists[p])
components = {}
compid = 1
links = 0
while True:
if links == 1000:
comps = {}
for k, v in components.items():
if not v in comps:
comps[v] = []
comps[v].append(k)
res = sorted([len(v) for v in comps.values()])
print(res[-1] * res[-2] * res[-3])
if len(set(components.values())) == 1 and len(components) == len(points):
print(a[0]*b[0])
break
links += 1
(a, b) = sortedPairs.pop(0)
if not a in components and not b in components:
components[a] = compid
components[b] = compid
compid+=1
elif a in components and b in components:
if components[a] == components[b]:
continue
else:
merged = components[b]
tomerge = components[a]
for k,v in components.items():
if v == tomerge:
components[k] = merged
elif a in components:
components[b] = components[a]
elif b in components:
components[a] = components[b]
main()