Successful submission for day 08!

This commit is contained in:
Ada Werefox 2025-12-08 18:04:45 -08:00
parent 02113adb0e
commit e317f8cb9d
2 changed files with 71 additions and 7 deletions

View file

@ -52,3 +52,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 all in the same circuit. What do you get if you multiply together the X coordinates of the last two junction boxes you need to connect?

View file

@ -2,7 +2,6 @@
from logging import debug, DEBUG, basicConfig
from sys import argv
from math import sqrt
def parse_input(input_filepath: str) -> list[tuple[int, ...]]:
@ -21,11 +20,9 @@ def get_distances(positions: list[tuple[int, ...]]) -> list[tuple[float, int, in
for b in range(a + 1, len(positions)):
distances.append(
(
sqrt(
pow(positions[a][0] - positions[b][0], 2)
+ pow(positions[a][1] - positions[b][1], 2)
+ pow(positions[a][2] - positions[b][2], 2)
),
pow(positions[a][0] - positions[b][0], 2)
+ pow(positions[a][1] - positions[b][1], 2)
+ pow(positions[a][2] - positions[b][2], 2),
a,
b,
)
@ -34,11 +31,70 @@ def get_distances(positions: list[tuple[int, ...]]) -> list[tuple[float, int, in
return distances
def create_circuits(
distances: list[tuple[float, int, int]], n: int
) -> tuple[list[int], tuple[float, int, int]]:
single_circuits: list[int] = [x for x in range(n)]
circuits: list[set[int]] = []
count = 0
circuits_after_n: list[int] = []
for d in distances:
if count == 999:
circuits_after_n = [len(x) for x in circuits]
if len(single_circuits) == 0 and len(circuits) == 1:
break
count += 1
debug(f"Current Iteration: {count} | {d} | {circuits}")
a_in_single_circuits = d[1] in single_circuits
b_in_single_circuits = d[2] in single_circuits
if a_in_single_circuits or b_in_single_circuits:
if a_in_single_circuits and b_in_single_circuits:
circuits.append(set([d[1], d[2]]))
single_circuits.remove(d[1])
single_circuits.remove(d[2])
continue
for c in circuits:
if d[1] in c:
c.add(d[2])
single_circuits.remove(d[2])
break
if d[2] in c:
c.add(d[1])
single_circuits.remove(d[1])
break
continue
circuit_a = circuit_b = set()
for c in circuits:
if d[1] in c:
circuit_a = c
if d[2] in c:
circuit_b = c
if circuit_a == circuit_b:
continue
circuits.remove(circuit_a)
circuits.remove(circuit_b)
circuits.append(circuit_a.union(circuit_b))
return (circuits_after_n, distances[count - 1])
def main() -> None:
input_filepath = "input/test_junction_boxes.txt"
input_filepath = "input/junction_boxes.txt"
junction_boxes = parse_input(input_filepath)
distances = get_distances(junction_boxes)
debug(f"DISTANCES BETWEEN BOXES: {distances}")
circuits_after_n, final_distance = create_circuits(distances, len(junction_boxes))
debug(f"CIRCUITS CREATED AFTER 1000 ITERATIONS: {circuits_after_n}")
debug(f"FINAL DISTANCE EXAMINED: {final_distance[0]} | {junction_boxes[final_distance[1]]} | {junction_boxes[final_distance[2]]}")
circuits_after_n.sort(reverse=True)
product_of_n_largest_circuits = (
circuits_after_n[0] * circuits_after_n[1] * circuits_after_n[2]
)
print(
f"The product of the three largest circuits after 1000 iterations is: {product_of_n_largest_circuits}"
)
print(
f"The product of the x coordinates of the last two junction boxes connected: {junction_boxes[final_distance[1]][0] * junction_boxes[final_distance[2]][0]}"
)
return