Hello, there and welcome to my Advent of Code 2020 Day 11 cruise log. Today I woke up at 5 am, late I know, with a piercing headache. If there is anything you don’t want especially during a latter Advent of Code day, it’s a headache. Problems themselves can cause headaches when given enough attention. Today’s problem doesn’t feel like it would have though. In fact, that was just some unnecessary pain that may or may not have impaired my coding.
After dragging myself out of bed, I walked to my office in the dark. Upon hitting the switch a wave of pain joined the piercing party that kept me company since waking up. Not good. Should I go back to bed, get some pain killers maybe? Nope, maybe just like House M.D. the pain will help me think today. Maybe I told myself that only for the laziness of not going to the kitchen to grab some water and pills. Eventually, I sat down, turned on my computer and started creating my files for today.
Enough complaining about my fragile mortal shell, and onto the brain games. Today’s challenge was based on a 2-dimensional array search and update algorithm followed by a count. The array would represent a map of seats which we would update following principles akin to Conway’s game of life. Relatively straightforward unless you bring bugs into your code for say, lack of sleep, headaches or general mind absence. In the first part, the search involves the directly adjacent seats.
The algorithm itself, or at least its shape is quite straightforward. As long as at least a single seat gets modified from the given rules, we can have another tick of life.
As you would expect, the first part did not work as expected. I basic test validating the whole method failed. And yet, some other tests I wrote on specialised methods passed. I must be hitting some cases I forgot to consider. After some further refinement that involved more tests to write. After running these additional tests, I realised that I was resetting a condition which resulted in ending my loop one round too soon.
It did take me an hour to get to that conclusion with a lot of manual scribbling of what the seat map should look like after each round. Once that bug fixed from a smaller test I go back to the example test. It still fails, but this time I’m getting a result higher than expected. Now I’m printing the map state between each round. After the second round, I’m getting a state that doesn’t match the subject example. Certain seats should be empty and are not.
The seat filling logic is sound and very much under lock so it can’t be the issue. Now I focus on the seat detection algorithm. I go back to the subject and realise something I hadn’t. I’m updating the map in real-time and the checks are based on previous values. After that eureka moment, I update my seat update method into an update check that determines whether the code should update a seat and by which value. This would lead me to create a new map at each tick but it’s worth it. After implementing that change all the tests pass. I run the input against my algorithm and it gives the right result. Time to hit part two. But first I needed a break because that headache is killing me.
After a good ten minutes stretching in the dark, the ache is still there. But I need to move on and close today’s challenge.
Now the rules to fill/empty a seat were a bit looser but the core remained pretty much the same. I did a minor refactoring where I would inject the method determining whether a seat location should be updated. By doing that, I allowed myself to reuse the same code as part one but only change the conditions upon which a seat is modified. I applied that change and ran the tests covering the first part and they still pass. Good. Now, let’s add new tests for part two.
I obviously started with the test validating the example result but it instantly failed with a suspiciously low number. Something else that changes in part two is that the seats to check aren’t directly adjacent. They’re seat you would see on a straight line or diagonal. I add some tests, they pass. I run against my input, it fails. Disappointment emerges for but a second.
At that point, I went back to pen and paper. That’s where I noticed. Something goes wrong from the second tick with my now larger small test. There, I’m guessing that something must be wrong with my visible seat detection. After creating new tests for all
After some debugging, I realise that all the seats on the left that should be removed on the second are still there. I write a new seat detection test with a filled grid. Now I realise that my code never explores the top-left diagonal seats which is why we’re in the bound to maintain these seats. By one.
After implementing that final solution, I rerun the part two test again, and it passes. I run all my tests, one fails. After careful examination, the test was based on an invalid assumption so I removed it. I ran the input against my code and it passed. Almost three hours in this nightmare of headache and sneaky bugs. At last, I can walk away. Too late to workout before office hours but just enough time to enjoy breakfast with my girlfriend. I can still work out after work but for now, I will have a pint of water and hope it takes that headache away. Hopefully, tomorrow will be a better day where this pain is nothing but a distant memory. One thing is for sure though, I’m never looking at that code ever again.
Thank you for reading my Advent of Code 2020 Day 11 cruise log, I will see you tomorrow, maybe. In the meantime, you can check out my Go Cloud series about building and deploying a Golang app to AWS. Also, I will push my code on Github at CodingNagger/advent-of-code-2020 so feel free to check it out but not before you’ve done the challenge yourself. Bye!