From e711bc9bff5dac89841f16931c2e0ee568d78423 Mon Sep 17 00:00:00 2001 From: Kees van Kempen Date: Tue, 9 Dec 2025 20:54:04 +0100 Subject: [PATCH] 2025(08): part 2 tering done --- 2025/08/part2.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 2025/08/part2.py diff --git a/2025/08/part2.py b/2025/08/part2.py new file mode 100644 index 0000000..0db424a --- /dev/null +++ b/2025/08/part2.py @@ -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)) \ No newline at end of file