Advent of Code 2019

The story of my slow descent into madness

View on GitHub
7 December 2019

Day 7: Amplification Circuit

by Simon Berger


Links:

Yeah alright, here we go again. I may sound a bit unenthusiastic, but that’s only because I was forced to extract the intcode interpreter logic to a separate module so as to avoid code repetition. I didn’t touch the code from day 2 though, I think that can still stand on its own.

The first part of today’s puzzle was pretty straightforward. The hardest part was generating the phase setting permutations, because Rust doesn’t (yet) come with a built-in permutations generator. Of course I didn’t bother coming up with my own algorithm, instead I went with Heap’s algorithm. Now, the hard part wasn’t translating the pseudo-code to Rust, well, in a sense it was. I mistook a very important part of the algorithm (namely the continuous mutation of the array in the swap loop) and ended up with only 32 unique permutations instead of 120. It took me quite some time to even notice and fix, as I had to rewrite the algorithm from scratch.

The rest of part 1 didn’t pose any additional problems. Apart from extracting the intcode interpreter to a common module, there was no need for additional changes, as each of the amplifiers could be run in sequence, with no need for dynamic input. To get the solution the code tries each of the 120 possible phase settings and finally outputs the highest output signal.

For the second part, however, a few adjustments needed to be made to the interpreter. In hindsight I should’ve turned the interpreter into a generator much like those used by Python. At the end of the day I did go with something similar, but because I just hacked the functionality into the existing interpreter I ended up with some weird features like input / output buffers and no real state management. Nevertheless it works flawlessly so there’s no real need to change it (yet?). The approach here is pretty much the same as for the first part, only there are two steps. First, all amplifiers are fed their phase setting along with the input and then, in a second step, the amplifiers are repeatedly fed the input until the last amplifier’s intcode interpreter halts.

Now that I’ve extracted the interpreter code, I do hope that it comes back for another round. It’s a great idea to have this re-occurring piece of logic that keeps on being expanded.

I would like to add a function to convert intcode to a human readable format in the style of Assembly for the next time. This way one could inspect what the code does. I have no interest in using this strategy to actually solve the puzzles, I intend only to satisfy my curiosity.

tags: intcode
Overview
Previous day Next day