-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
22 lines (17 loc) · 690 Bytes
/
main.py
File metadata and controls
22 lines (17 loc) · 690 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from math import prod
from operator import itemgetter
import numpy as np
from scipy.cluster.hierarchy import DisjointSet
from scipy.spatial.distance import pdist, squareform
with open("input.txt") as f:
coords = [[int(i) for i in x.split(",")] for x in f]
n_nodes = len(coords)
distances = sorted(np.ndenumerate(squareform(pdist(coords))), key=itemgetter(1))[n_nodes::2]
clustering = DisjointSet(list(range(n_nodes)))
for idx, ((n1, n2), _) in enumerate(distances):
clustering.merge(n1, n2)
if idx == 1000:
print(prod(sorted([len(c) for c in clustering.subsets()])[-3:]))
if clustering.n_subsets == 1:
print(coords[n1][0] * coords[n2][0])
break