Oh boy today hurt my feelings quite a bit. Since I did not finish the challenge this morning and continued only after work, let’s skip the fancy intro. Let’s jump right into this Advent of Code 2022 Day 9 cruise log.
While I expected today to be trickier than yesterday, I did not think it would cause more trouble than Day 7. But then, the fool in my brain tricked me into thinking that one could simple represent rope physics as code. At least the problem statement gave me a fair starting point so not much to complain about that.
Still, putting my best 5 am face on, I got ahead and within an hour I implemented some code. The code felt solid enough since the validation test passed. However, the AoC gods denied me.
As it turns out I misplaced a certain value and within ten minutes I rectified the mistake.
Now that I got my first star of the day, I can try and take on part two. The first part teased me something that could be entertaining yet possibly frustrating. I saw much more of the latter unfortunately but I am 100% to blame.
I had the funny feeling that to succeed in implementing the second part of the puzzle I would need some sort of visual aid. Somehow, I came up with the worst possible way of visualising coordinates: By writing them as a list which you can see below.
Heck, I even managed to get code that satisfied the requirements and passed all the tests from the sample inputs. All before 7 am struck. And yet, the output processing failed. I could not see why.
I tried over and over for the next two hours but nothing, I kept going in circles. Fortunately, the working day was about to provide a much needed break from this frustrating puzzle.
Later in the day, around 6.30 pm, I got back to my personal laptop to continue the challenge. I read the description again. And again. This is when I notice the words “you might want to visually compare your simulated rope to the one above”. VISUALLY. As in creating a visual representation of the knots and generating a visual map in the terminal.
At that point, I wanted to slam my laptop and throw it on the ground but instead I started coding. Fortunately, writing the visualisation code took me only half an hour. Now running the tests again, I get the right result but realise it’s accidental.
As it turns out, the visualisation was helpful. I could see that my simulation did not take the rope knots in all the right places. And yet the final calculation yielded the right result. Progress, at last.
From there I worked my way through the visual examples while trying to get my code to provide a similar outcome. After another thirty or forty minutes, I finally got my tests to return to green and the visualisation matched the problem’s statement. Having gained some confidence once more I submit a new answer and it’s wrong. Again.
The difference now is that I can see the visualisation of the rope tail locations. A proof that my code works fine would be that we would see no gaps between the various tail locations. And yet, there were plenty when running my code against the real input. What makes it even more frustrating is that there were absolutely no gaps when running it against test inputs.
At that point I decided to mostly bruteforce a fattest if-else induced piece of code I’ve written in a long time. The first step was to delete all the moving logic I implemented so far and go back to the basics. I would implement each step one by one and ensure they did exactly what I want. I ended up with the same results, but somehow the code was more readable.
Since I get the same results I need a new strategy. I decide to add some filthy if statement after executing each step to ensure that there are no gaps from one knot to the next. I figured to would help me find the gaps and write extra conditions to handle them.
Thanks to this bit, the code would break for each gap and my other debugging lines allowed me to see exactly where. Now we’re cooking and within ten minutes I wrote all the edge cases to handle the remaining gaps.
I run the code once more, and no gaps. I run all my day 9 tests, all green, no gaps. Time to submit a new answer and this is it. Finally, I am done and will never revisit this piece of
shit code again.
Thank you for reading my Advent of Code 2022 Day 9 log, I will see you tomorrow. 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.
Cover by Pexels User Kostina