45 lines
1.3 KiB
Python
45 lines
1.3 KiB
Python
|
|
with open(r'day8/input.txt', 'r') as input:
|
|
lines = list(map(lambda x: list(map(lambda y: int(y),x.split(','))),input.read().split('\n')[:-1]))
|
|
|
|
def dist(a,b):
|
|
return (a[0]-b[0])**2 + (a[1]-b[1])**2 + (a[2]-b[2])**2
|
|
|
|
def find_best(i):
|
|
best_distances[i] = [10**15,-1]
|
|
for j,node2 in enumerate(lines):
|
|
if idx_circuit[j] == idx_circuit[i]: continue
|
|
distance = dist(lines[i],node2)
|
|
best_distances[i] = min([distance,j], best_distances[i],key=lambda x : x[0])
|
|
|
|
best_distances = []
|
|
|
|
idx_circuit = list(range(len(lines)))
|
|
|
|
for i in range(len(lines)):
|
|
best_distances.append(0)
|
|
find_best(i)
|
|
|
|
pairs_left = 1000
|
|
while pairs_left > 0:
|
|
min_idx = best_distances.index(min(best_distances, key = lambda x:x[0]))
|
|
if (old_idx := idx_circuit[min_idx]) == (new_idx := idx_circuit[best_distances[min_idx][1]]):
|
|
find_best(min_idx)
|
|
continue
|
|
|
|
for i in range(len(lines)):
|
|
if idx_circuit[i] == old_idx:
|
|
idx_circuit[i] = new_idx
|
|
for i in range(len(lines)):
|
|
if idx_circuit[i] == new_idx:
|
|
find_best(i)
|
|
|
|
pairs_left -= 1
|
|
|
|
counts = []
|
|
for circuit in set(idx_circuit):
|
|
count = len(list(filter(lambda x: idx_circuit[x] == circuit,idx_circuit)))
|
|
counts.append(count)
|
|
counts.sort(reverse=True)
|
|
print(counts)
|
|
print("score:",counts[0]*counts[1]*counts[2])
|