#Day 08 Solutions
94 messages · Page 1 of 1 (latest)
This feels more like advent of code now
I probably did a bunch of unnecessary stuff
Contribute to svenwiltink/aoc development by creating an account on GitHub.
naive and slow
you're not supposed to commit your inputs btw
cc @pure glade
Hmm why?
Can I copy/redistribute part of Advent of Code? Please don't. Advent of Code is free to use, not free to copy. If you're posting a code repository somewhere, please don't include parts of Advent of Code like the puzzle text or your inputs. If you're making a website, please don't make it look like Advent of Code or name it something similar
because erik asks nicely 🙂
Oh :( But then my tests won't work hmmm
Let me think how to do this
how does your find and union work?
In computer science, a disjoint-set data structure, also called a union–find data structure or merge–find set, is a data structure that stores a collection of disjoint (non-overlapping) sets. Equivalently, it stores a partition of a set into disjoint subsets. It provides operations for adding new sets, merging sets (replacing them with their...
TIL
Yeah this
Basically map where the key is the element and the value is the root of the element
I sm0l brain just created a graph for each edge that I added xD
union find is really helpful for some problems, Ill add it as helper with some more useful functions on it
new edge? Better try finding a cluster again. Cluster != len(coords)? Add edge
not very efficient xD
Just fast enough for this input size
damn that was yucky
Contribute to geofpwhite/advent2025elixir development by creating an account on GitHub.
both of my parts too like 10 seconds to run but idrk how to optimize them in ex
damnnnn i'm so dumb why did I think i would have to generate 1000! pairs
this is just handshake problem
What's that
if u have x ppl in a room and they all shake hands with each other how many handshakes will happen in total. and the answer is the sum from 1 to x-1 aka (x*x-1)/2
part2 is simpler than part1
with all the printing
Part 2: Result: 136 624 [17194 12279 10275] [6238 3377 1071] 107256172
real 0m1.465s
user 0m0.578s
sys 0m0.118s
commenting out the intermediate printing
real 0m0.104s
user 0m0.099s
sys 0m0.005s
very ugly and not very fast
https://github.com/ldemailly/advent-of-code/blob/main/2025/day08/day8p2.go
there's a reason i'm not posting any runtime measurements 🧌
grol version is about 1/2 into generating distances
Point 510 done.
later will use pprof on these
lol
Part 2: Result: 136 624 [17194,12279,10275] [6238,3377,1071] 107256172
10:57:27.584 [INF] All done - retcode: 0
real 15m13.313s
user 14m40.921s
sys 0m6.037s
slightly slower than go - but easier to write, including that it sorts by distance and points automatically
damnnnnnnnn just redid this by pregenerating the pairs and this is so much faster
day 8 bench time: [14.200 ms 14.350 ms 14.527 ms]
part 1 + part 2 but my part 1 is slower
oh nvm
part 2 is nasty
took me 13ms
wait
nah its alright let me profiel this tho
clap my cheeks spending an hour to do disjoint set union on smallest edges
78% of time is spent on building the heap

idek how necessary heapifying is i just used slices.Sort
SortFunc in go right?
isnt sort a full one though?
ye
wdym a full one
n.log(n)
quicksort or whatever
the conceit of this problem is you only need to pull a small number of all the edges
so you want a heap
unless youre doing fuckass computer graphics algos which idk
i don't think that's the overall trick. the overall trick is just realizing that it's a (doably) finite number of pairs so u can precompute distances then sort then merge til u have one left. the heap is just one way of sorting the distances
n(n-1)/2 pairs
NOT 1000! as my smart ass friend blake pointed out to me
ie loop for i 0 to < len-1; then from i+1 to < len
ya you need to precompute all the distances
but u could still use a heap to pull the shortest ones
its definitely huge for part 1
idk about part 2 tbh, i just assumed youll be iterating over a small sample of the edges to get the MST
given in part 2 you have to do the whole thing, not sure what "small number of all the edges" does
but yeah topK if there was only part1
or if brute force sorting wasn't plenty fast enough for 500k entries
i think i know what u mean where u can just skip merge step if the cur two closest are in the same circuit already
I guess maybe part2 can be instant as in finding max distance?
no?
ah no because max can be close to another in the set
yeah i think u just gotta keep merging but it's fast
maybe some sort of bounding box though
btw I wonder if they actually setup the data such as if you don't use euclidian distance you get screwed (eg if you use say sum(abs(Ai-Bi)))
i bet they did
oh yeah
i managed to do p1 without "merging" subgraphs
i built a graph with all the edges first, then iterated over that to create a forest
also
you can see that i spend 10x as long in building the heap as popping from it
heap building is O(n), pops are log(n) each, so clearly we're using a very tiny amount of the possible edges
(this should be pretty obvious, there are ~N^2 edges and the MST will have ~N edges)
this is pissing me off