Hello everyone, it’s time for your Advent of Code 2021 Day 11 cruise log. Today I decided to spend the whole morning with my girlfriend regardless of finishing the challenge. With all the preaching I do about the importance of taking breaks I have been lacking the practice recently. We know that as days progress if we’re stuck on a coding problem or challenge we tend to believe we’re five minutes away from cracking it. Biggest lie ever.
With that in mind, waking up around 8 am did not put me in the best position to complete this in the morning. Still, I went for it. She’s still sleeping so I can technically try and beat Day 11 before she wakes up. Then I start reading the problem statement. We’re surrounded by a hundred octopuses and maybe it’s an easter egg for hentai aficionados. There is something about these changing states and sparking light at a certain count. Indeed, it is reminiscent of Day 6 so for a second which is all good since we already solved this. However, before jumping to conclusions, I decided to peek at the real input.
As it turns out, it also is a 10×10 grid just like the sample. This decided me to write a minimal effort code to solve just part one so that it is simple enough to adjust for part two. Once I finished writing the code I ran my tests and they went red. It felt odd since I was pretty sure I understood the assignment this time.
At this point, I start writing a smaller test to make sure the right octopuses go into a flashing state. For that, I use the smallest example which showcases the chain flashing scenario. I figured this must be the issue. Nope, that test goes green. Since I’m way over the value I should get for the first test that went and still is red, I try to break the scenario down.
Fortunately, the problem statement gives various states of the grid. I used these to make sure that as we iterate through the steps we got the right amount of flashes. Running for no step, one step, two steps it’s all green. The third step test goes red. Then I remember something, I always updated the octopus state even when I had to skip it. I did so because while reading the statement I understood the state always increases as in, each time you visit an octopus the state changes but nope. When the octopus flashes, his state cannot change anymore even if it is in another flash’s radius.
Upon that realisation, I changed my code and all the tests went green. In total, it took about under thirty minutes to unlock my first star of the day. Then I read the second part. That part required to wait until all the octopi flash in sync and return the number of the step when it happens. Super easy, barely an inconvenience. Since I thought my girlfriend might wake up at any moment I went and copied/pasted my iteration method from part one to make it an infinite loop. An infinite loop that breaks once all the octopi flash in sync. Super easy, barely an inconvenience. And yet, the test failed. Looking at the value I got for the sample data, I noticed I was off by one.
This is where I realise that I start the loop with the first counting step being zero rather than one. Once I fixed that, I got the test going green and went on to run the code against the real input. And there it was, my twenty-second star of this Advent of Code season.