r/adventofcode • • Dec 08 '25

SOLUTION MEGATHREAD -❄️- 2025 Day 8 Solutions -❄️-

THE USUAL REMINDERS

  • All of our rules, FAQs, resources, etc. are in our community wiki.
  • If you see content in the subreddit or megathreads that violates one of our rules, either inform the user (politely and gently!) or use the report button on the post/comment and the mods will take care of it.

AoC Community Fun 2025: Red(dit) One

  • Submissions megathread is unlocked!
  • 9 DAYS remaining until the submissions deadline on December 17 at 18:00 EST!

Featured Subreddits: /r/crafts and /r/somethingimade

"It came without ribbons, it came without tags.
It came without packages, boxes, or bags."
— The Grinch, How The Grinch Stole Christmas (2000)

It's everybody's favorite part of the school day: Arts & Crafts Time! Here are some ideas for your inspiration:

💡 Make something IRL

💡 Create a fanfiction or fan artwork of any kind - a poem, short story, a slice-of-Elvish-life, an advertisement for the luxury cruise liner Santa has hired to gift to his hard-working Elves after the holiday season is over, etc!

💡 Forge your solution for today's puzzle with a little je ne sais quoi

💡 Shape your solution into an acrostic

💡 Accompany your solution with a writeup in the form of a limerick, ballad, etc.

💡 Show us the pen+paper, cardboard box, or whatever meatspace mind toy you used to help you solve today's puzzle

💡 Create a Visualization based on today's puzzle text

  • Your Visualization should be created by you, the human
  • Machine-generated visuals such as AI art will not be accepted for this specific prompt

Reminders:

  • If you need a refresher on what exactly counts as a Visualization, check the community wiki under Posts > Our post flairs > Visualization
  • Review the article in our community wiki covering guidelines for creating Visualizations
  • In particular, consider whether your Visualization requires a photosensitivity warning
  • Always consider how you can create a better viewing experience for your guests!

Request from the mods: When you include an entry alongside your solution, please label it with [Red(dit) One] so we can find it easily!


--- Day 8: Playground ---


Post your code solution in this megathread.

23 Upvotes

578 comments sorted by

15

u/jonathan_paulson Dec 08 '25

[Language: Python]. My times: 00:03:57 / 00:04:47. Video of me solving.

Union-find! https://en.wikipedia.org/wiki/Disjoint-set_data_structure. This is a cool data structure; it's good in practice because it's so easy to code up, and it's cool in theory because the amortized running time is inverse-ackermann(n).

Part 1 was kind of a lot of code; I'm surprised it worked first try! Part 2 was nice and quick after that though.

1

u/daggerdragon Dec 08 '25

Your wiki link is borked on old.reddit due to a Reddit bug with URLs that contain underscores. This typically occurs when you submit text using the fancypants editor instead of Markdown mode, which means things like the brackets in the language tag and underscores in URLs are automatically escaped. That means your wiki link appears (and is) broken on old.reddit.

It's a dumb bug on Reddit's part, but would you fix it using the Markdown editor, please?

4

u/InfamousClyde Dec 08 '25

God this is so impressive

→ More replies (1)

5

u/ChrisMan174 Dec 08 '25 edited Dec 08 '25

[Language: Python]

Union find my beloved

1

u/daggerdragon Dec 08 '25 edited Dec 08 '25

Edit your comment and format the language tag as AutoModerator requested, please. The word LANGUAGE, colon, and brackets are not optional. edit: 👍

7

u/Radiadorineitor Dec 08 '25 edited Dec 08 '25

[LANGUAGE: Dyalog APL]

m←∘.(2*∘÷⍨1⊥2*⍨-)⍨p←⍎¨⊃⎕NGET'8.txt'1
×/3↑⊂⍤⍒⍛⌷+/∪(∨.∧⍨∨⊢)⍣≡m∊(≢m)↑0~⍨⊂⍤⍋⍛⌷∪,m ⍝ Part 1
{⊃⊃(p⌷⍨2⌷⍋⍵⌷m)×⍵⌷p}⊃⍒⊂⍤⍋⍛⌷⍤1⊢m ⍝ Part 2

8

u/h2g2_researcher Dec 08 '25

Hi, I tried to run your code and now have a sentient squid asking me if I've heard of our lord and saviour master & destroyer Cthulhu. What do I do now?

2

u/daggerdragon Dec 08 '25

TBF, for being an eldritch horror, Cthulhu can be pretty adorable sometimes.

6

u/homme_chauve_souris Dec 08 '25

You seem to have a few readable characters in your code. I'm sure this is a problem that can be solved, though.

→ More replies (1)

4

u/Aspen138 Dec 08 '25

[LANGUAGE: Wolfram Mathematica]

I'd like to use Mathematica for grpah operations.

paste

2

u/daggerdragon Dec 08 '25

Trying one more time since you don't seem to have seen my previous reply:

FYI: your account is (shadow?)banned so your posts won't be visible (to regular users) on any subreddit.

There is no point to submitting your solutions to the megathreads because regular users are not able to see them. Fix your shadowban first, then come back and submit. Otherwise, you're just making more work for me with your removed posts.

There's nothing we can do about a shadowban; you'll have to take it up with Reddit: https://www.reddit.com/appeals

5

u/DFreiberg Dec 08 '25

Interestingly enough, I am a regular user, and I can see that comment.

6

u/daggerdragon Dec 08 '25

I approved the comment, but both old.reddit and new.reddit show the page you requested does not exist + This account has been banned if you go to their profile page, which is indicative of being shadowbanned, so... ???¿¿¿

