part1 (3834B)
1--- Day 2: 1202 Program Alarm --- 2 3On the way to your gravity assist around the Moon, your ship computer beeps angrily about a "1202 4program alarm". On the radio, an Elf is already explaining how to handle the situation: "Don't 5worry, that's perfectly norma--" The ship computer bursts into flames. 6 7You notify the Elves that the computer's magic smoke seems to have escaped. "That computer ran 8[1m[97mIntcode[0m programs like the gravity assist program it was working on; surely there are enough spare 9parts up there to build a new Intcode computer!" 10 11An Intcode program is a list of integers separated by commas (like 1,0,0,3,99). To run one, start 12by looking at the first integer (called position 0). Here, you will find an [1m[97mopcode[0m - either 1, 2, or 1399. The opcode indicates what to do; for example, 99 means that the program is finished and should 14immediately halt. Encountering an unknown opcode means something went wrong. 15 16Opcode 1 [1m[97madds[0m together numbers read from two positions and stores the result in a third position. 17The three integers [1m[97mimmediately after[0m the opcode tell you these three positions - the first two 18indicate the [1m[97mpositions[0m from which you should read the input values, and the third indicates the 19[1m[97mposition[0m at which the output should be stored. 20 21For example, if your Intcode computer encounters 1,10,20,30, it should read the values at positions 2210 and 20, add those values, and then overwrite the value at position 30 with their sum. 23 24Opcode 2 works exactly like opcode 1, except it [1m[97mmultiplies[0m the two inputs instead of adding them. 25Again, the three integers after the opcode indicate [1m[97mwhere[0m the inputs and outputs are, not their 26values. 27 28Once you're done processing an opcode, [1m[97mmove to the next one[0m by stepping forward 4 positions. 29 30For example, suppose you have the following program: 31 321,9,10,3,2,3,11,0,99,30,40,50 33For the purposes of illustration, here is the same program split into multiple lines: 34 351,9,10,3, 362,3,11,0, 3799, 3830,40,50 39 40The first four integers, 1,9,10,3, are at positions 0, 1, 2, and 3. Together, they represent the 41first opcode (1, addition), the positions of the two inputs (9 and 10), and the position of the 42output (3). To handle this opcode, you first need to get the values at the input positions: 43position 9 contains 30, and position 10 contains 40. [1m[97mAdd[0m these numbers together to get 70. Then, 44store this value at the output position; here, the output position (3) is [1m[97mat[0m position 3, so it 45overwrites itself. Afterward, the program looks like this: 46 471,9,10,[1m[97m70[0m, 482,3,11,0, 4999, 5030,40,50 51 52Step forward 4 positions to reach the next opcode, 2. This opcode works just like the previous, but 53it multiplies instead of adding. The inputs are at positions 3 and 11; these positions contain 70 54and 50 respectively. Multiplying these produces 3500; this is stored at position 0: 55 56[1m[97m3500[0m,9,10,70, 572,3,11,0, 5899, 5930,40,50 60 61Stepping forward 4 more positions arrives at opcode 99, halting the program. 62 63Here are the initial and final states of a few more small programs: 64 65 66 - 1,0,0,0,99 becomes [1m[97m2[0m,0,0,0,99 (1 + 1 = 2). 67 68 - 2,3,0,3,99 becomes 2,3,0,[1m[97m6[0m,99 (3 * 2 = 6). 69 70 - 2,4,4,5,99,0 becomes 2,4,4,5,99,[1m[97m9801[0m (99 * 99 = 9801). 71 72 - 1,1,1,4,99,5,6,0,99 becomes [1m[97m30[0m,1,1,4,[1m[97m2[0m,5,6,0,99. 73 74 75Once you have a working computer, the first step is to restore the gravity assist program (your 76puzzle input) to the "1202 program alarm" state it had just before the last computer caught fire. To 77do this, [1m[97mbefore running the program[0m, replace position 1 with the value 12 and replace position 2 78with the value 2. [1m[97mWhat value is left at position 0[0m after the program halts? 79 80