Hey guys, I feel like I should have stayed in bed longer today. As per the last few days, I woke up at 4.50 am. To complete my awakening I took ten deep breaths and then downed an espresso. Still, I feel like this was a waste of sleep. Why? Well, find out by reading the rest of my Advent of Code 2022 Day 14 log.
As I type this a headache takes over me so I’ll make it as short as possible to have a break before getting to work. Fortunately, today I’ll work from home. Right, the puzzle. The puzzle input is pretty simple, we get a collection of vectors which represent lines of rocks in a sort of labyrinth.
There is a location pouring sand out within that labyrinth and we need to figure out the sand movements so that our character does not get trapped. Pretty straightforward. And still, I found a way to complicate it.
This is one of these times where the solution judgementally stared at me while I took the longest possible route. A terrible reflex we have as developers is to try and optimise code too early for our own good. Also, I kinda forgot about recursive solutions even though the problem was screaming to go for one.
Nah, I went for loops. All the way until I got trapped. Despite that flaw, I managed to get something that almost worked. No, actually I got something that worked for the simple input by cheating a little. I added a sneaky null check to get my code to exit at the right spot. The test passed but my confidence this would work with the real input file was very low.
There is one very important thing that I’ve learned again in this Advent of Code. If there are visuals representing the first few states that your solution should generate, write some code to generate that visual for debugging. Fortunately, I did that earlier when going through the painful iterative road.
From there I ran my code against the real input file and the final visual felt wrong. I could see lines of rocks not getting any sand, so surely something was amiss in my solution. Looking at the code I had so far I felt trapped. Once again, I played myself because the only way out is from scratch. At least for the sand movement detection code.
At this, I delete that whole detection thing and go back to read the problem statement. I read it a second time, then a third and fourth times. Finally, I hear them, the voices, the screams. Use recursion, you idiot! How did I not see that earlier? Well, now that I noticed, I can start the real work. It only took over three hours to get to that point.
From there, I got a solution pretty quickly. In under ten minutes, I wrote code that passed the validation test and felt right when executed against the real input file. At last, I claimed my first star of the day!
Now onto the second part. A new parameter that changed everything. And yet, I barely had to change anything. But the little I had to change did take me a while, as I tried to figure out the best angle to translate that change into code. All the while attempting not to change much of the code.
Eventually, once I fully grasped what that change meant, I realised that indeed I could end this soon. All I needed was to add some conditional here and there to accommodate that new requirement, and the validation test instantly passed. Strong with that newfound confidence, I went and claimed my second star of the day.
Thank you for reading my Advent of Code 2022 Day 14 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. 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.
Cover by Jordan Benton