AoC 2019 D7: Amplification Circuit
| Problem statement | Source code | Tags: IntcodeBrute force
← Previous Back to AoC Index Next →
This time we are introducing stateful programs, where each one needs to be paused and resumed (especially for part 2). So instead of my functional run_prog from day 5, I wrapped it into a class:
Now the execution state (instruction pointer, input/output queues, halted flag) is stored in the object. The program can be paused when an output is produced (run_until_output()), or run to completion (run()). When paused, the outputs can be popped and inputs can be sent in. I have to expose the memory (codes) because the caller sometimes needs to read from it (e.g., day 2 when there was no output) or modify it (e.g., setting flags).
Part 1
Conveniently, C++ has std::next_permutation to generate all permutations of the phase settings. So the main loop looks like:
For each permutation, we just construct 5 Program instances one by one, send in the phase setting and previous output, run to completion, and read the final output. It looks like this:
Part 2
We need to run the programs in a loop until they all halt. It looks like this:
Note, technically this may terminate in the middle of a cycle if one program halts, but it looks like the correct output is produced regardless.