part1 (5521B)
1--- Day 16: Chronal Classification --- 2 3As you see the Elves defend their hot chocolate successfully, you go back to falling through time. 4This is going to become a problem. 5 6If you're ever going to return to your own time, you need to understand how this device on your 7wrist works. You have a little while before you reach your next destination, and with a bit of trial 8and error, you manage to pull up a programming manual on the device's tiny screen. 9 10According to the manual, the device has four registers (numbered 0 through 3) that can be 11manipulated by instructions containing one of 16 opcodes. The registers start with the value 0. 12 13Every instruction consists of four values: an [1m[97mopcode[0m, two [1m[97minputs[0m (named A and B), and an 14[1m[97moutput[0m (named C), in that order. The opcode specifies the behavior of the instruction and how the 15inputs are interpreted. The output, C, is always treated as a register. 16 17In the opcode descriptions below, if something says "[1m[97mvalue A[0m", it means to take the number given as 18A [1m[97mliterally[0m. (This is also called an "immediate" value.) If something says "[1m[97mregister A[0m", it means to 19use the number given as A to read from (or write to) the [1m[97mregister with that number[0m. So, if the 20opcode addi adds register A and value B, storing the result in register C, and the instruction addi 210 7 3 is encountered, it would add 7 to the value contained by register 0 and store the sum in 22register 3, never modifying registers 0, 1, or 2 in the process. 23 24Many opcodes are similar except for how they interpret their arguments. The opcodes fall into seven 25general categories: 26 27Addition: 28 29 30 - addr (add register) stores into register C the result of adding register A and register B. 31 32 - addi (add immediate) stores into register C the result of adding register A and value B. 33 34 35Multiplication: 36 37 38 - mulr (multiply register) stores into register C the result of multiplying register A and register 39B. 40 41 - muli (multiply immediate) stores into register C the result of multiplying register A and value 42B. 43 44 45Bitwise AND: 46 47 48 - banr (bitwise AND register) stores into register C the result of the bitwise AND of register A 49and register B. 50 51 - bani (bitwise AND immediate) stores into register C the result of the bitwise AND of register A 52and value B. 53 54 55Bitwise OR: 56 57 58 - borr (bitwise OR register) stores into register C the result of the bitwise OR of register A and 59register B. 60 61 - bori (bitwise OR immediate) stores into register C the result of the bitwise OR of register A and 62value B. 63 64 65Assignment: 66 67 68 - setr (set register) copies the contents of register A into register C. (Input B is ignored.) 69 70 - seti (set immediate) stores value A into register C. (Input B is ignored.) 71 72 73Greater-than testing: 74 75 76 - gtir (greater-than immediate/register) sets register C to 1 if value A is greater than register 77B. Otherwise, register C is set to 0. 78 79 - gtri (greater-than register/immediate) sets register C to 1 if register A is greater than value 80B. Otherwise, register C is set to 0. 81 82 - gtrr (greater-than register/register) sets register C to 1 if register A is greater than register 83B. Otherwise, register C is set to 0. 84 85 86Equality testing: 87 88 89 - eqir (equal immediate/register) sets register C to 1 if value A is equal to register B. 90Otherwise, register C is set to 0. 91 92 - eqri (equal register/immediate) sets register C to 1 if register A is equal to value B. 93Otherwise, register C is set to 0. 94 95 - eqrr (equal register/register) sets register C to 1 if register A is equal to register B. 96Otherwise, register C is set to 0. 97 98 99Unfortunately, while the manual gives the [1m[97mname[0m of each opcode, it doesn't seem to indicate the 100[1m[97mnumber[0m. However, you can monitor the CPU to see the contents of the registers before and after 101instructions are executed to try to work them out. Each opcode has a number from 0 through 15, but 102the manual doesn't say which is which. For example, suppose you capture the following sample: 103 104Before: [3, 2, 1, 1] 1059 2 1 2 106After: [3, 2, 2, 1] 107 108This sample shows the effect of the instruction 9 2 1 2 on the registers. Before the instruction is 109executed, register 0 has value 3, register 1 has value 2, and registers 2 and 3 have value 1. After 110the instruction is executed, register 2's value becomes 2. 111 112The instruction itself, 9 2 1 2, means that opcode 9 was executed with A=2, B=1, and C=2. Opcode 9 113could be any of the 16 opcodes listed above, but only three of them behave in a way that would cause 114the result shown in the sample: 115 116 117 - Opcode 9 could be mulr: register 2 (which has a value of 1) times register 1 (which has a value 118of 2) produces 2, which matches the value stored in the output register, register 2. 119 120 - Opcode 9 could be addi: register 2 (which has a value of 1) plus value 1 produces 2, which 121matches the value stored in the output register, register 2. 122 123 - Opcode 9 could be seti: value 2 matches the value stored in the output register, register 2; the 124number given for B is irrelevant. 125 126 127None of the other opcodes produce the result captured in the sample. Because of this, the sample 128above [1m[97mbehaves like three opcodes[0m. 129 130You collect many of these samples (the first section of your puzzle input). The manual also includes 131a small test program (the second section of your puzzle input) - you can [1m[97mignore it for now[0m. 132 133Ignoring the opcode numbers, [1m[97mhow many samples in your puzzle input behave like three or more 134opcodes?[0m 135 136