6
votes
Day 8: Playground
Today's problem description: https://adventofcode.com/2025/day/8
Please post your solutions in your own top-level comment. Here's a template you can copy-paste into your comment to format it nicely, with the code collapsed by default inside an expandable section with syntax highlighting (you can replace python with any of the "short names" listed in this page of supported languages):
<details>
<summary>Part 1</summary>
```python
Your code here.
```
</details>
Clear ramp up in difficulty compared to the previous days. I like it!
For part 1, I initially used a max heap to keep track of the smallest 1000 pairs seen so far, followed by a DSU/Union Find data structure to keep track of which junctions are in the same connected component and the size of the components. Finally, I used a min heap to find the largest 3 connected components. However, it's much better to replace both heaps by just storing all the elements and using quick select to get the best N elements, since we don't care about their order. In my case, runtime went from ~14ms down to ~3ms when switching to quick select.
Part 2 can be done with a sort on all the pairs and using a DSU again to figure out when the current connected component includes all the junctions (based on its size). However, this can be further optimized by sorting while iterating over all the pairs and stopping early once all the junctions are connected. This optimization pushed the runtime from ~22ms down to ~4.7ms.
Solution Rust
TIL about disjoint set data structures and quick select. Definitely going to check those out later. Very nice solution!
Nice little increase in difficulty! I started with each junction box
positionin its own circuitfrozenset, then iteratively merged the sets in the order of shortest position combinations. I'm sure there's some optimization left on the table, but a runtime of <5s using only built-in Python types is good enough IMO.Part 1
Part 2
Really enjoyed this one! Definitely the hardest so far this year. I finished an initial solution in just under an hour, but took some extra time to clean it up and improve performance (switching to a minheap rather than pushing to a list and running quicksort afterward sped things up quite a bit). I think it could still stand to be a little more efficient, but it is what it is; the vast majority of the time is spent calculating and inserting the distances into the minheap, and I'm not sure how to optimize that code any further.
I'm glad I thought ahead and wrote a binary heap in advance. Most recent years have had at least one pathfinding puzzle, which is what I intended it for, but it came in handy for today!
Solution (Lily)
Heap implementation
Definitely more to think about today, and got to learn all about sets in python. I've also started
declaringhinting most of the types in function definitions because it feels like a good practice, and because I kept getting confused while implementing this. Looking at it now, almost everything is the same type, so it's pretty extraneous now, but it took a while for me to settle on (lists of) sets.Fun fact... Today I figured out that I've been globally scoping my file inputs and a handful of other variables this whole time. Mostly through luck it hasn't been an issue yet, but I'll try to not do that in the future. It could have easily been a disaster in today's puzzle.
Part 1 and 2 [Python]
Elixir
This one went much smoother for me, but still took me about 2 hours.
I'd be curious how much time y'all are spending on these solutions, because I do find it's starting to be a bit of a drain now. But then again, it's time well spent learning a new language.
I had two silly mistakes:
Part two was super quick, not much to change from part one.
Both parts
Benchmarks
It's... not very quick. But adequate.
The kinds of things some of y'all are cooking up to my amazement just demonstrate
how little time I spent on classical algorithms and data structures in my day job.
Catching up (poorly) on the last couple of days. I found a union-find pack which took out most of the hard work, though the rest of my code is still terribly inefficient. Oh well, just gotta keep the momentum going for now.
Prolog Solution