2025(08): part 2 tering done
This commit is contained in:
38
2025/08/part2.py
Normal file
38
2025/08/part2.py
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
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))
|
||||||
Reference in New Issue
Block a user