Today I looked at my watch and remembered it’s been a while since tackling an AoC puzzle. Especially since I have unfinished business with the 22′ edition. It pinged harder than usual considering that I will turn 32 in a couple of days. I felt like it would be a nice present to myself to take on one of these especially as I’m having my first quiet weekend in forever. Gone are the evenings akin to a party on Grove Street, time to sit down and write some code. Welcome to my Advent of Code 2022 Day 20 cruise log.
Reading the problem prompt, I see that we need to mix some numbers from their order of appearance. After the final sorting, we run some calculations on the data and voila. Despite feeling like this would be an easy one I knew to remain vigilant, especially with latter days puzzles. Not enough at first since I wrote code to solve the puzzle and immediately got a green test. I was quite surprised but it would not be the first that I solve a puzzle on my first attempt.
After running my code against the real input, my IDE hit me with an out-of-bounds exception. This made no sense until I realised that my main function was trying to run the Day 20 code against the Day 19 output. Ah, that will have been a pain in my back even from beyond its shallow grave.
After fixing that input issue, I still got denied upon submitting my answer from the new run. From there, I wrote a more comprehensive acceptance test which would validate each step of the mixing process.
Using that test, I tweaked my code in the dirtiest way possible until the test passed since it is the TDD way. Unfortunately, I forgot that it tends to play against you when doing AoC puzzles. Yet, it took until after another failure with the real input data for me to walk it back.
At that point, I realised that all the tweaking I did in my code probably fixed example-specific issues but may not work on more diverse samples like the real input. So I decided to get to the drawing board, literally.
Recently I picked back up the Visual Doing book which I bought about 4 years ago and abandoned after reading 26 pages. I know the exact number of pages because the page marker was still there.
About a week ago I got back to it but from the start this time. I’m getting the sense that I can improve my communication by getting more visual. I read most of it within a couple of days and decided to finish it this weekend.
The bonus part is that towards the end of the book, there is a section about visual problem-solving. Sure I’ve done that before with pen and paper but can’t be bothered especially with a standing desk. This puzzle gave me the opportunity to put that to good use while practising with Mural so I got to work.
I drew the trickiest scenarios and worked back the behaviour I would expect from the example and updated my code from there.
I started drafting the example data to present the problem differently. From there, I realised that besides the tweaking, my biggest mistake was to try and calculate the new position of an element without removing it first. I’m still surprised that my tweaks worked so well for the example considering how the code looks now. It’s not that different from the original version but it is much cleaner and most importantly, tweaks free. Kind of.
It started to make a different kind of sense which I used to rewrite my logic. The step-by-step acceptance test which I wrote previously didn’t pass right away but after removing enough fluff and fixing my code I eventually got it green again. From there I ran the code against the sample and got my star!
Now onto part two. From the description, it looks like my code had the right structure. All I need to do is apply some long multiplier on each value before going through the mixing process. Despite the looming impression that my values might go above int max I still decided to ignore that unless the part two execution results in a negative value.
I started writing my acceptance test to check the state after one round using the new conditions and immediately realised that I should have followed my gut.
Noticed anything? The values seem to be in the right place except that some values that should be really big are negative. The only explanation is that my integers overflowed. You know the thing I decided to ignore from the last paragraph. Life came at me fast on this one. Fortunately, this would be my toughest challenge today.
After a few more minutes of fiddling around by replacing my integer values with BigInteger values, I got that mini-acceptance test to pass.
From there, I assumed that the rest will probably work so I just ran the whole part two test without writing the other step checks and it was green too. Pfew! I really didn’t want to do all that copy-pasting to have these large values checked.
Feeling confident, I decided to run the code for the whole input. It worked! And just like that, I collected the final star of Day 20. Since I already completed Day 21 back in December, next time I’ll take on Day 22.
Thank you for reading my Advent of Code 2022 Day 20 log, I will see you when I see you. If you want to check out my previous entry, you can do so here. You can even read my entries from last year there. As usual, I will push the code to my repo for this year on Github. Also, if you want to test yourself against my AoC 2022 run, I’ve created a private leaderboard. The invite code is
382952-d065ee7a . Join, if you dare. I say dare but since I’m playing catch up, if you completed everything on time, you’re way ahead.
Cover by Edoardo Tommasini