this post was submitted on 08 Dec 2025
1 points (100.0% liked)
Advent Of Code
1248 readers
1 users here now
An unofficial home for the advent of code community on programming.dev! Other challenges are also welcome!
Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.
Everybody Codes is another collection of programming puzzles with seasonal events.
EC 2025
AoC 2025
Solution Threads
| M | T | W | T | F | S | S |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 | 12 |
Rules/Guidelines
- Follow the programming.dev instance rules
- Keep all content related to advent of code in some way
- If what youre posting relates to a day, put in brackets the year and then day number in front of the post title (e.g. [2024 Day 10])
- When an event is running, keep solutions in the solution megathread to avoid the community getting spammed with posts
Relevant Communities
Relevant Links
Credits
Icon base by Lorc under CC BY 3.0 with modifications to add a gradient
console.log('Hello World')
founded 3 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
Python
Both of my solutions make use of a Heap and a Disjoint-Set-Union / Union-Find data structure
Part 1: Use a heap to keep 1000 shortest connections, then union them all in DSU and get the 3 largest connected components.
Part 2: Use a heap to keep all connections, then union the shortest ones until all components are connected.
click to view code
import heapq as hq from itertools import combinations from collections import CounterDisjoint Set Union (or Union-Find) data structure
with path compression and union by rank
class DSU: def init(self, size: int) -> None: self.parent = list(range(size)) # ith value is the parent node to node i self.rank = [1] * size # max depth of subtree rooted here (used for union by rank)
parse puzzle input into junction coordinates (x, y, z)
def parse_junctions(data: str): return [tuple(map(int, line.split(','))) for line in data.splitlines()]
get distance between two points in 3D space
skipping the sqrt because we only care about relative distances
def get_dist(p1, p2): x1, y1, z1 = p1 x2, y2, z2 = p2 return (x2 - x1) ** 2 + (y2 - y1) ** 2 + (z2 - z1) ** 2
def part1(data: str, max_connections: int = 1000): junctions = parse_junctions(data) n = len(junctions)
def part2(data: str): junctions = parse_junctions(data) n = len(junctions)
sample = """162,817,812 57,618,57 906,360,560 592,479,940 352,342,300 466,668,158 542,29,236 431,825,988 739,650,466 52,470,668 216,146,977 819,987,18 117,168,530 805,96,715 346,949,466 970,615,88 941,993,340 862,61,35 984,92,344 425,690,689""" assert part1(sample, max_connections=10) == 40 assert part2(sample) == 25272
It seems like you forgot the backticks around the code. It's very hard to read this way. Also python comments look like markdown headlines :]
Fixed, thanks!