part1 (4980B)
1--- Day 5: Sunny with a Chance of Asteroids --- 2 3You're starting to sweat as the ship makes its way toward Mercury. The Elves suggest that you get 4the air conditioner working by upgrading your ship computer to support the Thermal Environment 5Supervision Terminal. 6 7The Thermal Environment Supervision Terminal (TEST) starts by running a [1m[97mdiagnostic program[0m (your 8puzzle input). The TEST diagnostic program will run on your existing Intcode computer after a few 9modifications: 10 11[1m[97mFirst[0m, you'll need to add [1m[97mtwo new instructions[0m: 12 13 14 - Opcode 3 takes a single integer as [1m[97minput[0m and saves it to the position given by its only 15parameter. For example, the instruction 3,50 would take an input value and store it at address 50. 16 17 - Opcode 4 [1m[97moutputs[0m the value of its only parameter. For example, the instruction 4,50 would output 18the value at address 50. 19 20 21Programs that use these instructions will come with documentation that explains what should be 22connected to the input and output. The program 3,0,4,0,99 outputs whatever it gets as input, then 23halts. 24 25[1m[97mSecond[0m, you'll need to add support for [1m[97mparameter modes[0m: 26 27Each parameter of an instruction is handled based on its parameter mode. Right now, your ship 28computer already understands parameter mode 0, [1m[97mposition mode[0m, which causes the parameter to be 29interpreted as a [1m[97mposition[0m - if the parameter is 50, its value is [1m[97mthe value stored at address 50 in 30memory[0m. Until now, all parameters have been in position mode. 31 32Now, your ship computer will also need to handle parameters in mode 1, [1m[97mimmediate mode[0m. In immediate 33mode, a parameter is interpreted as a [1m[97mvalue[0m - if the parameter is 50, its value is simply 34[1m[97m50[0m. 35 36Parameter modes are stored in the same value as the instruction's opcode. The opcode is a two-digit 37number based only on the ones and tens digit of the value, that is, the opcode is the rightmost two 38digits of the first value in an instruction. Parameter modes are single digits, one per parameter, 39read right-to-left from the opcode: the first parameter's mode is in the hundreds digit, the second 40parameter's mode is in the thousands digit, the third parameter's mode is in the ten-thousands 41digit, and so on. Any missing modes are 0. 42 43For example, consider the program 1002,4,3,4,33. 44 45The first instruction, 1002,4,3,4, is a [1m[97mmultiply[0m instruction - the rightmost two digits of the first 46value, 02, indicate opcode 2, multiplication. Then, going right to left, the parameter modes are 0 47(hundreds digit), 1 (thousands digit), and 0 (ten-thousands digit, not present and therefore zero): 48 49ABCDE 50 1002 51 52DE - two-digit opcode, 02 == opcode 2 53 C - mode of 1st parameter, 0 == position mode 54 B - mode of 2nd parameter, 1 == immediate mode 55 A - mode of 3rd parameter, 0 == position mode, 56 omitted due to being a leading zero 57 58This instruction multiplies its first two parameters. The first parameter, 4 in position mode, 59works like it did before - its value is the value stored at address 4 (33). The second parameter, 3 60in immediate mode, simply has value 3. The result of this operation, 33 * 3 = 99, is written 61according to the third parameter, 4 in position mode, which also works like it did before - 99 is 62written to address 4. 63 64Parameters that an instruction writes to will [1m[97mnever be in immediate mode[0m. 65 66[1m[97mFinally[0m, some notes: 67 68 69 - It is important to remember that the instruction pointer should increase by [1m[97mthe number of values 70in the instruction[0m after the instruction finishes. Because of the new instructions, this amount is 71no longer always 4. 72 73 - Integers can be negative: 1101,100,-1,4,0 is a valid program (find 100 + -1, store the result in 74position 4). 75 76 77The TEST diagnostic program will start by requesting from the user the ID of the system to test by 78running an [1m[97minput[0m instruction - provide it 1, the ID for the ship's air conditioner unit. 79 80It will then perform a series of diagnostic tests confirming that various parts of the Intcode 81computer, like parameter modes, function correctly. For each test, it will run an 82[1m[97moutput[0m instruction indicating how far the result of the test was from the expected value, where 0 83means the test was successful. Non-zero outputs mean that a function is not working correctly; 84check the instructions that were run before the output instruction to see which one failed. 85 86Finally, the program will output a [1m[97mdiagnostic code[0m and immediately halt. This final output isn't an 87error; an output followed immediately by a halt means the program finished. If all outputs were 88zero except the diagnostic code, the diagnostic program ran successfully. 89 90After providing 1 to the only input instruction and passing all the tests, [1m[97mwhat diagnostic code does 91the program produce?[0m 92 93