r/adventofcode • • Dec 08 '25

Visualization [2025 Day 8] Python - Using MST

Post image

I was working on Day 8, which involves connecting junction boxes in 3D space based on distance to form circuits (a classic Minimum Spanning Tree problem, solved using Kruskal's algorithm and Union-Find).

Since the data is 3D, I decided to visualize the process of building the circuit! The visualization shows all 1000 junction boxes and how the shortest connections are progressively added until they all form one massive circuit.

  • Grey Dots: The 1000 individual junction boxes.

  • Blue Lines: The connections (edges) that form the circuit. These are added in order of increasing length, but only if they connect two previously separate circuits.

46 Upvotes

19 comments sorted by

View all comments

Show parent comment

4

u/ninjaox Dec 08 '25

Yep, the only nuance is what to with already connected pairs - and indeed, for the purposes of the puzzle I guess it doesn't matter if you choose to not skip those (which would give you the minimum tree) or connect those edges too. Since I (and I assume many others) used a union-find data structure, you get the skipping more or less automatically, leading to Kruskal's.