March 10, 2025

Advent of Slowed Days 5, 6 and 7

I am, once again, very behind schedule with my Advent of Slowed adventure. This is due to a combination of being busy at work, getting sick, my kids getting sick, and Day 5, Part 2 being annoying (I think one of my commit messages was literally “Part 2 can go to hell”). We are now somewhat back on track and I hope to catch up in the next week or two.

Day 5

The data for day 5 took the form of a list of pairs of numbers, interpreted as a relation between numbers, and then the task for part 1 was to validate that some lists of numbers were in order with respect to that relation. I accomplished this reasonably speedily, although not at all efficiently: for each pair of numbers, I look through the list to confirm that the list conformed to the constraint of that pair. There are quicker ways to confirm a list is sorted, I am sure.

Part 2 then involved sorting lists of numbers. I decided that instead of rolling my own sort, I’d turn the list of pairs into an ordering, and use Rust’s sorting algorithms to sort the lists of numbers. And this is where I came up against a problem.

I wrote a solution that worked fine for the example data but wasn’t getting me the right answer on the full data. What I was basically doing is counting how many times a number appears on the right side of a pair. More appearances on the right hand side of a relation means its further to the right in the ordering. Use that count to order the numbers. This did not work, and when investigating, it looked like lots of numbers were getting the same count. So I thought that, for whatever reason, this shortcut wasn’t working and I would have to do it properly. I looked around at the data enough to also realise that the relation in the full data wasn’t really like the relation in the example data. In the example data, the relation defined by the list of pairs has a bunch of nice properties: in particular it’s transitive and it’s total. Neither of these properties hold of the relation defined by the pairs in the full data.

This is annoying. Now I’m perfectly OK with the Advent of Code example data not highlighting all the edge cases that might crop up in the real data, but this just strikes me as the example data being misleading. In fact, as we’ll see, the example data is even more misleading: the real data is even worse!

So I now took my task to be the following: construct a relation from the pairs in the data, take the transitive closure of that relation, and sort the lists with that. This took some getting right: I tried various methods and one thing or the other was not working quite right. Eventually I got something that I thought should work fine and… it did not work. In fact I was getting the same wrong answer as my original method did.

The problem, it turns out, is that the relation from the real data has a trivial transitive closure: every element holds the relation to every other one. So the real data is even less well behaved than we thought, and really, just a completely different problem from the one being solved in the example data.

I realised that I would need to filter the list of pairs to be used in the sort for each list I sort, and at this stage I gave up. I also had a cold at about this time, and I just put doing these puzzles to one side for a couple of weeks. I feel confident I could come back to part 2 and put an answer together, and I might do that at some stage, but for now, this one annoyed me into submission.

Day 6

After dealing with everyone in the household having various levels of cold, and after some busy times at work, I decided I’d come back to a clean slate and take a stab at day 6, leaving day 5 part 2 undone for now.

For day 6, I moved the Coords module I’d created for day 4 into its own file with its own tests so I can reuse here. The puzzle asks us to count how many positions a guard visits given some rules for how he moves about. I basically built a State object and solving the problem meant stepping through the guard’s movements until we reached a Stop state.

Setting all this up was slow, but I was being careful about writing tests for everything, and it did mean that when I finally ran the full solution, I got it right first time. I was lucky to spot a couple of potential pitfalls before falling into them too.

Part 2 required us to consider where to put a new obstacle, and the only way I could think to do this was to just try it out for each possible obstacle position. This was the first part of this year’s advent of code where the solution took a noticeable amount of time to be calculated. I could definitely make this faster (I can think of a couple of ways already and I’m sure there are more) but I got my answer so I’m happy for now.

Day 7

Being a little lax with my tests for day 5 definitely contributed to how long it took me, and being disciplined with writing tests made day 6 feel a lot easier, so I went into doing day 7 again using a sort of test-driven approach.

The first part involved working out whether a number can be made from a sequence of numbers by sticking the right operations between them. My strategy was to generate a list of all possible combinations of operations of the right length, and then try them all in sequence. Since for part 1 there are only two possible operations, if you need $n$ operations, there are $2^n$ possibilities. And generating all those possibilities can be done by taking each number from $0$ up to $2^n -1$, writing it in binary, and then mapping 0s to “add” and 1s to “multiply”.

Day 7 was one of those nice days where it turns out that the strategy you put together for part 1 is very easy to adapt for part 2. Part 2 adds a third operation: concatenation. So the same approach works: list all combinations of operations and try them in turn. Because of my nice tests, I could add a new boolean flag to say whether we were including concatenation, set the flag to false everywhere it is currently used, and confirm everything still works as expected. Then I add a new match case to the mapping operation. The trickiest part, actually, was probably rolling my own ternary representation of a number. I could have used an external crate, I guess, but I wrote something good enough for now. As for day 6, part 2 of day 7 takes a few seconds to run. And again I can already see a bunch of quick wins in performance, but I’d rather keep moving forward with the problems.

If I catch up with my deadlines and need something to do before starting the next problem, maybe I’ll revisit these problems: solve day 5 part 2, improve performance on parts 2 of days 6 and 7.

Lessons learned

I spent too much time with Day 5 Part 2: I should have stopped earlier and moved on. I’m doing these to learn things and enjoy myself, and wrestling with misleading examples furthered neither goal.

I have found taking a test-driven approach to be quite useful. It’s slow, and it kind of makes refactoring a little harder because I have to go refactor my tests, but it means less back-and-forth: once I’ve built something and tested it, I can go on to the next bit and be fairly confident that I’ve got it right. I could be wrong about that assumption on occasion, but it’s worked out alright so far.

I am beginning to get the hang of rust, but I now understand the memes about fighting with the borrow checker. I understand why it’s built the way it is, and I’m sure the guarantees it can give you are worth a great deal in some circumstances, but I am honestly finding it a bit annoying for this sort of project. I like the tooling (clippy, rust-analyzer and so on), I like the algebraic data types, I like having tests right there in the file, I like the helpful compiler error messages, I like traits as a way to write generic code, but refactoring a function yet again because it turns out I need to borrow this variable rather than move it is just annoying. Maybe as I get more used to it, my instincts about this will improve, but I am currently at the stage where a significant portion of my time on this Advent of Slowed project is spent tweaking my muts, moves and borrows to get it to compile. The alternative approach would be liberal use of clone(), but I kind of want to get it to work in a kind of rust-y way, rather than just abusing clone(). (Also, I have definitely used a bunch of unnecessary clone()s in some of my solutions).

I’m going to persevere because I think rust is a language I’d like to know better, but I am not sure I’d choose it to do this kind of project again. I need the code to give me the right answer precisely once and I don’t really mind if it takes a while to run (within reason). All of the things rust does to make code reliable, memory safe and lightning fast are not things I get any advantage from, but I still have to deal with all the sharp edges of the language that are there to make those things possible.

Having said that, I do really enjoy working with a language with a nice rich type system. I probably marginally prefer rust to haskell in this regard because I found haskell a bit too austere. Now that I’m more comfortable with combinator parsers, the input-parsing part of the exercise might not be so bad in haskell using parsec or one of its descendents, and maybe if I could figure out a testing framework to get some approximation of my rust workflow working in haskell, maybe I could enjoy doing these puzzles there.

Enough navel-gazing! On to day 8. If I get day 8 finished in the next few days I will be back on track. I am, however, going to Rome this weekend, so I will almost certainly immediately fall behind again.

© Seamus Bradley 2021–6

Powered by Hugo & Kiss.