38 lines
1.2 KiB
Python
38 lines
1.2 KiB
Python
import numpy as np
|
|
from part1 import load_junctions, distances, get_circuits
|
|
|
|
debug = False
|
|
|
|
def get_one_big_circuit_and_tering(junctions):
|
|
X, Y, dists = distances(junctions)
|
|
connections = []
|
|
|
|
# Start by connecting at least every junction once
|
|
for i in range(len(junctions)):
|
|
x, y = X[i], Y[i]
|
|
connections.append((x, y))
|
|
|
|
circuits = get_circuits(junctions, connections)
|
|
circuits = [list(set(c)) for c in circuits if c != []]
|
|
|
|
i = len(junctions)
|
|
while len(circuits) > 1:
|
|
x, y = X[i], Y[i]
|
|
idx_circuit_x = next((i for i, sublist in enumerate(circuits) if x in sublist), None)
|
|
idx_circuit_y = next((i for i, sublist in enumerate(circuits) if y in sublist), None)
|
|
if idx_circuit_x != idx_circuit_y:
|
|
# I truly (tering) hate that it pops before it saves. The order matters.
|
|
tering_1, tering_2 = min(idx_circuit_x, idx_circuit_y), max(idx_circuit_x, idx_circuit_y)
|
|
circuits[tering_1] += circuits.pop(tering_2)
|
|
i += 1
|
|
|
|
# Do the weird math
|
|
j1, j2 = junctions[[x, y]]
|
|
return j1[0]*j2[0]
|
|
|
|
if __name__ == "__main__":
|
|
test_junctions = load_junctions("testinput")
|
|
assert get_one_big_circuit_and_tering(test_junctions) == 25272
|
|
|
|
junctions = load_junctions("input")
|
|
print(get_one_big_circuit_and_tering(junctions)) |