January 16, 2025

Advent of Slowed Days 1 and 2

As I said in my first Advent of Slowed post, I started as I mean to carry on: blowing through the deadlines I set myself. According to the timeline I originally envisaged, I should have finished Day 2 of last year’s Advent of Code on the 8th and Day 3 on the 14th. Well, late last night, on the 15th, I finally finished Day 2. In my defense, most of the time I get to work on these problems is late in the evening with a baby asleep on me. I’m tired and distracted, and how much I get done depends on how deeply asleep he is. I’m also doing these puzzles in Rust, a language I only started learning last year.

I think I also took a bit of a wrong turn in my attempt to solve part 2, and this set me back a good long while. So part 1 of Day 2 was simple enough: figure out if a sequence of numbers is increasing by small amounts or decreasing by small amounts. That is, figure out if the differences between adjacent elements are all in a small positive range or all in a small negative range. (The ranges in the puzzle where [1,3] or [-3,-1] inclusive). This was just a matter of taking a windows sliding window view of the list, mapping each pair of elements to its difference, mapping this to a pair of booleans (is a small increase, is a small decrease) and then reducing this by doing “and” elementwise on our pair of booleans. Do this for each sequence in the input, and count how many pass the test. The code is available here if you’d like to look at some probably very amateurish rust code. I was fairly pleased with this solution: it was reasonably elegant, and felt like it should be quite efficient. I guess I could have combined the two maps together, but for the sake of readability I think it makes sense to keep them separate. I broke various parts of the solution out into separate functions as part of some attempts to reuse code in part 2. Again, there might be some efficiency gain by smooshing them all together, but I’m happy with where the code sits in efficiency/readability trade-off space.

Part 2 then involves figuring out the same thing, but allowing one number to be skipped. So 1,2,3,2,4,5 should count as a success, because you can drop the second 2 and then the sequence passes the test. The naive approach I wanted to avoid was just iterating over the length of the sequence, removing the ith element from the sequence and then seeing if it passes the test from part 1. This felt inefficient, and I wanted to see if I could do a similar kind of map/reduce approach to permitting a skip. This turned out to be a real headache. I tried a bunch of different approaches, and I submitted about 10 different answers to the website, none of them were the right answer.

I think the most promising approach involved using split_inclusive to break the list into a list of lists where the last element of each inner list is a point where the predicate is false. So, in this case, you pass in the predicate “is a small increase” and then it splits the list up into chunks where each chunk ends where it’s not a small increase (except the last chunk, which might or might not end with a failure of the predicate). Then you just count the chunks right? WRONG! If there’s only one chunk, you’re golden, and if there’s more than three chunks, it’s definitely failed. But in the middle there, there’s a lot of fiddly cases, and I never got it to work quite in exactly the way it was supposed to.

So, for example, if the original sequence was [2,7,3] this should pass, because if you skip the 7 the sequence is all small increases. So take the sequence of differences: [5,-4], and split it into [[5], [-4]]. Note that neither chunk passes the test for acceptability. Skipping the 7 means that the change at the point where 7 used to be is the sum of the differences either side of it. That is, the change between 2 and 3 is 1, which is the sum of 5 and -4. Here’s an attempt at a diagram.

2    7    3
  5    -4

Removing the 7 means checking if adding together the two differences that 7 was a part of makes for an acceptable difference.

[2,7,8] should also pass, but here it’s a case of dropping the first, rather than the second element. The test that worked for [2,7,3] does not work here, since summing the differences gets us 5+1=6. but here, note that the second chunk [1] is all acceptable. Looking at three consecutive numbers (two consecutive differences) is not sufficient to test for acceptability, though. Consider [1,2,7,3] and [1,2,7,8]. The first is acceptable but the second is not.

My attempts were all working on the sample input. I built up a pretty large collection of tests for edge cases, but I still couldn’t get the right answer. There was some kind of case I was missing, and I didn’t know how to find what it was, short of just looking through all the inputs and testing them all manually. I felt like I was close, but I didn’t really have the mental capacity to think hard enough about all the edge cases to get the solution working quite right. In the end, after days of trying different strategies I gave in, and did the naive thing of just trying to remove each element in turn. I had the right answer in about 15 minutes.

Because I don’t really understand how to handle rust’s various list-like thingies – array, vec, slice… – and because I had given up on all pretence at elegance or efficiency, I simply looped over the length of the sequence, removed the element, did the test, and then inserted the element back into the sequence. I bet there’s a better way to do this, swapping elements in and out instead of doing removals and insertions, but I was out of patience for this problem at this stage.

Now that I know the correct list of sequences, I can create a list of sequences my map/reduce approach fails at, and then I can figure out what edge cases I’m missing. That’s a problem for another time though, because I want to see if I can solve Day 3 before I hit the deadline for Day 4…

Will I learn anything from this experience? Probably not? The point of doing these puzzles is not to solve the puzzle, but to try to solve it in an interesting way. Interesting might mean elegant or efficient or making use of a cool module or something else. I am awarding myself points for style and I will not be explaining that further. I guess the real take-away is that doing the boring solution might provide test cases for the fun solution which could have saved me some time.

© Seamus Bradley 2021–6

Powered by Hugo & Kiss.