New day, same thoughts, just like yesterday I know that this puzzle took me longer than it should have. Today’s puzzle statement felt deceptively easy. Or maybe it was easy and coffee hadn’t sunk its claws into my brain just yet. Welcome to my Advent of Code 2022 Day 8 log.
In this one, the little magical elves wanted us to figure out the best spot to build a cabin in the woods. As expected, this puzzle is easier than yesterday but somehow my global ranking raised to the top 6000 as opposed to yesterday when I barely made it to the top 10000. Enough stat sneaking, let’s go through the process.
The gist of it is that we need to create a map of trees by their height and I chose to represent it as a two-dimensional array of integers. At first, I wanted to write something fancy with streams. But then I remembered after a couple of failed attempts that I am no master of streams and got a company top spot to preserve. From there, I just wrote loops. Heck, maybe there was no other way than loops here but I feel like there was.
Once I got my map, I can check what tree is visible from the edges of the map. That’s about it for that first part. I just had to iterate over my map and for each coordinate, I had a subroutine going in each direction to validate that the tree was visible from at least one of the edges. Fairly straightforward.
Unfortunately, I did play myself a couple of times by running my test with an empty test input file and inverting comparison operators. However, I eventually got my test passing. At this point, I submitted my answer and captured the fifteenth star of the season. All under 30 minutes. Now onto part two.
The second part felt like it would be easier, and it should have been. The logic very much matched the first part. However, I played myself a second time by misplacing the updating bit of a certain variable (not spoiling). The worst part is that I felt like this was the issue but last night’s coding dojo we did in the office must have contaminated me. It was about TDD.
Instead of slinging as I usually do, I took things slow. I wrote a smaller test, capturing the validation of a certain result. It failed. Then I wrote an even smaller, more localised parameterised test. It failed as well.
At this point, I enter debugging mode to figure out what went wrong. After a good twenty minutes, I realise it was exactly what I thought it was before going through all this. That one updating bit of variable update occurring after instead of before a certain evaluation. After fixing it, all the tests went through. Finally, I captured my final point of the day after submitting my findings.
While I did just moan about using TDD on an Advent of Code puzzle, more often than not it saves time. I could have stumbled from one bug to another without noticing otherwise. Even though I might not have needed to go through that painstaking process today, it will help in the latter days.
Thank you for reading my Advent of Code 2022 Day 8 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 Angela Cavina from Pexels