April 25, 2025

Advent of Slowed Days 11, 12, and some refactoring

So we’re nearing the end of April and I’m still (more or less) on schedule given the targets I set myself at the start of the year. In fact, I finished Day 11 before the deadline and so I took a little time to revisit Day 5 Part 2, and a few other bits of refactoring.

Day 11

This was a pretty easy puzzle, and one that took the classic AoC form of “Part 1: do this for n; Part 2: OK now do it for a bigger n”. The obvious solution for Part 1 takes way way way too long to run for Part 2. Essentially each step involves doing something to each element of a list. That something returns 0, 1 or 2 new items for the next iteration. So the size of the list can double each iteration if you put back in 2 elements each time. Doing this for 25 iterations is kind of manageable, you get a list of up 33 million-ish items for each starting item. (It’s actually less because you don’t always put 2 elements in the next list, but as a worst case estimate, this is ok). Part 2 is to do the same thing for 75 iterations which puts you up in the sextillions of items. The trick for Part 2 was to realise that order doesn’t matter, and all we ultimately care about is the final total number of items. So if there are duplicate numbers in your list, you can just do the calculation once and keep track of the number of duplicates. That means, instead of keeping track of a big list of items, you keep track of a smaller list of (item, repeats) pairs. My implementation meant re-sorting and re-grouping the list every iteration, but I’m sure there’s an even more efficient way of doing it.

Refactoring

I went back to Day 5 and finished off Part 2. It was still kind of fiddly, but after some time away from it, I think I got there quicker than I would have done if I’d just kept at it at the time.

I also replaced my old process of downloading the puzzle input using a python script with essentially the same script rebuilt in rust. This means one less step in the process: I can just call get_data and rust will check if the data exists, and if not, use reqwest to download it.

I had a go at improving the performance of Day 6 Part 2 as well. This was about finding places where you can place an obstacle to force the guard into a loop. My original solution involved iterating over all possible positions on the map, but, of course, the only places where an obstacle will make a difference to the guards route are the positions on the guard’s route. So I can iterate over a much smaller set. This, and a couple of other tweaks cut the test time down from 90 seconds to 20 seconds. I bet I could shrink it by at least another order of magnitude by parallelising it, but that’s a project for another day.

Day 12

Another puzzle, another classic AoC experience: this time, I committed to some premature optimisation in Part 1 that I thought would help in Part 2, but it didn’t. In fact, Part 2 involved a complete rewrite that made things way way simpler and had negligible effect on performance.

I was quite pleased with my solution to this one actually, because it involved spotting a few non-obvious aspects of the problem. Your puzzle input is a grid of characters, and you need to split that grid of characters into contiguous regions containing the same letter. Once again using the coords module, I can generate a HashMap of coordinates to characters. I can then split that up into contiguous regions using an algorithm for finding graph components. I originally thought to use a recursive algorithm for this, but that proved tricky given rust’s memory model, so I went for adding items to visit to a queue instead. It’s tricky to recurse, because you want to have a function that visits a node, and then visits all its neighbours. So the function needs a mutable reference the data structure that keeps track of what has been visited, but you can’t pass that reference in to each of the recursive calls, because then you’d have more than one mutable reference! (This is my understanding of at least one of the problems with this).

This was where I made my premature optimisation mistake. I happened to have read something that suggested representing a graph as a vector where each element of the vector is a collection of indices into that vector (representing those nodes that have an edge with the node in this position). You could either make a separate vec for this, or each element of the vector is some more complex type that contains a collection of neighbour indices. In any case, this is a good idea because get on Vec is O(1). So looking things up by index in a vec is quick: at the time, I thought this would give me an advantage over other methods, such as using my HashMap directly.1 The other mistake I made was trying to muddle through with just using tuples of elements and mapping this to that, just trying to keep clear in my mind what everything was. I got through Part 1 like this, but reviewing my code after unlocking Part 2, I just went “God, this is a mess”.

So, rip it all out, start again. Create a struct to house all our useful data (with some nice helper methods), and don’t bother with the whole vector of indices thing: just loop through a queue of coordinates, keep a HashMap from coords to our bespoke struct around. Everything is suddenly so much more comprehensible!

The puzzle itself was about counting the area and boundary size of those contiguous regions with the same character. For Part 1, the boundary you are counting is just the number of places where one character meets another. I did this by just doing 4-n where n was the number of (same character) neighbours each character had. Part 2 instead wanted the total number of straight walls for each region. Here I looked at the coordinates in a region where their neighbour to the north is not in the region, and then, among those, I filtered out any coordinates whose neighbour to the west is also in this collection. I did this for each cardinal direction (looking west for north and south, looking north for east and west). This basically gave me one member (the east-most or south-most member) of each wall. The interesting thing about this was it made me realise that I could do Part 1 in the same way (just skip the second step) and that this approach makes it clear that you don’t need to keep track of who is a (same-character) neighbour to whom. Once you’ve split things out into components, that information is implicit.

Onwards to Day 13! Almost halfway through the puzzles!


  1. At the time, I thought get would be slower for a HashMap, and I think in some theoretical sense it is, but its expected cost is also O(1) so in practical terms, I was wrong about this. ↩︎

© Seamus Bradley 2021–6

Powered by Hugo & Kiss.