Source: Day 17: Chronospatial Computer
Full solution for today (spoilers!).
Part 1
Implement a virtual machine. The machine will have 3 unbounded signed registers, 8 opcodes (see below), a variable parameter scheme (see below that). You will be given the initial values of the 3 registers and a program. Find the final output.
Instructions
| Opcode | Instruction | Description | Notes |
|---|---|---|---|
| 0 | adv reg/val | A = A >> OP | |
| 1 | bxl val | B = B ^ OP | |
| 2 | bst reg/val | B = OP & 0b111 | |
| 3 | jnz val | If a =/= 0, jump to LIT | |
| 4 | bxc ignore | B = B ^ C | Still takes param, but ignores it |
| 5 | out reg/val | Output b | Only outputs lowest 3 bits |
| 6 | bdv reg/val | B = A >> OP | Same as adv but writes to b |
| 7 | cdv reg/val | C = A >> OP | Same as adv but writes to c |
Parameter specification
For instructions that can take reg/val, 0 to 3 (inclusive) are treated as literal values, 4 is register A, 5 is B, 6, is C, and 7 is an error (should never happen).
For instructions that only take val, it’s always a literal value in the range 0 to 7 (inclusive).