2025(08): solve part 1 (it's not quick)

CPU times: user 3.14 s, sys: 0 ns, total: 3.14 s
Wall time: 3.15 s
This commit is contained in:
2025-12-09 20:10:40 +01:00
parent 76e962969d
commit a3e148f9ba
3 changed files with 1027 additions and 9 deletions

View File

@ -57,3 +57,11 @@ This process continues for a while, and the Elves are concerned that they don't
After making the ten shortest connections, there are 11 circuits: one circuit which contains _5_ junction boxes, one circuit which contains _4_ junction boxes, two circuits which contain _2_ junction boxes each, and seven circuits which each contain a single junction box. Multiplying together the sizes of the three largest circuits (5, 4, and one of the circuits of size 2) produces _`40`_.
Your list contains many junction boxes; connect together the _1000_ pairs of junction boxes which are closest together. Afterward, _what do you get if you multiply together the sizes of the three largest circuits?_
### Part Two
The Elves were right; they _definitely_ don't have enough extension cables. You'll need to keep connecting junction boxes together until they're all in _one large circuit_.
Continuing the above example, the first connection which causes all of the junction boxes to form a single circuit is between the junction boxes at `216,146,977` and `117,168,530`. The Elves need to know how far those junction boxes are from the wall so they can pick the right extension cable; multiplying the X coordinates of those two junction boxes (`216` and `117`) produces _`25272`_.
Continue connecting the closest unconnected pairs of junction boxes together until they're <span title="I strongly recommend making an interactive visualizer for this one; it reminds me a lot of maps from futuristic space games.">all in the same circuit</span>. _What do you get if you multiply together the X coordinates of the last two junction boxes you need to connect?_

1000
2025/08/input Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,5 @@
import numpy as np
from math import prod
debug = False
@ -61,6 +62,7 @@ def get_circuits(junctions, connections):
"""
# circuits = [[x] for x in range(len(junctions))]
circuits = [list(x) for x in connections]
circuits += [[x] for x in range(len(junctions))]
changed = True
while changed:
changed = False
@ -69,19 +71,27 @@ def get_circuits(junctions, connections):
for x in circuits[i]:
if x in circuits[j]:
# NOTE: Elements might appear multiple times per circuit.
circuits[i] += circuits.pop[j]
circuits[i] += circuits[j]
circuits[j] = []
changed = True
return circuits
def product_largest_three_circuits(junctions, num_of_connections):
X, Y, dists = distances(junctions)
connections = []
for i in range(num_of_connections):
x, y = X[i], Y[i]
connections.append((x, y))
circuits = get_circuits(junctions, connections)
lengths = [len(set(circuit)) for circuit in circuits if circuit != []]
lengths.sort()
return prod(lengths[-3:])
if __name__ == "__main__":
test_junctions = load_junctions("testinput")
X, Y, dists = distances(test_junctions)
connections = []
# unconnected = list(range(len(test_junctions)))
assert product_largest_three_circuits(test_junctions, 10) == 40
for i in range(len(X)):
x, y = X[i], Y[i]
if not is_connected(x, connections) and not is_connected(y, connections):
connections.append((x, y))
print(len(connections))
junctions = load_junctions("input")
print(product_largest_three_circuits(junctions, 1000))