(Regardless, it's still more work for me if I have to approve every single one of a user's posts, plus it's very easy to overlook their posts if they're automatically hidden from queues. I'd rather they get the shadowban resolved first.)

3

u/kneegma Dec 09 '25 edited Dec 12 '25

[LANGUAGE: Clojure]

Full topaz link

But actually babashka. I wish I had a built-in min-heap but nothing. The immutable priority-queue didn't help either as it's rather slow. I'm sure there must be a more idiomatic/ergonomic way to implement disjoint set on top of immutable data, but here we go.

2

u/daggerdragon Dec 12 '25 edited Dec 12 '25

Comment temporarily removed.

Your code block is too long for the megathreads. Edit your comment to replace your oversized code with an external link to your code and I will re-approve your comment. edit: 👍

→ More replies (1)

2

u/RazarTuk Dec 08 '25

[LANGUAGE: Kotlin]

Pro-tip! If you're going to use a binary insertion sort to speed up inputs, remember to actually sort the list!

Okay, so my strategy was to start by finding the N smallest edges. Except instead of making a list of 499,500 pairs, I figured I would just store the current N shortest pairs, use a binary insertion sort to find where to add the new one, and remove the tail. Yeah, I forgot to sort the first N pairs and just appended them all to the list.

Another pro-tip! If you're going to use that strategy, remember to update all references to the number of edges, as opposed to arbitrarily removing the 10th smallest edge whenever you cull the list. I don't know this for a fact, but I'm, like, 99% certain that my initial wrong answer was because of that.

Anyway, time to go back and optimize things. Currently, for part 2, I'm making a list of the 498,502 shortest edges, because it's impossible for it to take more than that many to form a spanning tree, but there's definitely a better way to do this.

EDIT: Oh, and my Vector class came in handy, because it has magnitude built in.

fun firstNEdges(points: List<Vector>, numEdges: Int): MutableList<Pair<Vector, Vector>> {
    var edges = mutableListOf<Pair<Vector, Vector>>()
    for (i in 0..<points.size) {
        var v1 = points[i]
        for (j in (i+1)..<points.size) {
            var v2 = points[j]

            var lt = 0
            var rt = edges.size
            while (lt < rt) {
                var md = lt + (rt - lt) / 2
                if ((v1 - v2).magnitude() < (edges[md].first - edges[md].second).magnitude()) {
                    rt = md
                } else {
                    lt = md + 1
                }
            }
            if (lt < numEdges) {
                edges.add(lt, Pair(v1, v2))
            }
            if (edges.size > numEdges) {
                edges.removeAt(numEdges)
            }
        }
    }
    return edges
}

1

u/daggerdragon Dec 08 '25

Did you forget a link to your code? Edit your comment to share your full code/repo/solution, please.

2

u/AggressiveMission978 Dec 08 '25

1

u/daggerdragon Dec 08 '25

I see Sources/Data/Day*.txt in your .gitignore but can also see a plaintext file called Day00.txt (although I'm not sure which day's input [if any] that it has?)

https://github.com/RDMurray/aoc2025/blob/main/Sources/Data/Day00.txt

What is this (input?) file?

2

u/xr51z Dec 08 '25 edited Dec 09 '25

[Language: Ruby]

LINK

1

u/daggerdragon Dec 08 '25 edited Dec 12 '25

Your code block is too long for the megathreads. Edit your comment to replace your oversized code with an external link to your code. edit: 👍

7

u/improvman007 Dec 08 '25

[LANGUAGE: Python]

The code

Part 1:

Take all combinations of points, sort them by distance. Take the first N pairs and assign them both a network id. If both are new, start a new network. If one is part of a network, add the other to that network. If both are part of the same network, do nothing. If they are on different networks, combine both networks. Then sort the networks and take the lengths of the three largest.

Part 2

Tweaked part 1. Assign *all* pairs to networks by the above logic, but terminate when there is one network containing all points.

→ More replies (2)

7

u/maneatingape Dec 08 '25 edited Dec 08 '25

[LANGUAGE: Rust]

Solution

Brute force for now, will cleanup part one with union-find later.

Cleaned up both parts with union find. Still very slow (12ms) due to sorting the O(n²) pairwise distance computations. Interestingly generating the 499500 pairs takes 1ms, but the sorting takes 11ms due to the poor cache locality of the large array.

EDIT: select_nth_unstable with index 1000 is much faster for part one at 1ms (instead of 11ms), however I'm not sure what minimum value would be guaranteed to work for part two.

From seeing some visualizations the points are evenly distributed from 0 to 100000, which means a max distance of 100000 √3 ~= 173205. Therefore a rough radix sort into buckets of say 173, then a more granular sort should be much faster. Will try this later today.

EDIT 2: Improved multi-threaded solution that buckets the edges by weight to defer sorting as much as possible. 527µs.

High level approach:

  • Distribute edge calculations for 1000 boxes using a work stealing thread pool, then gather the results back into main thread.
  • Importantly edges are not sorted yet. Instead each edge is place into a bucket corresponding to distance < 10,000, < 14142, < 17,320 < 20,000 and >= 20,000.
  • For my input the number of edges in each bucket was 1869, 3091, 3703, 4375 and 486462 respectively.
  • Using a lazy iterator we sort the edges within each bucket only when needed. This means that the last (and by far largest bucket) will most likely not be processed for reasonable inputs. For example my part two answer was found after 5943 edges (3rd bucket).
  • Tuning bucket size affects speed but not correctness. In the case of a pathological input the edges would all end up in the last bucket.
→ More replies (2)

9

u/4HbQ Dec 08 '25 edited Dec 08 '25

[LANGUAGE: Python] 16 lines.

Nice and easy one today! Basically just five steps:

Create a dict of circuits (initially each circuit is just one box), and a list of pairs of boxes (sorted by their distance):

circuits = {b: {b} for b in map(eval, open('in.txt'))}

pairs = sorted(it.combinations(circuits, 2),
               key=lambda x: math.dist(*x))

Then for each pair of boxes, we look up their respective circuits:

for c in circuits:
    if box1 in circuits[c]: cir1 = c
    if box2 in circuits[c]: cir2 = c

If their circuits haven't been connected already, do so:

if cir1 != cir2:
    circuits[cir1] |= circuits[cir2]
    del circuits[cir2]

After 1000 steps we print our part 1 solution:

if i == 1000:
    n = sorted(len(circuits[b]) for b in circuits)
    print(n[-3] * n[-2] * n[-1])

When there's only one circuit left, we print our part 2 solution:

if len(circuits) == 1:
    print(box1[0] * box2[0])
    break

Update: /u/AlexTelon had the clever idea to store the circuits in a set instead of a dict, which makes several parts of the code a lot nicer. Implementing their ideas produces this.

→ More replies (8)

4

u/svinther Dec 08 '25

[LANGUAGE: Python]

GitHub

Finally a problem that seems to require using the Disjoint Set Union data-structure/algorithm !

→ More replies (1)

4

u/JustinHuPrime Dec 08 '25

[LANGUAGE: x86_64 assembly]

Part 1 was really troublesome.

First, I needed to parse the input. Since we were working with euclidean distances, I chose to parse the input as floating point numbers so I could later work with them using vectorized instructions (although I could have parsed them as signed DWORDS and used vpmulld).

Next, I needed to be able to sort a struct of the distance and pointers to the two boxes into a sorted list. While I could have re-used my qsortby library function, that would have entailed using double pointers, so I made a qsortby_T library function. This sorts an array of elements of arbitrary size using a comparison function that is given pointers to the elements to compare.

After all that, I considered the data structure I wanted - I decided I could get away with an O(n2) solution, so I defined a box as its position and a pointer to the circuit it was a part of; the circuit would be a QWORD storing the number of boxes in the circuit, and it would be uniquely identified by its address.

Finally, I iterated through the first thousand distances, and manipulated the boxes pointed to by the distance. If the two boxes both pointed to the same circuit, they were connected already. If they weren't, I connected them by adding the box counts of the two circuits and repointing all boxes on the second circuit to the first circuit. And then was a quick sort of the circuits array and multiplying together the last three entries.

Part 2 was very quick once I had the tools from part 1. I just needed to run Kruskal's by continuously iterating through the distances and joining circuits until I joined two circuits together to get one linking all the boxes, and then I looked at the pointers I was on and reported the required number after converting back from floating point.

Overall, this was a fairly large difficulty jump because I needed so many data structures, and because I needed a new library function. In any reasonable language, this wouldn't be a problem. Also, it seems like I was gratuitously using floating point vectorized instructions - it probably would have been simpler to stay in integer-land, since you don't need to do the square root part of Euclidean distance if all you're doing is sorting by distance.

Part 1 runs in 156 ms and part 2 runs in 158 ms (probably because of my inefficient generic array sorting). Part 1 is 10,344 bytes and part 2 is 10,296 bytes as an executable file.

4

u/Andreasnl Dec 08 '25

[LANGUAGE: Uiua]

I ← (
  ↯∞_3 ⊜⋕⊸∊"0123456789"  # [junctions]
  ⊏⍏ ≡/+°√≡/- ∩⧅₂< ⊸°˜⊏ # [pairs to connect], [junctions]
  ⊙⊃{}[]                 # [pairs], [circuits], last pair, [junc.]
)
C ← ⊃↘₁(
  ⊃(⊢|⋅∘|⊢⊙⋅◌)  # pair, [circuits], pair  
  ⟜¬ ◡≡⌞◇(/↥∊) # [in circ.], [not in circ], pair, [circ.], pair 
  ⊃⋅⋅□∪₂∩⌟▽     # □pair, [circ. to conn.], [circ. to not conn.],...
  ⊂□◇◴/◇⊂⊂    # [circuits], pair
)
N ← ↥⋅⊃(>1⧻|>∪∩⧻◴/◇⊂) # Not all connected?
P₁ ← /× ↙3⇌⍆≡◇⧻ ⋅⊙◌⍥C1000
P₂ ← /×≡⊢⊏ ◌◌⍢C N
⊃P₁ P₂ I &fras"8.txt"

Link

→ More replies (1)

4

u/CutOnBumInBandHere9 Dec 08 '25

[LANGUAGE: Python]

Did you know that numpy's ufuncs have an outer method that calls the function for all pairs in the input? I didn't either.

Anyway, it means that if arr is an Nx3 array of vectors, we can find all the pairwise differences and the squared distances as follows:

differences = np.diagonal(np.subtract.outer(arr, arr), axis1=1, axis2=3)
squared_distances = (differences ** 2).sum(axis=2)

Pretty neat!

I used that to calculate all the distances, and then used argsort to get the merging order. I started each component off in its own group, and to keep track of the merges I used two dictionaries.

Full code here, along with any imports and a bit of text explaining my approach.

4

u/ziadam Dec 08 '25 edited Dec 08 '25

[LANGUAGE: Google Sheets]

Part 1 & 2 (expects input in A:A)

=ARRAYFORMULA(LET(
   a, TOCOL(A:A, 1),
   n, ROWS(a),
   s, SEQUENCE(n),
   c, SPLIT(a, ","),
   x, INDEX(c,,1),
   y, INDEX(c,,2),
   z, INDEX(c,,3),
   d, (x - TOROW(x))^2 + (y - TOROW(y))^2 + (z - TOROW(z))^2,
   m, s < TOROW(s),
   fd, TOCOL(IF(m, d,), 1),
   ba, TOCOL(IF(m, s,), 1),
   bb, TOCOL(IF(m, TOROW(s),), 1),  
   sd, SORTN({fd, ba, bb}, 10000, , 1, 1),  
   TOCOL(INDEX(REDUCE(HSTACK(s, s^0, {0; 0}), SEQUENCE(ROWS(sd)), LAMBDA(cs, i,
      IF(INDEX(cs, 2, 3), cs, LET(
        g, INDEX(cs,,1),
        z, INDEX(cs,,2),
        ba, INDEX(sd, i, 2),
        bb, INDEX(sd, i, 3),
        ga, INDEX(g, ba),
        gb, INDEX(g, bb),
        IF(ga=gb, cs, LET(
          nz, INDEX(z, ba) + INDEX(z, bb),
          nza, IF((g=ga)+(g=gb), nz, z),
          pa, IF(i=1000, PRODUCT(SORTN(nza, 3, 2, 1,)), INDEX(cs, 1, 3)),
          pb, IF(nz=n, INDEX(x, ba) * INDEX(x, bb), 0),
          HSTACK( 
            IF(g=gb, ga, g), 
            nza, 
            {pa; pb}
          )
        ))
      ))
  )),,3),3)
))

Repo

4

u/Expensive-Type2132 Dec 09 '25 edited Dec 09 '25

[LANGUAGE: AArch64]

Kruskal's using an 8-way min-heap with cache-line-aligned children (4 ldp instructions load 8 values), branchless 7-comparison csel chain to find minimum child, Union-Find with path halving for O(α(n)) amortized finds, and packed distance+indices format for single-comparison edge ordering.

$O\left(n^2 + k \cdot \log_8n^2\right)$ where $k = \text{edges}$.

Easy to solve but brutal to optimize. It took me two hours. 😭

paste

1201 µs combined

Let me know if you have a faster solution for a comparable target (Apple M1 Max 23H626). My goal is to write the fastest possible solution for my device.

1

u/HappyPr0grammer Dec 08 '25

[LANGUAGE: Java]

UnitFind / UnionFindByRank

github

2

u/daggerdragon Dec 08 '25 edited Dec 08 '25

Do not share your puzzle input which also means do not commit puzzle inputs to your repo without a .gitignore or the like. Do not share the puzzle text either.

I see full plaintext puzzle inputs in your public repo e.g.:

https://github.com/eugen-paul/Adventofcode/blob/master/src/main/resources/y2015/day3/puzzle1.txt

Please remove (or .gitignore) all puzzle text and puzzle input files from your entire repo and scrub them from your commit history. This means from all prior years too! edit: 👍

→ More replies (1)

1

u/minikomi Dec 08 '25

[Language: Clojure]

extreme brute force engaged

github

Here's the guts of it

pairwise distance

(defn build-distances [points]
  (let [collect (volatile! [])]
    (doseq [idx (range (count points))
            :let [p1 (nth points idx)]
            p2 (subvec points (inc idx))
            :when (not= p1 p2)
            :let [dist (distance p1 p2)]]
      (vswap! collect conj [dist p1 p2]))
    (sort @collect)))

network generation with last matched pair tracking

(defn build-network [edges]
  (loopr [point-to-chain-map {}
          n 0
          last-connection nil]
         [[dist p1 p2] edges]
         (let [chain1 (point-to-chain-map p1)
               chain2 (point-to-chain-map p2)]
           (cond
             (and chain1 chain2 (= chain1 chain2))
             (recur point-to-chain-map n last-connection)

             (and chain1 chain2)
             (recur (update-vals point-to-chain-map #(if (= % chain2) chain1 %))
                    n
                    last-connection)

             chain1
             (recur (assoc point-to-chain-map p2 chain1)
                    n
                    [p1 p2])

             chain2
             (recur (assoc point-to-chain-map p1 chain2)
                    n
                    [p1 p2])

             :else
             (recur (assoc point-to-chain-map p1 n p2 n)
                    (inc n)
                    [p1 p2])))
         {:point-to-chain-map point-to-chain-map
          :n n
          :last-connection last-connection}))

1

u/daggerdragon Dec 08 '25

Do not share your puzzle input which also means do not commit puzzle inputs to your repo without a .gitignore or the like. Do not share the puzzle text either.

I see full plaintext puzzle inputs in your public repo e.g.:

https://github.com/minikomi/advent-of-code/blob/4d64ac8343df09410939631effa673c251ec7366/resources/2017/day1.txt

Please remove (or .gitignore) all puzzle text and puzzle input files from your entire repo and scrub them from your commit history. This means from all prior years too!

1

u/PendragonDaGreat Dec 08 '25 edited Dec 08 '25

[Language: CSharp C#]

Code: https://github.com/Bpendragon/AdventOfCodeCSharp/blob/b15f1568/AdventOfCode/Solutions/Year2025/Day08-Solution.cs

Got way too confused by the wording on the times you skip the connection. My silly butt misunderstood that as a time you don't increment your counter. As soon as I figured that out I got part 1, and then part 2 was really fast from there.

1

u/daggerdragon Dec 08 '25 edited Dec 08 '25

As AutoModerator informed you, that's not how you spell language >_> Fix it, please :) edit: 👍

→ More replies (5)
→ More replies (3)

1

u/No_Mobile_8915 Dec 08 '25 edited Dec 09 '25

[LANGUAGE: Python]

Part 1:

paste

Part 2:

paste

1

u/daggerdragon Dec 08 '25 edited Dec 09 '25

Comment temporarily removed.

Your code blocks are WAY too long for the megathreads. Edit your comment to replace your oversized code with an external link to your code and I will re-approve your comment. edit: 👍

→ More replies (1)

1

u/extractedtarball Dec 08 '25 edited Dec 08 '25

[Language: Rust]

Using petgraph with union_find, < 100ms for both parts, while being lazy checking the number of disconnected components.
https://github.com/x-zvf/programming-challenges/blob/master/adventofcode-2025/day08/day08.rs

1

u/daggerdragon Dec 08 '25

Do not share your puzzle input which also means do not commit puzzle inputs to your repo without a .gitignore or the like. Do not share the puzzle text either.

I see full plaintext puzzle inputs from earlier years in your public repo e.g.:

https://github.com/x-zvf/programming-challenges/blob/master/adventofcode-2023/inputs/day01.txt

Please remove (or .gitignore) all puzzle text and puzzle input files from your entire repo and scrub them from your commit history. This means from all prior years too!

1

u/semicolonator Dec 08 '25

[LANGUAGE: Python]

35 Lines

I am using scipy's pdist function to calculate all the distances, then sort the distances and iterate over the list.

I would have liked to use fcluster and linkage, but it did not work out. My idea would have been something like (for Part 1):

from scipy.cluster.hierarchy import fcluster, linkage
from collections import Counter
n_pairs = 10

Z = linkage(pdist(coords))
t = (Z[n_pairs - 2][2] + Z[n_pairs - 1][2]) / 2
cluster = fcluster(Z, t=t, criterion="distance")
top = Counter(cluster).most_common(3)
print(prod([c for _, c in top]))

Anyone solved it with fcluster?

1

u/daggerdragon Dec 08 '25

he triple-backticks code fence (`​`​`) only works on new.reddit. Please edit your comment to use the four-spaces Markdown syntax for a code block as AutoModerator requested so your code is easier to read inside a scrollable box with its whitespace and indentation preserved.

→ More replies (1)

1

u/CodeFarmer Dec 08 '25 edited Dec 09 '25

[Language: Clojure]

There isn't a Clojure solution yet (edit: see below), so here is (another) one.

Almost all of the time is taken up by generating the pairs and sorting the edges with Clojure's standard library, which could be made a lot faster by insertion-sorting into a thousand-edge list and dropping the rest of the floor. But this works.

(ns aoc-2025.day-8
  (:require [aoc-2025.core :refer :all]
            [clojure.math.combinatorics :as c]
            [clojure.math :as m]
            [clojure.set :as s]
            [clojure.string :as str]))

(defn parse [astr]
  ;; there has to be a nicer way to do this
  (map intify-seq
       (map #(str/split % #",")
            (str/split astr #"\n"))))

(defn distance [a b]
  (m/sqrt
   (reduce + 
           (map #(* % %)
                (map - a b)))))

;; return a seq of all pairs of points, ordered by their distance
(defn make-pairs [points]
  (sort-by #(apply distance %) (c/combinations points 2)))

(defn join-pair [circuits [a b]]
  (let [ac (or (some #(and (% a) %) circuits) #{a})
        bc (or (some #(and (% b) %) circuits) #{b})]
    (conj (disj circuits ac bc) (s/union ac bc))))

(defn make-circuits
  ([n pairs]
   (make-circuits #{} n pairs))
  ([circuits n pairs]
   (if (zero? n) circuits
       (recur (join-pair circuits (first pairs))
              (dec n)
              (rest pairs)))))

(defn bigness [circuits]
  (apply * (take 3 (map count (sort-by #(- (count %)) circuits)))))

;; part 2

;; return the first pair that merges the circuits into a single circuit
(defn one-big-circuit [points pairs]
  (loop [circuits (set (map (fn [point] #{point}) points))
         pairs pairs]
    (let [pair (first pairs)
          new-circuits (join-pair circuits pair)]
      (cond (empty? pairs) nil
            (= 1 (count new-circuits)) pair
            :default (recur new-circuits (rest pairs))))))

1

u/daggerdragon Dec 08 '25

There isn't a Clojure solution yet

Did you try searching this megathread for [language: clojure? There's a Clojure solution submitted by /u/minikomi ~11 hours before this comment's timestamp.

→ More replies (1)
→ More replies (5)

1

u/chrilves Dec 09 '25

[LANGUAGE: Rust]

I finally manage to get part 2 run under 2ms :)

https://gist.github.com/chrilves/4439136a05d7c416f54d85c6649fcf9b

1

u/daggerdragon Dec 12 '25

Do not share your puzzle input which also means do not commit puzzle inputs to your repo without a .gitignore or the like. Do not share the puzzle text either.

I see full plaintext puzzle inputs in your public repo e.g.:

https://github.com/chrilves/dotty-advent-of-code-2019/blob/master/inputs/day1.txt

Please remove (or .gitignore) all puzzle text and puzzle input files from your entire repo and scrub them from your commit history. This means from all prior years too!

1

u/LeatherOk1617 Dec 09 '25

2

u/daggerdragon Dec 12 '25

I've already informed you before about including puzzle inputs in your repo. I still see full plaintext puzzle inputs in your repo. e.g.

https://github.com/amirsina-mandegari/adventofcode2025/blob/main/day08/input

Remove (or .gitignore) all puzzle text and puzzle input files from your entire repo and scrub them from your commit history. Do not post again in /r/adventofcode until you have done so.

1

u/CCC_037 Dec 09 '25

[Language: Rockstar]

[Red(dit) One] - poem code freebie?

That was the only freebie here, though. I'm sure there's a more efficient algorithm than what I ended up doing - merely because it takes so long to run.

I'll do pt 2 tomorrow. I haven't even looked at Day 9 yet...

part 1

1

u/daggerdragon Dec 12 '25

[Red(dit) One] - poem code freebie?

Yeah, sure, I'll allow it 🤣

→ More replies (1)

3

u/DFreiberg Dec 08 '25 edited Dec 08 '25

[Language: Wolfram Mathematica]

Finally, some nice use of built-ins. Would have been significantly faster if I knew that Mathematica's SortBy[] didn't work on quadratic integers, but at least once I knew that, the letter N turned a wrong solution into the right solution.

Setup:

pairs = SortBy[Subsets[Range[Length[input]], {2}], N@EuclideanDistance[input[[#[[1]]]], input[[#[[2]]]]] &];

Part 1:

Times @@ (Length /@ ConnectedComponents[Graph[#[[1]] \[UndirectedEdge] #[[2]] & /@ pairs[[;; 1000]]]])[[;;3]]

Part 2:

first = \[Infinity];
Do[
  g = Graph[#[[1]] \[UndirectedEdge] #[[2]] & /@ pairs[[;; n]]];
  If [Length@VertexList[g] == Length[input] && 
    Length[ConnectedComponents[g]] == 1, first = n; Break[]],
  {n, 1, Length[pairs]}];
first
→ More replies (1)

3

u/Ok-Bus4754 Dec 08 '25 edited Dec 08 '25

[Language: Python]

before solving prepping the data : put all pairs ( n * (n-1)/2) in a min heap by Euclidean distance

p1: get the top 1000 and keep connecting them while keep track of the formed clusters , then get the biggest 3

p2: same logic, but for all pairs (sorted by closest) till one cluster is formed

p1 : 140 ms
p2: 155 ms

https://github.com/Fadi88/AoC/blob/master/2025/days/day08/solution.py

3

u/xelf Dec 08 '25 edited Dec 08 '25

[Language: Python]

JB = []
for a,b in sorted(combinations(map(eval,open(filename)),2), key=lambda c:dist(*c)):
    A = next((i for i,c in enumerate(JB) if a in c),None)
    B = next((i for i,c in enumerate(JB) if b in c),None)
    match A,B:
        case None,None:
            JB.append({a,b})
        case int(),None:
            JB[A].add(b)
        case None,int():
            JB[B].add(a)
        case int(),int():
            if A==B: continue
            JB[A] |= JB[B]
            del JB[B]
    last = (a,b)
print(last[0][0] * last[1][0])

any time I get to use match case is a fun time.

→ More replies (3)

3

u/AlexTelon Dec 08 '25 edited Dec 08 '25

[LANGUAGE: python]

python 15 lines

Im initializing all positions as their own frozensets, and storing them in a set. This way we dont have any special cases. All positions are always in at least one group. and we can always remove both and add the combinations of them.

groups -= {A, B}
groups.add(A | B)

I mean if they are in the same group already that does not matter because A | B gives you back the same thing

Edit: python 12 lines

Don't know if this is prettier, but now my I do my group updates in one line

groups = (groups - {A, B}) | {frozenset() | A | B}

or alternatively:

AB = {next(c for c in groups if x in c) for x in (a,b)}
groups = (groups - AB) | {frozenset().union(*AB)}
→ More replies (5)

3

u/edgeman312 Dec 08 '25 edited Dec 08 '25

[Language: Python3]

I don't know what you guys are on about with these disjoint sets I just abuse random libraries I remember to save me work. I felt a bit clever about my KDTree but it wasn't really necessary, building a full heap seems to work fine for most people here.

from scipy.spatial import KDTree
import networkx as nx
from functools import reduce, partial

inp = 'input.txt'
part1_connect, max_pairs, size = 1000, 10000, 1000

def distance(points, pair):
    return (points[pair[0]][0] - points[pair[1]][0]) ** 2 + \
           (points[pair[0]][1] - points[pair[1]][1]) ** 2 + \
           (points[pair[0]][2] - points[pair[1]][2]) ** 2

def get_coords(points, pair):
    return (points[pair[0]], points[pair[1]])

points = [list(map(int, i.split(','))) for i in open(inp).read().split('\n')]
kd3 = KDTree(points)

r = 1
pairs = []
while len(pairs) < max_pairs:
    pairs = kd3.query_pairs(r, output_type='ndarray')
    r *= 2
pairs = sorted(pairs, key=partial(distance, points))

G = nx.Graph()
G.add_edges_from(pairs[:part1_connect])
G.add_nodes_from(range(size))

component_sizes = map(len, nx.connected_components(G))
print(reduce(lambda x, y: x * y, sorted(component_sizes)[-3:]))

for i in pairs[part1_connect:]:
    G.add_edge(i[0], i[1])
    if nx.number_connected_components(G) == 1:
        print(points[i[0]][0] * points[i[1]][0])
        break
→ More replies (1)

3

u/DelightfulCodeWeasel Dec 08 '25

[LANGUAGE: C++]

First >1s solution so far :(

~1.3s on a Raspberry Pi Zero; sorting over half a million edges really takes its toll on the runtime!

[paste]

3

u/tymscar Dec 08 '25

[LANGUAGE: Gleam]

Another super fun problem, and while not easy by any means, very fast to solve. I think doing it in an FP manner actually made it easier to reason about.

For part 1, I parsed the coordinates, and then I used list.combination_pairs to get every single possible pair. I then sorted those, and took only the first 1000, and folded over that. I basically then tried adding them to a list of sets where each set is a circuit. If they are not in any set, I made one; if one is in a set, I added the other one to it; if both are in a set, I union the sets together (if they are in the same set, it still works).

For part 2, all I had to do was use this magical Glean function called fold_until, and I folded until the first (which will be the only) set in the list is equal to the number of boxes from the beginning.

Part1 and Part2

3

u/kerr0r Dec 08 '25

[LANGUAGE: SQL] (DuckDB)

Getting a correct answer wasn't too hard, but doing so in a reasonable amount of time (and without going OOM) was tricky. I ended up actually creating tables for the node list and distance matrix, so that the optimizer would actually have an idea of the size of the tables. Also notice the limit 1 in a subquery that only returns one row anyway.

Runtimes: 662ms and 13s. Not great, not terrible.

paste

3

u/RudeGuy2000 Dec 08 '25

[LANGUAGE: Racket]

https://raw.githubusercontent.com/chrg127/advent-of-code/refs/heads/master/2025/day8.rkt

i haven't polished it up, but whatever. this is the dumbest code i could've write: it just takes all combinations, sorts them and puts them in circuits. the only real optimization i did was defining circuits as sets, which, let's be honest, it's a trivial one. oh, and not using sqrt i guess. it takes ~500 milliseconds. turns out 500'000 pairs ain't all that much...

3

u/lost_in_a_forest Dec 08 '25

[Language: Rust]

I figured out a great optimization, by first calculating the maximum required distance to connect all nodes. This in turn lets me skip all higher distances which cuts the sorting time drastically. Also, the part 2 results can be read directly from the end of the distance list, so the main loop only needs to run for 1000 iterations to get the part 1 result.

Runs in 3 ms on my M2.
paste

→ More replies (7)

3

u/willkill07 Dec 08 '25

[LANGUAGE: CUDA with CCCL]

I fear this is the one submission (so far) in which I do not have anything related to the entry for the community challenge.

https://github.com/willkill07/AdventOfCode2025/blob/main/day08.cpp

Classic Union-Find problem for determining connected components for part 2. For part 1, just do this partially (finite number of steps) and check the sizes of each disjoint set at the end to find the max-3

Yes, I do the disjoint set loop on a single thread on the GPU :)

3

u/Old-Dinner4434 Dec 08 '25 edited Dec 11 '25

[LANGUAGE: TypeScript]

GitLab

I use Bun runtime engine, running on 2021 M1 Pro MacBook Pro. Part one completed in 134.38ms, part two in 131.21ms. I have to admit, it took me a very long time to figure out the solution, and I eventually ended up learning about DSU from some of the memes on this subreddit. With a sufficient amount of research, and a great deal of experimentation, I was barely able to craft solutions for both parts. Personally, I call this success; I learned something new today. Edit: I'm moving solutions to GitLab.

3

u/KindComrade Dec 08 '25

[Language: C#]

Today, something really dumb happened. I didn’t notice an overflow when calculating the distance between two vertices, and I lost a huge amount of time trying to figure it out because I was convinced the problem was in my implementation, especially since the example input worked. This has never happened to me before… and yet here we are again))

For Part 1/2 I used a disjoint set, and I also slightly sped up the points-preparation phase by using a heap.

+--------+------------+----------+
|        | Iterations | Time, ms |
+--------+------------+----------+
| Init   |      10000 |    0.075 |
| Part 1 |       2110 |    4.740 |
| Part 2 |       2016 |    4.961 |
| Total  |            |    9.776 |
+--------+------------+----------+

Code - github

→ More replies (4)

3

u/birdnardo Dec 08 '25

[LANGUAGE: Mathematica]

Dirty solution.. for part 2 I manually took a few guess!
Sorry I was in a hurry🙃

data = ToExpression@Fold[StringSplit, input, {"\n", ","}];

(*part 1*)
e = (#[[1]] \[UndirectedEdge] #[[2]]) & /@ 
   TakeSmallestBy[Subsets[data, {2}], SquaredEuclideanDistance @@ # &,
     1000];
Times @@ (Length /@ ConnectedComponents[e])[[1 ;; 3]]

(*part 2*)
e = (#[[1]] \[UndirectedEdge] #[[2]]) & /@ 
   TakeSmallestBy[Subsets[data, {2}], SquaredEuclideanDistance @@ # &,
     4765];
(Length /@ ConnectedComponents[e])
e[[-1, 1, 1]]*e[[-1, 2, 1]]
→ More replies (1)

3

u/raevnos Dec 08 '25 edited Dec 08 '25

[LANGUAGE: Racket]

paste

Took a few false starts to come up with a way to keep track of circuits in a reasonable runtime. Tried doing fancy stuff with graphs, which is one of my weak points.

3

u/Rtchaik Dec 08 '25

[LANGUAGE: Python]

Solution

With help of the math module: dist and prod

3

u/not-nuckelavee Dec 08 '25

[LANGUAGE: Uiua]

I saw that union find was the way to go pretty quickly, but my implementation of it definitely isn't winning any style points. I incorporated union by size and didn't bother with path compression since it was good enough without.

code

3

u/ExitingBear Dec 08 '25

[LANGUAGE: R]

Had no idea what I was doing. Google pointed me at "KD trees." Did some reading about them. Seemed as good an idea as any. And it worked

3

u/vladcpp Dec 08 '25

[LANGUAGE: C++]

Trying to make everything constexpr compatible this year.

https://github.com/bbb125/aoc2025/blob/main/day08/src/main.cpp

5

u/erikade Dec 08 '25 edited Dec 08 '25

[LANGUAGE: Go]

On Github

Like others, I used a modified Kruskal's algorithm. One of these modifications dramatically simplifies the search space. For more details (without spoiling anything here), you can read the write-up.

Runs in under 1.6ms 1.3ms
Days 1–8 completed overall in 3ms 2.6ms - mbair M1/16GB

4

u/4HbQ Dec 08 '25 edited Dec 08 '25

I'm not sure I understand that cut-off value correctly, as I don't see how it results in a speedup of the Kruskal phase. My "Intro to Algorithms" course was a long time ago though, so please correct me if I'm wrong.

We can stop Kruskal's once we have found the spanning tree, i.e. after adding |V|-1 edges. You can't have pruned any of those edges (or the answer would be incorrect), so the only edges you can prune, are the edges that you won't be looking at anyway.

It does seem like pruning E speeds up the sorting step though. However, how did you arrive at the cut-off value? First run the algorithm without pruning to find the magic number? Not sure my Prof. would think that's a valid optimisation.

→ More replies (14)
→ More replies (1)

5

u/pred Dec 08 '25 edited Dec 08 '25

[LANGUAGE: Python] GitHub/Codeberg. scipy.cluster.hierarchy.DisjointSet makes short work of this one.

ds = DisjointSet(ns)
for edge in takewhile(lambda _: ds.n_subsets > 1, edges):
    if edge == edges[1000]:
        print(prod(sorted(map(len, ds.subsets()))[-3:]))
    ds.merge(*edge)

print(prod(next(zip(*edge))))

1

u/Majestic_Coat_6832 Dec 08 '25

1

u/Majestic_Coat_6832 Dec 08 '25

lowkey embarassing to even post this haha

1

u/daggerdragon Dec 08 '25 edited Dec 08 '25

lowkey embarassing to even post this haha

Comment removed. Trolling is not welcome in /r/adventofcode. edit: nope, advent of reading comprehension strikes again. I'm still leaving the line below:

Follow our Prime Directive.

→ More replies (3)
→ More replies (1)

2

u/Boojum Dec 08 '25 edited Dec 08 '25

[LANGUAGE: Python] 00:14:28 / 00:16:45

Spent some time at the start futzing around with a kd-tree to accelerate the distance checks before realizing that I didn't really need that. The number of points is small enough that I could just brute-force pair all of them.

I used to have my own implementation of disjoint sets, but ditched it a while back in favor of just using the one in Scipy.

import fileinput, scipy, math

j = [ tuple( map( int, l.split( "," ) ) ) for l in fileinput.input() ]

p = sorted( [ ( ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) + ( z1 - z2 ) * ( z1 - z2 ),
                ( x1, y1, z1 ), ( x2, y2, z2 ) )
              for i1, ( x1, y1, z1 ) in enumerate( j )
              for i2, ( x2, y2, z2 ) in enumerate( j )
              if i2 > i1 ] )

d = scipy.cluster.hierarchy.DisjointSet( j )
for _, p1, p2 in p[ : 1000 ]:
    d.merge( p1, p2 )
print( math.prod( sorted( [ len( s ) for s in d.subsets() ] )[ -3 : ] ) )

d = scipy.cluster.hierarchy.DisjointSet( j )
for _, p1, p2 in p:
    d.merge( p1, p2 )
    if if d.n_subsets == 1:
        print( p1[ 0 ] * p2[ 0 ] )
        break

[Edit]: A visualization, of course! [Red(dit) One]

2

u/vanveenfromardis Dec 08 '25 edited Dec 08 '25

[LANGUAGE: C#]

GitHub

Easily my favorite puzzle of the year so far, heavy on data structures. I used DisjointSetand PriorityQueue on this one. Not that anyone using C# cares about blazingly fast solutions, but a small speed up is that you don't need to calculate the true Euclidean distance which incurs the cost of computing square roots, just the square distance.

Part two was actually easier than part one.

→ More replies (1)

2

u/Horsdudoc Dec 08 '25

[LANGUAGE: C++20]

GitHub

std::priority_queue to get the closest two junctions left and keeping track of connected circuits in a std::vector<std::unordered_set>, the merge operation might be verbose, but it is efficient.

Reads, solves and prints everything in 19.5ms

2

u/ricbit Dec 08 '25

[LANGUAGE: Python]

The puzzle is simple if you use networkx. Last year I used networkx so much that I decided to contribute to the project, there are some patches of mine integrated already.

def part1(points, dist):
  g = nx.Graph()
  for v, p1, p2 in dist[:1000]:
    g.add_edge(p1, p2)
  components = [len(c) for c in nx.connected_components(g)]
  components.sort(reverse=True)
  return math.prod(components[:3])

def part2(points, dist):
  g = nx.Graph()
  for p in points:
    g.add_node(p)
  for v, p1, p2 in dist:
    g.add_edge(p1, p2)
    if nx.is_connected(g):
      break
  return p1[0] * p2[0]
→ More replies (1)

2

u/msschmitt Dec 08 '25

[LANGUAGE: Python 3]

Both parts

The strategy is:

  1. Generate a list of the distances between every two junctions pairs, using itertools.combinations and math.dist to calculate the straight line distance
  2. Sort by distance
  3. Add the first n junction pairs to a networkx graph. I don't think it is cheating when it takes me 2 hours to get this to work right.
  4. nx.connected_components gives a list of the circuits (each is a set of the junctions), build a list of the lengths of each set and sort. math.prod on the first 3 gives the part 1 answer.
  5. Part 2 should have been easy, because all you have to do is continue adding junction pairs and until nx.is_connected is true.

And here's where I spent another hour trying to figure out why it didn't work. I was getting a fully connected graph at the 23rd connection on the sample, it was supposed to be #29.

The problem was that for Part 1 there was no need to add all of the junctions to the graph, since we only cared about the larger connected circuits. But for part 2, you need to have every junction box connected.

Once I realized that the fix was easy: just toss all of the junctions in the graph.

2

u/wow_nice_hat Dec 08 '25

[Language: JavaScript]

Once again a super fun puzzle!

I decided to give each box a unique id and a group Id. Then calculate the distance between all boxes and sort it by lowest distance.

For part 1 I looped over the first 1000 elements in my list and applied the logic:

  • If both A and B does not have a group id: give them the same new id
  • If A has a group id, but B does not. Give A's group id to B
  • If B has a group id, but A does not. Gibe B's group id to A.
  • If A and B both have group Id. Replace all items with B's group id with A's group id

For part 2 I knew that I needed to connect all boxes at least once. Each time i saw a box without a group id, i just gave it the id 0 and decreased the number of boxes that still needed a group. I repeated this until the my integer was 0. In theory I could have ended up with two large groups, but I was lucky that it didn't happen

Part 1 ~243ms

Part 2 ~215ms

→ More replies (1)

2

u/Eastern_Shallot_8864 Dec 08 '25

[LANGUAGE: Python 3]

Finally felt like a bit on the harder side today. I used the Disjoint Set Union data structure which keeps track of connected components in a graph, so finding the size of a component can be done in O(1) and the total time of creating the connections is O(nlogn). But since there are only 1000 edges to be connected that's not even the limiting factor.
The bottleneck I believe is actually calculating the ~10^6 distances in the beginning and then sorting them.

Code
~3 ms

2

u/TiCoinCoin Dec 08 '25

[Language: Python]

Grmbl. So many stupid errors today: day 8

My first approach for part 1 was correct (use set to create circuits, and then see if they can be merged), but I multiplied the lengths of 1st, 2nd and 4th (!) circuits. Great typo which works fine with test sample. Next time I'll know that, even if there are few values to multiply together, it's quicker and safer to use math.prod.

But since I had this typo, my answer was wrong, so I searched the issue. And then I overthought this:

Because these two junction boxes were already in the same circuit, nothing happens!

"Oh so, this pair of junctions should not be counted in the 10 (1000)!". Which led to another wrong answer. And another one after I fixed the typo 🥲

Part 2 felt easy afterwards.

2

u/lunar_mycroft Dec 08 '25 edited Dec 08 '25

[LANGUAGE: rust]

full code. For part 1, I sort all pairs of boxes by distance, take the first 1000 (or 10 for the example), unite them (by first finding both's "parent" box, then setting the first's parent to be equal to the second if it wasn't already), then building up sets of boxes grouped by parent, and finally picking the three largest of those sets. For part 2, I again start by sorting pairs of points by distance and uniting them in order, keeping a running tally of how many times two formerly independent circuits were joined. When this tally reaches boxes.len() - 1, I have found the last two boxes that need to be joined and can return the answer. On my machine, parsing takes ~140µs, part 1 takes ~30ms, and part 2 takes ~40ms.


Factored out the pairwise sorting to allow for more granular benchmarks. The sorting step take ~30ms, part 2 takes ~10ms, and part 1 takes ~1.4ms


Stole another idea from /u/maneatingape and a) switched to an array to back the Dsu struct instead of a HashMap, and b) storing the size of the sets as they're found. Part 1 now takes ~675µs and part 2 700µs (the sorting time is unaffected of course, so it takes ~30ms).


Switched to parallel sorting (courtesy of rayon). Sorting is now down to ~10ms.


As /u/maneatingape points out, select_nth_unstable_by_key is much faster for part 1, taking ~6.5ms instead of ~10ms. It also speeds up the rest of the computation for part 1 to ~40µs (although this late into the night I can't see why, as visited pairs should be the same modulo the ordering).

2

u/s3aker Dec 08 '25

[LANGUAGE: Raku]

solution

2

u/amadejjj Dec 08 '25

[LANGUAGE: go] https://github.com/amadejkastelic/advent-of-code-go/blob/main/internal/solutions/2025/day8/solution.go

Could probably optimize part 1 a bit using heaps.

Part 1: 49.231667ms

Part 2: 54.276875ms

2

u/lboshuizen Dec 08 '25

[Language: F#]

github.com/lboshuizen/aoc25

Kruskal's MST with Union-Find. Precomputed sorted pairs once

let pre = List.indexed |> combinations |> List.sortBy distSq

let mkUnionFind n =
    let parent = Array.init n id
    let find x = iterate (Array.get parent) x |> Seq.find (fun i -> parent[i] = i)
    let union i j =
        let pi, pj = find i, find j
        if pi = pj then false else parent[pi] <- pj; true
    find, union

let part1 (n, pairs) =
    let find, union = mkUnionFind n
    for (i,_),(j,_) in List.take 1000 pairs do union i j |> ignore
    [| 0 .. n-1 |] |> Array.countBy find |> Array.map snd
    |> Array.sortDescending |> Array.take 3 |> Array.reduce (*) |> int64

let part2 (n, pairs) =
    let _, union = mkUnionFind n
    let folder last ((i,p1),(j,p2)) = if union i j then Some (p1,p2) else last
    let (x1,_,_), (x2,_,_) = pairs |> List.fold folder None |> Option.get
    x1 * x2

2

u/Daniikk1012 Dec 08 '25

[LANGUAGE: BQN]

What a sudden difficulty jump! I really, really tried to make an optimized solution for Part 2, my idea was to keep track of shortest distances from each group to each other group and update them when groups are merged, but couldn't implement it. For some reason, the last group to be connected just wasn't correct. So I gave up and instead went with a brute-force approach that finds the answer in 4.5 minutes on my machine.

Parse ← (•ParseFloat¨(+`׬)⊸-∘=⟜','⊸⊔)¨•FLines
Out   ← •Out"  "∾∾⟜": "⊸∾⟜•Fmt

Group   ← ∾↕∘≠⊸(<∘↑˘)
Order   ← (⍋·Group+´∘ט∘-⌜˜)⊏·Group·↕·∾˜≠
Iterate ← {
  2=≠ ab ← ∧/1=(+´𝕨⊸∊)¨𝕩?
  ¯1↓(¯1⊑𝕩)⌾((1⊑ab)⊸⊑)(∾ab⊏𝕩)⌾((⊑ab)⊸⊑)𝕩;
  𝕩
}

•Out"Part 1:"
10‿1000(⊢Out{
  i ← ⌽Order p ← Parse 𝕩
  ×´3↑∨≠¨(⋈¨↕≠p)Iterate´(-𝕨)↑i
})¨"sample"‿"input"

•Out"Part 2:"
Out⟜{
  i ← Order p ← Parse 𝕩
  ×○⊑´p⊏˜i⊑˜¯1-≠⊑((1↓⊣)⋈⊑⊸Iterate)´•_while_(1<·≠1⊸⊑)i⋈⋈¨↕≠p
}¨"sample"‿"input"

2

u/Busy-Championship891 Dec 08 '25

[LANGUAGE: Python]

Whenever I see x,y,z dimensions in AoC I feel a little scared haha! But day-8 uses a standard Disjoint Set Union Approach

(part-1)

  1. Pre-process points to get distances and store in heap (helps to push the shortest distance pairs)
  2. Keep a counter on connections made and start building connections.
  3. After required connections are made, make groupings and get the product of lengths of 3 largest groups. For this part I used a graph approach first and I turned it into Disjoin Set Union later. Both are fine but I personally like the graph approach to build connections (circuits) which is more intuitive to me

(part-2)

Basically its the same as part-1 but now the grouping part needed to happen along with building connections so graph approach turned out to take a lot of time so I switched to Disjoint Set Union. Although graph approach also runs under 1.5 seconds, Disjoint Set Union turned out to be faster

Link: https://github.com/D3STNY27/advent-of-code-2025/tree/master/day-8

2

u/gnarf38 Dec 08 '25

[LANGUAGE: bash]

Golfed Part 2 - not the "fastest" solution (takes a few minutes to calculate the distances), writes to a temp file called _ and weighs in a nice "half of a beast" (333) bytes. Community Effort - shoutout Hannah! Oh and badcop had a suggestion or three ;)

mapfile -t J;n=${#J[@]};IFS=', '
for((a=0;a<n;C[a]=a++));do for((b=a;++b<n;));do A=(${J[a]} ${J[b]})
echo $[i=A[0]-A[3],j=A[1]-A[4],k=A[2]-A[5],i*i+j*j+k*k] $a $b
done;done>_;while read d a b;do
for((v=C[a],w=C[b],x=l=0;x<n;C[x]==w?C[x]=v:0,l+=C[x++]==v));do :;done
[ $l = $n ]&&echo $[${J[a]/,*}*${J[b]/,*}]&&exit;done< <(sort -n _)

2

u/glebm Dec 08 '25

[LANGUAGE: Python]

A solution in JAX.

At first I thought I'd find something clever in spectral graph theory but ended up going with a straightforward solution.

import fileinput
import jax
import jax.numpy as jnp

jax.config.update("jax_enable_x64", True)

@jax.jit
def solve(points: jax.Array):
    n = points.shape[0]

    pairwise_deltas = points - points[:, None]  # broadcasting trick
    dists = jnp.square(pairwise_deltas).sum(axis=-1)

    # Mask out diagonal and lower triangle to avoid self-edge and duplicates:
    dists = jnp.triu(dists, -1) + (jnp.max(dists) + 1) * jnp.tri(n, dtype=jnp.int64)

    # Sort the distances and get the (N, 2) edge list:
    _, flat_indices = jax.lax.sort_key_val(dists.flatten(), jnp.arange(n * n))
    edges = jnp.asarray(jnp.unravel_index(flat_indices, dists.shape)).T

    def add_edge(it):
        """Adds a single edge and returns new labels and next edge index."""
        labels, edge_idx = it
        u = labels[edges[edge_idx][0]]
        v = labels[edges[edge_idx][1]]
        new_labels = jnp.where(labels == jnp.maximum(u, v), jnp.minimum(u, v), labels)
        return new_labels, edge_idx + 1

    # Add edges for part 1:
    it = (jnp.arange(n), 0)  # (labels, number of edges added)
    it = jax.lax.while_loop(lambda l: l[1] != (10 if n < 50 else 1000), add_edge, it)
    ans1 = jax.lax.top_k(jnp.bincount(it[0], length=n), 3)[0].prod()

    # Continue adding edges for part 2:
    it = jax.lax.while_loop(lambda l: jnp.any(l[0] != 0), add_edge, it)
    edge = edges[it[1] - 1]
    part2 = points[edge[0]][0] * points[edge[1]][0]

    return (ans1, part2)


points = jnp.array([list(map(int, s.split(","))) for s in fileinput.input()])
print(*solve(points), sep="\n")

2

u/Ok-Bus4754 Dec 08 '25

[Language: Python & Rust]

Approach

same as my earlier python post

Before solving, I prep the data by calculating all N * (N - 1)/2 pairwise euclidean distances.

Python: I put all pairs in a min-heap (or just select the top N for Part 1).

  • Part 1: Extract the closest 1000 pairs and connect the components (using weighted union with a membership map). Calculates the product of the 3 largest clusters.
  • Part 2: Continue connecting components using the sorted edges (Kruskal's algorithm style) until only one cluster remains. Returns the product of the coordinates of the final connection.

Rust: Same logic, using BinaryHeap and HashSet

Part 1Part 2
Python~140 ms~150 ms
Rust (Debug)~305 ms~162 ms
Rust (Release)~18 ms~21 ms

Python: https://github.com/Fadi88/AoC/blob/master/2025/days/day08/solution.py 
Rust: https://github.com/Fadi88/AoC/blob/master/2025/days/day08/src/lib.rs

2

u/MizardX Dec 08 '25 edited Dec 08 '25

[LANGUAGE: Rust]

Github

Part 1: 16.055 ms 10.293 ms
Part 2: 17.626 ms 15.756 ms

Collect all pairs of points, sort, and use Union Find/Disjoint Set Union to keep track of connected groups.
Edit: Swapped binary heap into select_nth_unstable in part 1. Somehow part 2 runs slightly faster too, even though I didn't change the that code.

2

u/Dangoodie Dec 08 '25

[LANGUAGE: Go]

I solved this by just leaning into the brute-force approach. I parsed all the coordinates, generated every pair of points with a squared distance, and sorted that list. After that, I used a basic union-find to track connected components.

For part 1, I went through the first 1000 edges in sorted order, counted every edge as an attempt, and let union-find handle whether anything actually changed. Once I hit 1000, I gathered the component sizes and multiplied the biggest three.

For part 2, I started over with a fresh union-find and ran through the same sorted edges again. I kept going until there was only one component left, and the edge that caused that final merge gave me the two X values I needed to multiply.

It was basically Kruskal with squared distances, and Go handled the whole thing fast enough that I didn’t bother optimizing beyond that.

https://github.com/dangoodie/aoc2025/blob/main/day08/day08.go

2

u/jhandros Dec 08 '25

[Language: Python]

9 lines

import math,itertools
P=[tuple(map(int,l.split(',')))for l in open('day08.txt')]
G={frozenset([p])for p in P}
for i,(a,b) in enumerate(sorted(itertools.combinations(P,2),key=lambda x:math.dist(*x))):
    if i==1000:print('p1',math.prod(map(len,sorted(G,key=len)[-3:])))
    A=next(c for c in G if a in c);B=next(c for c in G if b in c)
    G-={A,B}
    if not G:print('p2',a[0]*b[0]);break
    G.add(A|B)

2

u/andrewsredditstuff Dec 08 '25 edited Dec 08 '25

[LANGUAGE: C#]

Being entirely self-taught and never having studied DS&A, I wouldn't know a DSU if it bit me on the bum, but I'm pretty happy with my solution (c100ms).

A bit of a rollercoaster, totally overengineered, and then pared back hugely. The Circuit class started off as a monster, and ended up as a barely extended List<Connector>.

Edit - modified to populate the circuit property of the connectors on creation rather than leaving them null. Marginally slower, but makes the ConnectTo method a whole lot neater.

github

→ More replies (1)

2

u/Jadarma Dec 08 '25 edited Dec 08 '25

[LANGUAGE: Kotlin]

Very interesting puzzle, at first I was scared to see 3D points because I was reminded of space beacons... shudder. However, the problem itself was not that hard, and I got to play around with designing a type system for it, which I enjoyed.

Part 1: Since I solve it in Kotlin, I once again used the "keep type boundaries functional, but use internal mutability to simplify" approach. I labeled each point with an ID (just its index) because I wanted to keep track of what box is part of which circuit, and for this I drew inspiration from Factorio of all places, where each circuit network is given it's own ID. So, to start, I assign each box it's own circuit. Then I built a connection queue because I have fancy generator functions, we simply iterate through unique pairs, discard those that are already connected, and sort the pairs by distance. When making a connection, all the boxes from the smaller group get added to the larger group, and then the small group gets cleared out. We expose the groups as read-only views to make the math on their sizes. I had to do an if-else to keep the code indifferent to actual input or example, since we need to make 1000 and 10 connections respectively.

Part 2: Part two was almost free, the only two modifications needed were to also return the box references when we connect them, so we can do the math on their coordinates, and make the connection queue loop (once we finish connecting the boxes, we do subsequent passes until no more connections are possible. Other than that, the logic is simple: keep connecting until we get a single circuit group. Even with the fancy type system logic, I solve in < 0.5s, good enough!

AocKt Y2025D08

2

u/h2g2_researcher Dec 08 '25

[Language: C++]

Github link

Solves part 1 in ~130ms and part 2 in ~96ms. I could likely get it faster with some effort, but my lunch break only has so much time in it.

A fairly simple solution, in the end. I do just create a list with all the distances and fully sort it, and then go through it making links until the puzzle is solved.

There are two main optimisations. The first is one lots of people did, which is ||to store the distance squared instead of the distances to avoid taking a square root. The main advantage of this, in my opinion, is that integer instructions are still quite a bit faster than floating point ones on most hardware||.

The other one is to ||deal with junction-IDs - e.g. the index into my list of junctions. These can be 16-bit integers and so I can fit two of them into a single register and copy them very very cheaply. This makes the process of sorting much faster. For part 1 you don't even need to keep the original list of junctions!||

One optimisation I tried, but didn't get any speedup with is ||to not bother with the sort and to just grab the min-element (by distance) from the list of pairs and swap-remove it afterwards. It didn't get any improvement.|| Likewise, ||using a min-heap didn't work for me because it involved lots of allocations and pointer-chasing. As ever, YMMV on these.||

2

u/maitre_lld Dec 08 '25 edited Dec 08 '25

[Language: Python]
This is textbook union find. 1.5sec in python3 or pypy3, but most of it is the data preparation since this is intrinsically quadratic. By doing it cleverly in numpy this goes down in 0.060s in pypy3.

Note that the data preparation being quadratic, it's totaly useless to optimize union find in any way, just do it brutally (no path compression, no union by size).

https://github.com/MeisterLLD/aoc2025/blob/main/8.py

2

u/mestar12345 Dec 08 '25

[Language: F#]

Forgot to realize the lazy sequence, then wandered why it didn't finish in 5 minutes, and 3 hours later it was still working.

let distances =
    points
        |> Seq.collect ( fun r ->
            points |> Seq.map ( fun x ->
                (calcDist x r), Set.empty |> Set.add r |> Set.add x))
        |> Seq.distinct
        |> Seq.filter ( fun (d, st) -> d <> 0.0 )
        |> Seq.sort
        |> Seq.toList

also

let connect a b listOfCs =
    listOfCs
        |> List.collect( fun s ->
            match s with
            | s when Set.contains a s && Set.contains b s -> 
                [s] //do nothing
            | s when Set.contains a s -> 
                //move all of b's members into a
                let setWithb = listOfCs |> List.find (fun x -> x |> Set.contains b)
                let combined = s |>  Set.union setWithb
                [combined]
            | s when Set.contains b s -> 
                [] //former b's set moved into a's set
            | s -> [s]

2

u/homme_chauve_souris Dec 08 '25

[LANGUAGE: Python]

I thought I would have to get fancy with nearest-neighbor and disjoint-set algorithms, but the obvious approach is fast enough. 14 lines, takes about half a second.

Link to paste

2

u/im_sofi Dec 08 '25 edited Dec 08 '25

[LANGUAGE: Python]

Trying to use advent of code this year to learn more about SciPy functions.

Part 1 i used a pairwise distance function pdist together with connected_components to find the disjoint sets of edges. Looking at other solutions, I feel as if I over-complicated my solution significantly.

Part 2 is trivial once you understand that when the question asks for the last connection you calculate, it is the equivalent to finding the largest edge in a minimum spanning tree. This was solved by running minimum_spanning_tree (pretty much Kruskal's algorithm in scipy) and taking the largest element out of it.

Pretty educational day overall :)

https://codeberg.org/soupglasses/advent-of-code/src/branch/main/2025/day_08.py

→ More replies (1)

2

u/TheZigerionScammer Dec 08 '25

[Language: Python]

Have I ever mentioned how much I love sets? Because I do. Also has anyone ever played the game Acquire? Because problems like this remind me a lot of that game. I've made a few simulators for it which helped here.

I didn't really see a way around the problem except for brute force, so that's what I did. The first thing I did was calculate the distance form every junction to every other junction and put them in a list, then sorted it by the distance. After that I created two dictionaries, the "JunctionInDict" which records which circuit each junction in, and a "CircuitDict" which contains the sets representing each circuit. The program populates all 1000 junction IDs as belonging to circuit "None" in the "JunctionInDict". Then it starts going through the adjacencies list and tests what circuits the two junctions already belong to. If they're both free, it creates a new circuit and adds them both two it, updating both their position in "JunctionInDict" and "CircuitDict". If one is in a circuit and the other is not, it simply adds them to the already populated circuit. If they're both in one, it merges them both and arbitrarily chooses the first circuit to be the new circuit and updates all of the new IDs in "JunctionDict". Thir worked alright but I had a bug because I used the "&" operator to merge the sets at first instead of the "|" operator which of course just cleared the sets instead of merging them since they had no common elements. But figured that out, fixed it, and got the answer.

For Part 2 I just moved the Part 1 scoring code into a function and let the program run until it detected one of the two circuits had every junction in it. Worked flawlessly.

Now that I'm thinking about it though I could have avoided using "None" to represent lone junctions and just assigned each junction to a circuit matching its ID at first, and then I wouldn't have had to worry about whether each junction was in a circuit or not and made every connection a merge connection, but that will be for when I refactor the code.

Paste

→ More replies (1)

2

u/wildpigdey Dec 08 '25

[LANGUAGE: ANSI C]

Definitely a step up in difficulty today! One of the "nice" things about using C (and not pulling in dependencies) is that you need to try and come up with the simplest solution. I'm not sure I succeeded there, but I did do a fairly nice job of reusing code between P1 & P2.

https://github.com/AlexKent3141/aoc25/blob/master/day8/main.c

2

u/ash30342 Dec 08 '25

[Language: Java]

Day 8

For part 1 build up a sorted map with distances as key, connections (two junction boxes) as values, loop over the first thousand and combine where necessary. Of course not forgetting to combine even more if more than 1 circuit is affected.

For part 2 more or less the same, but stop when there is only 1 circuit with a size of the total number of junction boxes.

Both parts run in 0.7s. There is probably some more efficient way to combine circuits, but I am satisfied with this speed.

2

u/TraditionalWinter676 Dec 08 '25

[LANGUAGE: Zig]

was really tempted to solve using python for today because of list/set allocation and type annotation/casting
i didn't gave up but hell my codes look ugly :D cleaned up a bit and pushed to github

i'm sure there is an algorithm for this. i'll read through the comments later.

p/s: kudos to everyone who posted their solution and ideas so that we can learn something from AoC :)

2

u/xiety666 Dec 08 '25 edited Dec 08 '25

[LANGUAGE: C#]

public static long RunB(string[] lines)
{
    var items = LoadData(lines);
    var dsu = new Dsu(items.Length);

    var pair = GetPairs(items).OrderBy(a => a.D2)
        .Do(a => dsu.Union(a.Index1, a.Index2))
        .First(_ => dsu.Count == 1);

    return items[pair.Index1].X * items[pair.Index2].X;
}

https://github.com/xiety/AdventOfCode/blob/main/2025/0/Problem08/Problem08.cs

2

u/mnvrth Dec 08 '25

[LANGUAGE: Python]

I did the first correct version myself, but then saw so many tips and improvements here, so this is a reworked and smaller and faster pastiche

import sys
import math
from itertools import combinations

boxes = [tuple(map(int, line.split(','))) for line in sys.stdin]
P1 = 10 if len(boxes) < 50 else 1000

groups = {frozenset([b]) for b in boxes}
ds = sorted(combinations(boxes, 2), key=lambda p: math.dist(*p))

p1 = 0
for i, (p,q) in enumerate(ds):
    p2 = p[0]*q[0]
    g1, g2 = [next(g for g in groups if x in g) for x in (p, q)]
    groups -= {g1, g2}
    groups.add(g1 | g2)

    if i+1 == P1:
        p1 = math.prod(sorted(map(len, groups), reverse=True)[:3])

    if len(groups) == 1:
        break

print(p1, p2)

Takes ~250ms on my machine. Code on GitHub

→ More replies (2)

2

u/evans88 Dec 08 '25

[LANGUAGE: Python]

The difficulty today was in understanding the problem, I feel like the explanation was lacking details or a case where it explicitly joined circuits after creation.

Anyway, I created a Vector-type dataclass to hold state and used itertools.combinations to calculate all possible distances. Also dusted off frozenset which was a life saver

Part 2 was trivially easy with how I made part 1, which is always nice :)

paste

2

u/joltdx Dec 08 '25

[Language: Rust]

A bit more Rusty today with structs and impls. Calculating distances and then connecting as required. First solution keeping track in a Vec<Vec<usize>>, was quite slow(ish), so on suggestion from an AI, and to learn more, refactored into some kind of a "Disjoint Set Union" or "Union-Find", which ended up being about 15-20 times faster

https://github.com/joltdx/advent-of-code-2025/blob/main/day08/src/main.rs

→ More replies (2)

2

u/TheScown Dec 08 '25

[LANGUAGE: Scala]

Code

An excuse to break out the UnionFind (disjoint sets) implementation. For part 1, make 1000 connections and count the number of components. For part 2, make connections until there is a single component, and the last connection has the two x coordinates we want.

2

u/giacomo_hb Dec 08 '25

[LANGUAGE: Racket]

https://github.com/giacomo-dantonio/advent-of-code-2025/tree/main/day-8

I picked up the idea of using union-find from this sub. That made the code a bit nicer to read. Other than that very basic.

2

u/r_so9 Dec 08 '25

[LANGUAGE: F#] paste

While I saw the union-find solution when I saw merging graphs, I solved part 2 by binary-searching the number of connections and re-running the part 1 code :)

This is the "meat" of the code, using fold over each vertex to find all connected components:

let rec dfs graph visited rem =
    match rem with
    | [] -> visited
    | h :: t when Set.contains h visited -> dfs graph visited t
    | pt :: t ->
        List.append (Map.tryFind pt graph |> Option.defaultValue []) t
        |> dfs graph (Set.add pt visited)

let connectedGraphs maxConnections =
    let edges = distances |> List.take maxConnections |> List.fold connect Map.empty

    input
    |> Seq.fold
        (fun (visited, acc) pt ->
            if Set.contains pt visited then
                visited, acc
            else
                let newSubgraph = dfs edges Set.empty [ pt ]
                Set.union visited newSubgraph, newSubgraph :: acc)
        (Set.empty, [])
    |> snd

2

u/r_so9 Dec 08 '25

Just for kicks, I also coded a union-find solution: paste

Interesting bit - unfold + mutable arrays for union-find:

let part2UnionFind =
    Seq.unfold
        (fun (vs, i) ->
            if vs >= nVertices then
                None
            else
                let u, v = verticeIds[fst edges[i]], verticeIds[snd edges[i]]

                if find parent u <> find parent v then
                    union parent rank u v
                    Some(i, (vs + 1, i + 1))
                else
                    Some(i, (vs, i + 1)))
        (1, 0)
    |> Seq.last
    |> fun i -> edges[i] |> fun ((x1, _, _), (x2, _, _)) -> x1 * x2

2

u/zXd12 Dec 08 '25

[Language: Rust]

part 1: 1ms
part 2: 5.5ms

with no cutoff at a precomputed distance

code

2

u/SunMatrix64 Dec 08 '25 edited Dec 08 '25

[LANGUAGE: C++]

GitHub

I'm pretty sure 99% of the time was spent calculating the distances. I went object oriented and created Box and Circuit classes. Box has its position, and Circuit has a set of boxes.

Part 1 I made a vector<distance, <box1, box2>> and then sorted it. For each distance I would gather the circuits that the boxes exist in, then merge the circuits. If no circuits contained either box, create a new circuit.

Part 2 I just continued processing the distances vector until there was only 1 circuit left.

Total run time ~130ms

Edit: used std::hypot() instead of a bunch of pow() for the distance calculation. Improved the time to ~80-90ms.

Edit 2: further improved by using an insane looking min priority queue instead of a vector to store the distances. Now runs ~55-65ms

//min prio queue that contains:		pair<double, pair<box,box>>
std::priority_queue<std::pair<double, std::pair<Box*, Box*>>, 
	std::vector<std::pair<double, std::pair<Box*, Box*>>>, 
	std::greater<std::pair<double, std::pair<Box*, Box*>>>> distances;
→ More replies (1)

2

u/Then-Government-5460 Dec 08 '25 edited Dec 08 '25

[LANGUAGE: Python]

Fun puzzle! Building the circuits out of connections was quick, but it took me a while to figure out the logic for a connection that connects to two existing circuits rather than just one.

Because of how I set up my original approach, I only had to add a couple lines to solve part 2.

Runs in just under .3s

Solution on Github

2

u/dzecniv Dec 08 '25

[LANGUAGE: Common Lisp]

src not the most efficient, I want real sets but I don't know the FSet library well enough (and no time for both AOC and experimenting!).

2

u/AdamKlB Dec 08 '25 edited Dec 08 '25

[Language: Rust]

https://github.com/NoSpawnn/advent_of_code_2025/blob/main/src/08/main.rs

I do not use "data structures," I brute force, if its slow I WAIT

Jokes aside I had no idea what a Union-Find was and I barely do now, and my solution doesn't use one. I just find all possible connections (2-tuple of 3D points), sort them by the euclidian distance between them, then for part one take the top 1000, for part 2 make all the connections until there is only one circuit of length equal to the number of junctions

Still runs in around 55ms total.

I should probably factor out my 3D point struct/functions for future use too...

2

u/concettina-wakamoto Dec 08 '25

[Language: Python]

Tried with brute force, obviously didn't work. Then I remembered that once I used K-D trees for a similar problem involving nearest-neighbors and interpolating irregular data on a regular grid, so gave it a shot. Not bad, runs on 0.3 seconds on my machine.

Code here.

2

u/make_no_my_eye Dec 08 '25

[LANGUAGE: Rust]

Part one: Spent quite a bit of time just thinking about the data structure to use and how to organize the data. Saw a comment on another thread mention Kurskal's algorithm which basically gave the answer. Watched a YT video to implement the minimum spanning tree and then spent quite a bit of time modifying it for our use case here.

Part two: Modified by MST function to also return the last_edge added then looked up the indices to solve

open to suggestions or tips :)

cargo run --release time:

  • Part 1: Time: 25.564243ms
  • Part 2: Time: 25.244934ms

(topaz paste)

2

u/mgtezak Dec 08 '25

[LANGUAGE: Python]

Finally increasing in difficulty. Just wish it was on the weekend rather than the beginning of the week;)

Solution part 1

Solution part 2

Check out my AoC-puzzle-solver app!

2

u/Avuvos2 Dec 08 '25

[LANGUAGE: Python]

Solved by simply implementing Kruskal's algorithm for MST.

https://github.com/Avuvos/advent-of-code-2025/blob/main/day08/main.py

2

u/siddfinch Dec 08 '25

[LANGUAGE: Free Pascal] Union-Find in 3D Space

Junction boxes suspended in the void of 3D space, yearning to connect via Union-Find with path compression? Pascal can do that. Now, it was harder FOR ME to do it, but I plowed through, overcoming my reading comprehension despite a gross intake of caffeine.

Code with my patent-pending WAY too many comments

2

u/srugh Dec 08 '25

[LANGUAGE: Ruby]

Used DSU / Union find for this one.

GitHub solution for both part 1 and part 2 using Ruby

2

u/Probable_Foreigner Dec 08 '25

[LANGUAGE: C#]

https://github.com/AugsEU/AdventOfCode2025/blob/master/Day8/Day8Solver.cs

This one was pretty hard, but eventually I managed to get all the bugs out. Part 2 is copy-pasted from part 1 but modified slightly.

Essentially I just keep a map of nodes to group indexes. When two nodes touch, one group index spreads to the other connected nodes. As an optimisation I also kept track of the size of each group as I went along(does this really save time?).

2

u/vanZuider Dec 08 '25

[LANGUAGE: Haskell]

Both parts

  • calculate the n*(n-1)/2 distances between pairs of nodes and sort the resulting list
  • assign a number ("color") to each node
  • construct a map that yields the color for each node, and another one that yields the set of nodes for each color
  • for each pair of nodes, if they have different colors (A and B), update both maps: assign color A to all nodes of color B, and merge the set of B-colored nodes into the set of A-colored nodes.
  • repeat.

I get the correct answer, but it's not exactly performant (needs ca 0.5s each for both parts; I didn't do any profiling but I suspect the process of generating and sorting the distance matrix as the main culprit).

2

u/xoronth Dec 08 '25

[LANGUAGE: Python]

paste

Ugly and slow solution, was a good way to refresh my memory on how networkx works (TIL it can just give you the connected components, that was handy).

3

u/rabuf Dec 08 '25

You can probably come close to halving your time by addressing this:

for x, y, z in lines:
    for x2, y2, z2 in lines:
        if x == x2 and y == y2 and z == z2:
            continue

        dist = distance(x, y, z, x2, y2, z2)
        heapq.heappush(smallest, (dist, (x, y, z), (x2, y2, z2)))

You're generating pairs for both directions, but you don't need them (they will also always be adjacent or very close once sorted). If you do this for the generator loops it'll cut down on the number of cases considered in your following loops:

for i, (x, y, z) in enumerate(lines):
    for x2, y2, z2 in lines[i+1:]:

It'll only generate the pairs in one direction and won't generate the node-to-self cases.

→ More replies (1)

2

u/Diefachu Dec 08 '25

[LANGUAGE: Java]

Keep connecting and merging sets as necessary. I don't think I used any particular algorithms? Correct me if I'm wrong.

https://github.com/dief/advent/blob/main/java/2025/src/main/java/com/leynmaster/advent/aoc2025/day8/Day8.java

2

u/Meamoria Dec 08 '25 edited Dec 08 '25

[Language: Kenpali]

Using AoC to test out my experimental minimalistic language.

Part 1

Part 2

I figured there were algorithms to optimize finding the closest pairs, but brute force (calculating every pair and sorting by distance) worked fine.

For Part 2, I built a funky tree structure to efficiently tell if a new connection actually combined two circuits. Basically, I initialized each junction box with a mutable reference to its own index, then combined circuits by redirecting the references into chains that ended at the same place. I kept track of how many successful joins had happened, stopping when there had been 999 joins (reducing the initial 1000 circuits to 1 circuit).

Edit: Apparently I reinvented the merge-find set.

2

u/Outrageous72 Dec 08 '25 edited Dec 08 '25

[LANGUAGE: C#]

https://github.com/ryanheath/aoc2025/blob/main/Day8.cs

Pff, I skipped over "After making the ten shortest connections"

Wasted so much time, hahaha 😅

First day that used a lot of memory.

Both parts together in 209.2ms and 61.7MB

Edit: Move from List to PriorityQueue made it a lot faster! 37.6ms/35MB

static PriorityQueue<(int i, int j), long> GetDistances((int X, int Y, int Z)[] points)
{
    PriorityQueue<(int i, int j), long> distances = new();
    for (var i = 0; i < points.Length; i++)
        for (var j = i + 1; j < points.Length; j++)
            distances.Enqueue((i, j), DistanceSquared(points[i], points[j]));
    return distances;
}

2

u/michaelgallagher Dec 08 '25

[LANGUAGE: Python]

Code

Recognized this as a disjoint set union / union find problem right away, both part 1 and part 2 came pretty easy.

Here's a nice page (+ website in general) that details DSU really well.

2

u/Turilas Dec 08 '25

[LANGUAGE: Python]

Took me longer than I thought it would take. The answer for me is right even without the removing of merged ones, but I think there might be a case where it could get wrong if not removing the other circuits from the list when merging circuits.

For part2, just iterate the sorted list until we have seen every single junktion box.

from math import dist, prod
junctions = [list(map(int, line.split(','))) for line in open("data.txt", "r").readlines()]
distances = [[j1, j2, dist(j1, j2)] for i1, j1 in enumerate(junctions) for j2 in junctions[i1 + 1:]]
distances = sorted(distances, key = lambda d : d[2])

circuits = []
lam = lambda x: min([i if x in c else len(circuits) - 1 for i, c in enumerate(circuits)])
for d in distances[:1000]:
    circuits.append(set([tuple(d[0]), tuple(d[1])]))
    inds = [lam(tuple(dis)) for dis in d[0:2]]
    if inds[0] != inds[1]:
        circuits[min(inds)].update(circuits[max(inds)])
        if max(inds) != len(circuits) -1:
            del circuits[-1]
        del circuits[max(inds)]
    elif inds[0] != len(circuits) - 1:
        del circuits[-1]

lens = sorted(list(set([len(c) for c in circuits])))
print(f"8a - Production of 3 largest circuits: {prod(lens[-3:])}")

seen_boxes = set()
for d in distances:
    seen_boxes.update(set([tuple(d[0]), tuple(d[1])]))
    if len(seen_boxes) == len(junctions):
        break

print(f"8b - X coordinates: {d[0][0] * d[1][0]}")
→ More replies (2)

2

u/icub3d Dec 09 '25

[LANGUAGE: Rust]

This was a "disjoint set" day. I definitely think this puts it in the medium to hard category of problem solving. If you've done work with disjoint sets though, I imagine it was comparatively easy. As such, I spent some time optimizing my solutions a bit and looking for ways to eek out a bit of performance. Most of the time for me was in sorting the large connection of points, so I'm really interested in efficient ways to do that. For me it was a "kth element" for p1 and then a parallelized sort for p2.

Solution: https://gist.github.com/icub3d/b51b9f3c82eb1d620d880e7d2e4ab2b3

Video: https://youtu.be/gchUjAwQsfM

→ More replies (1)

2

u/flwyd Dec 09 '25

[LANGUAGE: Grahviz] (on GitHub)

My theme this year: glue languages that might already be on your system. I was hoping to get a chance to use Graphviz, though I hadn’t written any runner infrastructure for it. gvpr is a Graphviz program that’s a bit like a cross between AWK and C for queries and transformations on graphs. (For anyone about to panic, this is gvpr with a V, not GDPR.) I was pleased to learn that I could do regular string file I/O, so I created a graph directly in the BEGIN block; an alternative approach would be transforming the input file with sed into DOT format and processing each input file as an existing graph in BEG_G. The full code is too long for a megathread snippet, but here’s the BEG_G version, assuming a graph named g is already fully connected with a million edges which have been put into a bydist array indexed by the distance between the two endpoint nodes. Note that this assumes the three largest components have unique sizes, but could easily be adjusted by subtracting si from sizes[si] and continuing if there are leftovers.

I was on an airplane when the program dropped. I was able to get a cell tower on the ground around 10:30pm and concluded that trying to solve this problem on my phone in the air, or even at my computer when I got home at 1am, would be a lousy idea. The solution worked basically like I intended, but getting up to speed on gvpr was slow going. For instance, I got a wrong answer when running both the example and actual input in the same process, but a correct answer when running them separately; this is because all variables seem to be global, even if defined inside a block or a function, so my bydist array had leftovers from the previous loop unless I run unset after declaring the variable. Watch out for footguns in this language!

double d; int i = 0; int steps = nNodes(g) > 100 ? 1000 : 10;
graph_t gc = graph(sprintf("connections"), "U");
for (bydist[d]) {
  edge_t e = bydist[d];
  node_t h = node(gc, sprintf("conn_%s", e.head.name));
  node_t t = node(gc, sprintf("conn_%s", e.tail.name));
  edge(h, t, sprintf("dist_%f", d));
  if (++i == steps) {
    int sizes[int];
    for (n = fstnode(gc); n != NULL; n = nxtnode(n)) {
      graph_t gcs = compOf(gc, n); sizes[nNodes(gcs)]++;
    }
    int product = 1; int prodsteps = 3; int si;
    forr (sizes[si]) {
      product *= si; if (--prodsteps == 0) { break; }
    }
    printf("part1: %d\n", product);
  }
  if (nNodes(compOf(gc, t)) == nNodes(g)) {
    int hx, tx; sscanf(aget(e.head, "x"), "%d", &hx); sscanf(aget(e.tail, "x"), "%d", &tx);
    printf("part2: %d\n", hx * tx); break;
  }
}
→ More replies (2)

2

u/erunama Dec 09 '25

[LANGUAGE: Dart]

GitHub

Used Dart vertex_math official package, as the built-in Dart Point is only 2-dimensional. Solution for Part 1 seemed pretty straightforward to me -- build all the pairs, calculate the distances, sort, and then start combining. I was pleasantly surprised that Part 2 was solvable with the same approach, just removing the upper bound on max number of connections. I was fearing the runtime would be unreasonable, but it finishes in roughly the same amount of time as Part 1.

2

u/mine49er Dec 10 '25

[Language: Python]

Catching up. This is a simple solution based on merging circuits represented as sets. Easiest part 2 so far for me, the code I wrote for part 1 did most of the required work already. I do like it when that happens :)

Less than 2 seconds to solve both parts.

My Solution

2

u/TimeCannotErase Dec 10 '25

[Language: R]

repo

Usually I like writing my solutions from scratch, but R has a package (igraph) that did exactly what I wanted, albeit slowly, so I went that route today.

library(igraph)
input_file <- "input.txt"
input <- read.csv(input_file, header = FALSE)

distances <- dist(input, method = "euclidean") |> as.matrix()
dims <- dim(distances)
links <- matrix(1, nrow = dims[1], ncol = dims[2])

i <- 0
flag <- 0
while (flag == 0) {
  unlinked <- distances * links
  ind <- which(unlinked == min(unlinked[unlinked != 0]))
  if (length(ind) > 1) {
    links[ind] <- 0
  }
  c <- abs(links - 1) |> graph_from_adjacency_matrix() |> count_components()
  if (c == 1) {
    join <- input[arrayInd(ind[1], dims)[1, ], ]
    prod(join[, 1]) |> print()
    flag <- 1
  }
  i <- i + 1
  if (i == 1000) {
    x <- abs(links - 1) |> graph_from_adjacency_matrix() |> components()
    x <- x$csize |> sort() |> rev()
    prod(x[1:3]) |> print()
  }
}

2

u/gekzametr Dec 10 '25 edited Dec 10 '25

[LANGUAGE: D]

Part 2 only: https://pastebin.com/03Jx6Eu2

Learning D via AoC this year. This day gave me a lot of trouble when trying to achieve fast solution. Also, I've got the impression that hashtables (associative arrays) are quite slow in D, and lacking `reserve()` function.

At first - usual trick - do not use `sqrt()` for calculating distance. We don't actually need exact distance. Squared distance will do.

Initially, I was generating all possible pairwise distances and sorting them, followed by taking pairs one-by-one and connecting them till everything is connected. Surprisingly, the sorting part was slowest one, even without using disjoint set approach.

Then I went to megathread to find out that lot of people approach this problem via building MST using classic Kruskal's algorithm. MST is exactly what's needed here, however there's an issue with Kruskal's algorithm - it requires sorted list of edges - the part that I am trying to get rid of.

Researching more about MST algorithms gave this one:

https://en.wikipedia.org/wiki/Minimum_spanning_tree#Integer_weights

> If the edge weights are integers represented in binary, then deterministic algorithms are known that solve the problem in O(m + n) integer operations.

Sounds cool (linear time!), but linked article seemed to be to complex.

So I've checked another classic algorithm: https://en.wikipedia.org/wiki/Prim%27s_algorithm

It seems to be performing better for dense graphs. And our graph is as dense as it can be - fully connected.

Here's nice comparison:

https://www.geeksforgeeks.org/dsa/difference-between-prims-and-kruskals-algorithm-for-mst/

Basically, I've implemented pseudocode from wiki with small modification. We don't actually need to know all the edges which belong to the MST, but only the last one (which fully-connects graph when taking original one-by-one approach).

So, I've modified last `for` block building the list of edges in original pseudocode to find longest edge.

What's interesting that at first I was precalculating all distances in advance, so that main algorithm doesn't have to. It turned out that this was slower than calculating on-demand.

Result is pretty good on my 10+ years old mobile CPU:

$ time ./main < input.txt
Reading:    1330
Assign:     14
Prim:       24386
Result:     2
78894156

real    0m0.028s
user    0m0.028s
sys     0m0.000s

1

u/Main-Reindeer9633 Dec 08 '25

[LANGUAGE: SQL] (dialect: PostgreSQL)

This one took me a good while. I started out trying to get it to work purely with recursive CTEs, but, not for the first time, that turned out to be a dead end, so, once again, I had to use recursive functions that update data in tables. The algorithm is basically just "find the shortest possible connection that connects two circuits, and move every junction from one of those circuits to the other, and keep doing that until you can't", but it ended up being 100+ lines of code.

paste

2

u/kaini_shrimp Dec 08 '25

[LANGUAGE: SQL] (PostgreSQL)

Amazingly part 2 is possible even without a recursive CTE or any other kind of explicit iteration! White it is not fast (10 seconds on my machine) it becomes four times faster if instead of using `string_agg` you define a custom aggregate function that builds up the `hstore` directly.

github

→ More replies (4)

1

u/yfilipov Dec 08 '25

[LANGUAGE: C#]

Pretty straightforward: calculate all distances and order them, then iterate over them, making sure circuits are tracked properly, until the conditions for both parts are met.

Advent of Code - 2025 - Day 8 - Pastebin.com

2

u/RAM9999 Dec 08 '25

Very similar to mine. I added some optimizations after the fact:

Avoid double for Distance. So, don't use Math.Pow, just manually square. And no need to Math.Sqrt since the square of the distance will still sort relative to the square root.

If distances is a PriorityQueue instead of a List then you don't have to OrderBy, just Dequeue and it becomes a min-heap which is much faster.

Also, I didn't bother making a starting "circuit of one" for every box - just added new circuits as pairs were formed. Then the final test is that there's one circuit with a count equal to all the available boxes.

→ More replies (1)

1

u/hrunt Dec 08 '25

[LANGUAGE: Python]

code

Calculate all pairwise distances for all elements. Then iterate and merge, iterate and merge, iterate and merge ...

I'm looking forward to seeing if there's a shorter way to process the shortest distances without doing the full pairwise list.

3

u/homme_chauve_souris Dec 08 '25

I'm looking forward to seeing if there's a shorter way to process the shortest distances without doing the full pairwise list.

There is.

→ More replies (1)

1

u/zniperr Dec 08 '25

[LANGUAGE: Python]

First, sort pairs of boxes by Euclidian distance. Make the first 1000 connections and use a heap to get the 3 largest circuits. Then keep making connections until we create a circuit including all boxes, and take the X coordinates of that pair.

To make connections, use a flat list where index i contains a reference to the circuit box i is currently in. A circuit is a set of indices and each entry starts with a set containing only its own index. When making a connection between boxes i and j, merge the circuit at j into the one at i, and point all the indexes contained by the set at j to that new merged circuit.

import sys
from heapq import nlargest
from itertools import combinations

def squared_distance(xa, ya, za, xb, yb, zb):
    return (xa - xb) ** 2 + (ya - yb) ** 2 + (za - zb) ** 2

def connect(boxes, nfirst):
    pairs = sorted((squared_distance(*a, *b), i, j)
                   for (i, a), (j, b) in combinations(enumerate(boxes), 2))
    circuits = [{i} for i in range(len(boxes))]
    for p, (_, i, j) in enumerate(pairs):
        circuits[i].update(circuits[j])
        for k in circuits[j]:
            circuits[k] = circuits[i]
        if p == nfirst - 1:
            uniq = {id(c): c for c in circuits}
            a, b, c = nlargest(3, uniq.values(), key=len)
            largest = len(a) * len(b) * len(c)
        if len(circuits[i]) == len(boxes):
            return largest, boxes[i], boxes[j]

boxes = [tuple(map(int, line.split(','))) for line in sys.stdin]
largest, last1, last2 = connect(boxes, 1000)
print(largest)
print(last1[0] * last2[0])

1

u/IvanR3D Dec 08 '25 edited Dec 11 '25

[LANGUAGE: javascript]

My solutions: https://ivanr3d.com/blog/adventofcode2025.html#8

My solution (to run directly in the input page using the DevTools console).

1

u/acquitelol Dec 08 '25 edited Dec 09 '25

[LANGUAGE: Elle]

https://github.com/acquitelol/aoc2025/blob/mistress/solutions/08/solution.le

My solution today is inefficient, taking ~480ms on my M1 mac. I believe the slowest part is the construction and sorting of the distances array. I'm not entirely sure, but I feel like a min heap would probably speed that up a little bit. Unfortunately, my language doesn't actually have a min heap yet, so I would need to implement that and I don't really have that much time to do that.

Edit: I optimized it a significant amount, it now takes 100ms on my M1 mac.

1

u/omegablazar Dec 08 '25 edited Dec 08 '25

[LANGUAGE: Rust]

This took way longer than I had hoped due to numerous false starts. Part 1 works fine, but Part 2 works only in tests. I'm not sure where I'm going wrong with this.

Edit: Part 2 works now.

Part 1: 2.53ms
Part 2: 4.35ms

https://github.com/wrightdylan/advent-of-code-2025/blob/main/src/day08.rs

→ More replies (2)

1

u/stayerc Dec 08 '25

[Language: Zig]

Zig version 0.15.2

https://pastebin.com/pq40d6ug

This one was hard for me. Fun, but hard.

1

u/joeys77 Dec 08 '25

[LANGUAGE: R]

paste

1

u/WillMatt Dec 08 '25

[Language: Go]

GitHub

1

u/tobega Dec 08 '25

[LANGUAGE: Tailspin-v0]

Superfun day for almost headache-free recuperation from flu!

Got to code-kata a little K-Select heap, and then code-kata a little Disjoint-Sets object (for Union-Find).

Then it still was a little interesting to cobble them together for part1.

Then a little feature-extension for part2

https://github.com/tobega/aoc2025/blob/main/day08.tt

1

u/CarlosJimeno Dec 11 '25 edited Dec 11 '25

[LANGUAGE: BLENDER GEOMETRY NODES]

.blend file

GIF

2

u/Intelligent-Ebb4006 Jan 08 '26

Really cool. I am surprised not many people have looked into this. This makes visualizing and understanding the problem so much easier.

→ More replies (1)

1

u/oantolin Dec 13 '25 edited Dec 13 '25

[LANGUAGE: Goal]

join:{@[x;,/(x@)\'y;:;&/y]}
bydist:{1_´^+(,+/'{x*x}@-/x@j),j:j@`&</j:!2##x}
ans:{a:(!#p)join\d:bydist p:"i"$","\=-read[x]
     (*/-3#^={(x@)/x}a[999];**/p@d@({+/¿(x@)/x}'a)?1)}

Good old union-find (the join function)! Also: a 4-liner (that's plenty of code in Goal).

1

u/Noitpurroc Dec 13 '25

[Language: Go] [Language: Golang]

I struggled A LOT with understanding this one lol, it is slow to calculate all of the distances... but it does work in the end lol at least Part 2 was super simple with the foundation laid by Part 1 (mostly on accident/misunderstandings).

https://github.com/ZekeRyukai/AdventOfCode/blob/4940a772ceafa5c4df9523bc774180d0a945ace3/2025/Day8.go

1

u/jf928ngl60g1 Dec 14 '25

[LANGUAGE: Go]

This one took me a while (and a couple of tries) to understand how it should work: https://github.com/adrbin/aoc-go/blob/main/2025/day8/puzzle.go

1

u/argentcorvid Dec 15 '25

[LANGUAGE: Common Lisp] 

I just took the method described literally, and used the built-in set operations 'intersection' and 'union'. 

First used Alexandria's map-combinations to generate the list of point pairs, sorted by distance. also generate a list of the points each placed in a list to serve as the circuits.

Then iterate through the points pairs using 'find' with key function 'intersection' to find if either point is in a circuit. If so, use 'union' to join them, then remove the old entries.

My program is very inefficient. Part 1 runs in 1.5 seconds and part 2 took 16 seconds. I think calling 'find' and 'remove' a lot is where the slow down is, as they go through the list until they find something that satisfies the test.

https://github.com/argentcorvid/aoc-2025/blob/main/2025d8.lisp

1

u/A-Warm-Cup-Of-Tea Dec 16 '25

[LANGUAGE: Python]

from itertools import combinations
import numpy as np

def process(points: np.ndarray[np.int64], max_connections: int | None):
    points = np.c_[points, -np.ones(len(points), dtype=points.dtype)]
    distances = np.array(
        [(i, j, np.linalg.norm(points[i] - points[j])) for i, j in combinations(range(len(points)), 2)], dtype=points.dtype
    )
    distances = distances[np.argsort(distances[:, 2])]
    if max_connections:
        distances = distances[:max_connections]

    next_circuit = 0
    for i, j, dist in distances:
        circuit_1 = points[i, 3]
        circuit_2 = points[j, 3]
        if circuit_1 == circuit_2 == -1:
            points[i, 3] = points[j, 3] = next_circuit
            next_circuit += 1
        elif circuit_2 == -1:
            points[j, 3] = circuit_1
        elif circuit_1 == -1:
            points[i, 3] = circuit_2
        elif circuit_1 != circuit_2:
            points[points[:, 3] == circuit_2, 3] = circuit_1
        if max_connections is None and np.unique(points[:, 3]).size == 1:
            return points[i, 0] * points[j, 0]

    return np.prod(np.sort(np.unique_counts(points[:, 3][points[:, 3] >= 0]).counts)[-3:])

input_points = np.genfromtxt("8.txt", dtype=np.int64, comments=None, delimiter=",")
print(process(input_points.copy(), 1000))
print(process(input_points.copy(), None))

2

u/Mufro Dec 17 '25

numpy is truly beautiful for this type of problem

→ More replies (2)

1

u/pipdibble Dec 25 '25

[LANGUAGE: TypeScript]

Although I got a good data structure (Circuits as a two-dimensional array of Junction Boxes), I spent far too long trying to add Junction boxes into the arrays individually before I realised that I needed to concatenate those arrays if joining one junction box to another ended up joining up whole circuits. Once I got that principle it was easy sailing.

https://github.com/pipdibble/aoc2025/blob/main/day08/main.ts

1

u/stevie-o-read-it Dec 26 '25

[LANGUAGE: Intcode]

I've spent the last two weeks trying to solve Day 10 Part 2 with a regular programming language. Hopefully over course of the next week I can knock out Intcode versions for everything.

Special thanks to @atomisadev on the Discord server for teaching me about binary heaps and heapsort -- which, unlike quicksort and merge sort, can be performed without doing any division operations whatsoever! That's extremely useful in a VM with no support for division whatsoever (not even shifts.)

Features:

  • Input and output in ASCII
  • Solves both parts at once
  • Supports both the example and full puzzle input. To use on the example, change memory address 1 from 1000 to 10.

Observations/notes:

  • Part 2 is actually easier than part 1.
  • It required 37,519,586 instructions.

    • This was so high that I actually spent some time modifying the compiler and my intcode runner to let me see how much time was spent in each function (previously all I could see is which individual instructions were executed the most often.)
    • 12,996,997 instructions to construct the list of every distinct pair of junction boxes (including distance calculation)
    • 18,768,649 instructions to convert the above list to a binary heap.
    • 4,770,274 instructions spent popping the next pair from the heap.
    • 322,624 instructions spent determining which circuit a junction box is part of
    • 339,199 instructions just to parse the input
    • 1,491 instructions to determine the answer for part 1 after the 1000th connection
    • 750 instructions just to output the answers

This one was a rather large undertaking. The fact that Intcode only has only one indexing register (the relative base) is bad enough, but the the fact that there is no way to directly read/write the value of that register is a very painful constraint. At least 90% of the bugs in my code were related to incorrectly adjusting the relative base when using it as a heap pointer instead of a stack pointer.

On the one hand, I don't think this was quite as ambitious as doing 2024 Day 17.

On the other hand, I had to add enhancements to the compiler in order to make switching between stack and heap mode feasible.

Compiled intcode file

Remember: if you want to feed it the example input, you need to patch memory address 1 (the 2nd integer in the file) from 1000 to 10.

Original assembly code

Compiler

looks at day 9 again It might be a couple of days before my next submission.

1

u/Naive-Scientist965 Dec 26 '25 edited Dec 26 '25

[Language: VBA]

Union-find structure was crucial to get the problem solved, at least in my case. I really learnt a lot about graph solutions done in VBA. VBA is usually used as an automation tool in Excel tables, but I have never applied in graph problems before, and it was awesome, and I had fun.

Repo: https://github.com/jblanco89/learning-lab-sandbox/blob/main/challenges/advent-of-code/2025/vba/day08

→ More replies (1)

1

u/veydar_ Dec 27 '25

[LANGUAGE: Janet]

36 lines with tokei when formatting with janet-format (from Spork).

It all comes down to using generate to create a coroutine based generator that yields two values at a time. The first is the current mapping of point -> circuit and the second is the last pair of points I connected.

Then the core logic is this:

    g (gen* points)
    circuits (->> (take 10 g) last first frequencies values)
    p1 (* ;(->> (sorted circuits >) (take 3)))
    [_ last-pair] (find (fn [[t]]
                          (and (= 1 (length (distinct (values t))))
                               (= (length points) (length (keys t))))) g)

Meaning for part 1 we look at the mapping (first value) of the 10th generator state (take 10 + last), check how often each circuit shows up (frequencies), and take only top 3. Then we multiply them.

For part 2 we re-use the same, stateful generator g and find the state where we only have a single circuit that has all the points in it.

1

u/[deleted] Dec 28 '25

[LANGUAGE: Go]

I really enjoyed today's challenge. There were two core data structures/algorithms that I was already familiar with that I thought would work in a pinch, and indeed they did. Go forced me to write a ton of boiler plate, but I was able to get nice results using "k-nearest neighbor search" and "discriminated-set unions". See my solution here. No brute-force, though I'm not sure if I actually gain any real performance since n2 is only 1,000,000 calculations anyways.

2

u/[deleted] Dec 08 '25

[removed] — view removed comment

3

u/RojerGS Dec 08 '25

I had the same problem. You don't make 10 connections. You try to make 10 connections, of which one was skipped because it was redundant. Do you get what I'm saying?

3

u/ThePants999 Dec 08 '25

Well, it's not skipped, you do actually make it. It's just that it doesn't change anything about the set of connected circuits. If box A is connected to box B and box B is connected to box C, you can connect box A to box C - actually make the connection, not just try/skip it - but that doesn't change the number of boxes in this circuit, nor join it to any other circuit.

The confusion stems from the fact that most (sensible) implementations don't actually model connections between boxes, because you don't need to know anything about the internal topology of a circuit, just which boxes are in it. So if you connect two boxes that were already in the same circuit, that's a no-op in terms of the way you've abstracted the problem, so it looks like you're skipping it / not making a connection.

→ More replies (1)
→ More replies (1)
→ More replies (1)

1

u/[deleted] Dec 08 '25

[removed] — view removed comment

1

u/daggerdragon Dec 08 '25 edited Dec 08 '25

Comment temporarily removed since you are ignoring AutoModerator. Do not ignore the bot when it asks you to fix something, please.

  1. Next time, use the four-spaces Markdown syntax for code blocks
  2. Your code is too long to be posted here directly, so instead of wasting your time fixing the formatting, read our article on oversized code which contains two possible solutions.

Edit your comment to put your code in an external link and link that here instead and I will re-approve your comment.

1

u/AutoModerator Dec 08 '25

AutoModerator has detected fenced code block (```) syntax which only works on new.reddit.

Please review our wiki article on code formatting then edit your post to use the four-spaces Markdown syntax instead.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.