Stacks, everybody loves stacks. Stacks of burgers, stacks of pancakes, stacks of cash, all sorts of stacks. Today, stacks got me to think a little harder that I should at this time of the day. Time for my Advent of Code 2022 Day 5 cruising log.
This is a strange start. Ever got that feeling when, something looks deceptively easy. You can see what the solution is, and yet it keeps evading you? That’s pretty much what happened today. The problem is quite straightforward, we need to parse and process some input. Some of the input contains the instructions but I assumed the trickiest bit was to parse both the initial state and input.
Oh boy, was I wrong. The trickiest part is not mentally figuring how specific stack implementation are structured internally. Also not using methods to pick at the topmost value because IntelliJ’s debugger feels faster to use. Double wrong.
These two mistakes cost me dearly and it took me over an hour to get over this. First, I had to parse the input which was a tad tricky but within a good half hour I had the code working. Or so I thought, when came time to apply the parsed instructions I entered a world of pain.
At first, I thought that I messed up the instructions application step but it all looked fine. I went over the code quite a few times, debugging it line-by-line. The IntelliJ preview seemed to reveal that my stacks were good so I didn’t think twice about it. But then when the time came to apply instructions, it all came down crashing.
Eventually, I realised I must have made a mistake in the parsing. As it turns out I did. When parsing the input, I get the stack elements from the top to the bottom. And somehow I forgot to flip the stacks afterwards. From there I decided the clever thing was to parse the data using a queue and then use the queue structure to store the data in the right order.
I tried to be clever, alas too clever for my own good. For some reason this didn’t work and I resorted to using intermediary stacks before flipping them. And flipping them I did. From there, the data looked fine before and after applying the parsed instructions. I run my unit test and it goes green. From there we do the usual and I get my first star of the day.
Then I start reading the second part of the puzzle. It’s about the same expect that the way we transfer elements from a source to a destination changed. Instead of moving elements one by one, we move them by group.
Fortunately, the class I created to represent the problem had a method handling just that. All I needed from there was to extend that class, override the method and voila! Tests go green, second star and the satisfaction of still sitting atop my company’s Advent of Code leaderboard. While participating is fun, leading is better.
Thank you for reading my Advent of Code 2022 Day 5 log, I will see you tomorrow. If you want to check out my previous entry, you can do so there. As usual I will push the code to my repo for this year on Github.
Cover by El Capra