After a fun wine-centric evening with a friend, I returned to the world of coding the next day. This one I recall solving the first part with some dodgy calculations that relied on cubes having six faces. Today I revisited this with the intent of completing part two. Welcome to my Advent of Code 2022 Day 18 cruise log.
On December 18th last year, I did go and write some code. I remember taking on this puzzle the morning preceding the World Cup final. My memory tells me that I solved the first part pretty quickly then got stumped on the second half. Not sure why I didn’t complete it that day. I would guess that celebrating that friend’s birthday the whole day before did not help.
Here we are, a few weeks later, after a friendly wine-drinking evening. Looks like I repeated the same mistake but nope. Today my mind is as sharp as ever and I am determined to end this puzzle today.
My first attempt saw me taking on a whole different route to solving this. I replaced my dodgy code with a BFS implementation. While it did not give the result I expected, it yielded the result of my first part implementation hence the replacement. Everyone loves ditching filthy code for something a little more elegant.
From there I wrote a bunch of tests based on simple setups and manual calculations like the one below:
Thanks to the extra tests, even when my validation test passed, the failure of the extra tests showed me there was still work to be done. Still, just in case I tried to run the code against the real input. As expected, it failed.
The BFS switch made me look at the problem in a different light. Since we’re trying to determine the external surface of lava droplets, I could write code exploring the air outside of that droplet. However, for the second part, we need to ignore the air trapped inside the lava. This looked easy at first but was trickier than I pictured.
Since my final solution looked good on paper, I went back to coding. Maybe about half an hour later, I got something that felt right. My algorithm would collect all the lava blocks at the edge of the droplet, then count the sides that touched the filled air.
All the tests passed! But my input-based answer was still wrong.
At that point, I’m ready to call it a night since I still need to work out before 9 pm. I go through my code once more, I must have missed something but everything looks good. Or almost. I realised that some lava blocks on the outside could be neighbours with inner trapped air. So I need my code to exclude the air blocks outside of my filled air set and it worked!
Now that I completed this way before 9 pm, I took the liberty of writing this entry before heading to do some heavy cardio. It’s gonna be even more satisfying now that I put this puzzle behind me.
Thank you for reading my Advent of Code 2022 Day 18 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 Daniel Torobekov