Hello, there and welcome to my Advent of Code 2020 Day 22 cruise log. The day 20 post-mortem shall wait, maybe even until after the AoC completes. Trying to keep my mind fresh and away from code for most of the day so I don’t spend more than a couple of hours on it. However, this doesn’t stop me from writing daily logs for other days so let’s check what we did today.
First the lore, after finding food for our trip, we now need to entertain ourselves on that raft. Luckily we have some space cards with us and even a crab to join the fun. In the first part, we taught the crab how to play combat. Basically, we each have a deck of cards and we play the card on top each round. When a player runs out of cards, the other player wins.
Full disclosure, I only notice as I write that the lore highlights the words “space cards” with a link to last year’s day 22. I’m so glad I didn’t see that otherwise, I could have sweat much more on this. 2019’s day 22 is the day which took a final toll on me back then, while the first part went fine, I got stuck on the second. Getting stuck there I saw my company’s top 5 positions get out of reach and it put an end to my 2019 Advent of Code.
Back in the present day. This time, I felt confident enough to avoid writing small tests and only write the big test for part one. After mentally parsing the rules, I start the write some code, a doubly-linked list. What sort of crazy person still write their own doubly-linked list in 2020, especially in the middle of the Advent of Code out of all times? After wasting a good ten minutes, I realise that I don’t need to and just using some slice magic and the append keyword I got all I need.
From there I create my deck class which represent a player’s deck and a combat class which represents the game. Basically, we create the decks, run the game and compute the winner’s score from the subject rules. This goes quite smoothly and my single test from the example is green on the first pass. There is always some air of surprise to my face when that happens, which is often enough. At that point, I run the code against my input and land my first star of the day. Now onto part two.
After the crab’s victory in the first part, we think we can outsmart him by playing recursive combat in the second. It’s basically the same rules as “la bataille” a french card game. You can either learn french and check what it is here. Alternatively, you can go the AoC website for this year and open day 22, won’t link as I need that SEO power.
Back to the game. While I felt lucky I didn’t pay attention to the lore so much, missing bits of the part two problem cost me time. I just didn’t know yet. As often, it is written in such a way that it confuses you. When you read rules to write a program, your (at least my) brain tends to check these rules in the reading order when writing the code. This caused me all kind of trouble. Mostly for the subgame detection, the subgame decks and the infinite loop prevention. If you haven’t done the challenge yet, make sure you triple check these rules. If you don’t chances are you will spend ages debugging that code of yours. It gets worse if you need to refactor to fit the first part code to your second part requirements.
Eventually, after some of the finest debugging tool known to man a.k.a printing stuff, I found the right key to unlock my 44th star this year. I will now have me some breakfast, relax and get back on that day 20 post-mortem of mine. I hope to see you tomorrow for day 23.
Thank you for reading my Advent of Code 2020 Day 22 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. Feel free to check it out but not before you’ve done the challenge yourself. Bye!