Advent of Code Day 2

Part 1 prompt

— Day 2: 1202 Program Alarm —
On the way to your gravity assist around the Moon, your ship computer beeps angrily about a “1202 program alarm”. On the radio, an Elf is already explaining how to handle the situation: “Don’t worry, that’s perfectly norma–” The ship computer bursts into flames.

You notify the Elves that the computer’s magic smoke seems to have escaped. “That computer ran Intcode programs like the gravity assist program it was working on; surely there are enough spare parts up there to build a new Intcode computer!”

An Intcode program is a list of integers separated by commas (like 1,0,0,3,99). To run one, start by looking at the first integer (called position 0). Here, you will find an opcode – either 1, 2, or 99. The opcode indicates what to do; for example, 99 means that the program is finished and should immediately halt. Encountering an unknown opcode means something went wrong.

Opcode 1 adds together numbers read from two positions and stores the result in a third position. The three integers immediately after the opcode tell you these three positions – the first two indicate the positions from which you should read the input values, and the third indicates the position at which the output should be stored.

For example, if your Intcode computer encounters 1,10,20,30, it should read the values at positions 10 and 20, add those values, and then overwrite the value at position 30 with their sum.

Opcode 2 works exactly like opcode 1, except it multiplies the two inputs instead of adding them. Again, the three integers after the opcode indicate where the inputs and outputs are, not their values.

Once you’re done processing an opcode, move to the next one by stepping forward 4 positions.

For example, suppose you have the following program:

1,9,10,3,2,3,11,0,99,30,40,50
For the purposes of illustration, here is the same program split into multiple lines:

1,9,10,3,
2,3,11,0,
99,
30,40,50

The first four integers, 1,9,10,3, are at positions 0, 1, 2, and 3. Together, they represent the first opcode (1, addition), the positions of the two inputs (9 and 10), and the position of the output (3). To handle this opcode, you first need to get the values at the input positions: position 9 contains 30, and position 10 contains 40. Add these numbers together to get 70. Then, store this value at the output position; here, the output position (3) is at position 3, so it overwrites itself. Afterward, the program looks like this:

1,9,10,70,
2,3,11,0,
99,
30,40,50

Step forward 4 positions to reach the next opcode, 2. This opcode works just like the previous, but it multiplies instead of adding. The inputs are at positions 3 and 11; these positions contain 70 and 50 respectively. Multiplying these produces 3500; this is stored at position 0:

3500,9,10,70,
2,3,11,0,
99,
30,40,50

Stepping forward 4 more positions arrives at opcode 99, halting the program.

Here are the initial and final states of a few more small programs:

1,0,0,0,99 becomes 2,0,0,0,99 (1 + 1 = 2).
2,3,0,3,99 becomes 2,3,0,6,99 (3 * 2 = 6).
2,4,4,5,99,0 becomes 2,4,4,5,99,9801 (99 * 99 = 9801).
1,1,1,4,99,5,6,0,99 becomes 30,1,1,4,2,5,6,0,99.
Once you have a working computer, the first step is to restore the gravity assist program (your puzzle input) to the “1202 program alarm” state it had just before the last computer caught fire. To do this, before running the program, replace position 1 with the value 12 and replace position 2 with the value 2. What value is left at position 0 after the program halts?

Part 1 discussion/solution

This was a lot to take in, but it is actually pretty simple. Our input is just a comma separated list of numbers:

1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,6,1,19,1,19,5,23,2,9,23,27,1,5,27,31,1,5,31,35,1,35,13,39,1,39,9,43,1,5,43,47,1,47,6,51,1,51,13,55,1,55,9,59,1,59,13,63,2,63,13,67,1,67,10,71,1,71,6,75,2,10,75,79,2,10,79,83,1,5,83,87,2,6,87,91,1,91,6,95,1,95,13,99,2,99,13,103,1,103,9,107,1,10,107,111,2,111,13,115,1,10,115,119,1,10,119,123,2,13,123,127,2,6,127,131,1,13,131,135,1,135,2,139,1,139,6,0,99,2,0,14,0

Each “instruction” is 4 numbers long so we’ll take our first one: 1,0,0,3. The opcode is 1, which means we take the number at position 0 and position 0 and add them, and store the result in position 3. So after this instruction completes our list will start with 1002 because 1 + 1 is 2 and our list is zero-indexed. After this, we’re done with the first instruction and we move down to the next one which is 4 numbers after the first. That would be 1,1,2,3. This is adding again. We take the number at the second spot and add it to the third, and store it in the fourth. We keep doing this until we find an opcode of 99, where the program ends.

#!/usr/bin/python

#example line is opcodes separated by commas, 99 to end opcodes and begin inputs
#opcodes:  1 = - first 2 numbers are start,stop position of numbers to ADD
#              - third number indicates position to store output
        
#opcodes:  2 = - just like opcode 1 but multiply
#to move to next opcode, step forward 4 positions (opcode, 1, 2, 3, NEXT)
#0-indexed

#open input file and split it at each comma into "commands"
with open("input.txt") as fp:
    line = fp.readline()
    commands = line.split(",")
    
#split gave us strings but we want ints, this loops over and changes them to ints
for i in range(len(commands)):
    commands[i] = int(commands[i])
i = 0

#the prompt says to replace the second and third numbers with 12 and 2
commands[1] = 12
commands[2] = 2
while i < len(commands):
    if commands[i] == 1: #add case 
        commands[commands[i + 3]] = commands[commands[i + 1]] + commands[commands[i + 2]]
    elif commands[i] == 2: #multiply case
        commands[commands[i + 3]] = commands[commands[i + 1]] * commands[commands[i + 2]]
    elif commands[i] == 99:
        break;
    else:
        print("unkown opcode:" + str(commands[i]))
    i+=4
#the prompt asks for what the value of the first number is after the program runs
print(commands[0])

Part 2 Prompt

“Good, the new computer seems to be working correctly! Keep it nearby during this mission – you’ll probably use it again. Real Intcode computers support many more features than your new one, but we’ll let you know what they are as you need them.”

“However, your current priority should be to complete your gravity assist around the Moon. For this mission to succeed, we should settle on some terminology for the parts you’ve already built.”

Intcode programs are given as a list of integers; these values are used as the initial state for the computer’s memory. When you run an Intcode program, make sure to start by initializing memory to the program’s values. A position in memory is called an address (for example, the first value in memory is at “address 0”).

Opcodes (like 1, 2, or 99) mark the beginning of an instruction. The values used immediately after an opcode, if any, are called the instruction’s parameters. For example, in the instruction 1,2,3,4, 1 is the opcode; 2, 3, and 4 are the parameters. The instruction 99 contains only an opcode and has no parameters.

The address of the current instruction is called the instruction pointer; it starts at 0. After an instruction finishes, the instruction pointer increases by the number of values in the instruction; until you add more instructions to the computer, this is always 4 (1 opcode + 3 parameters) for the add and multiply instructions. (The halt instruction would increase the instruction pointer by 1, but it halts the program instead.)

“With terminology out of the way, we’re ready to proceed. To complete the gravity assist, you need to determine what pair of inputs produces the output 19690720.”

The inputs should still be provided to the program by replacing the values at addresses 1 and 2, just like before. In this program, the value placed in address 1 is called the noun, and the value placed in address 2 is called the verb. Each of the two input values will be between 0 and 99, inclusive.

Once the program has halted, its output is available at address 0, also just like before. Each time you try a pair of inputs, make sure you first reset the computer’s memory to the values in the program (your puzzle input) – in other words, don’t reuse memory from a previous attempt.

Find the input noun and verb that cause the program to produce the output 19690720. What is 100 * noun + verb? (For example, if noun=12 and verb=2, the answer would be 1202.)

Part 2 discussion and “solution”

So, basically this part is just asking us to choose the numbers we substituted in during the first part in such a way that we get the answer 19690720 in the first spot after the program runs.

Realistically, I bruteforced this problem. I noticed increasing the first number increased the answer every time by a lot, and increasing the second number increased the answer by a smaller amount. So, I increased the first number until it the wanted answer got too big then went back one. Then increase the second number until it is the desired number. Bruteforcing is a clunky way and I did it even clunkier. I didn’t even check the values programatically, I changed them by hand and paid attention to when it got too big. Programming it to do this would have been easy but I wasn’t certain it would work until, well, it did.

#!/usr/bin/python

#example line is opcodes separated by commas, 99 to end opcodes and begin inputs
#opcodes:  1 = - first 2 numbers are start,stop position of numbers to ADD
#              - third number indicates position to store output
        
#opcodes:  2 = - just like opcode 1 but multiply
#to move to next opcode, step forward 4 positions (opcode, 1, 2, 3, NEXT)
#0-indexed
i = 0
desired=19690720
noun=77
verb=33
commands=""
with open("input.txt") as fp:
    line = fp.readline()
    commands = line.split(",")
def loadfile():
    with open("input.txt") as fp:
        line = fp.readline()
        commands = line.split(",")

def run():
    i = 0
    commands[1] = noun
    commands[2] = verb
    while i < len(commands):
        if commands[i] == 1: #add case 
            commands[commands[i + 3]] = commands[commands[i + 1]] + commands[commands[i + 2]]
        elif commands[i] == 2: #multiply case
            commands[commands[i + 3]] = commands[commands[i + 1]] * commands[commands[i + 2]]
        elif commands[i] == 99:
            break;
        else:
            print("unkown opcode:" + str(commands[i]))
        i+=4
    print(commands[0])
    
for i in range(len(commands)):
    commands[i] = int(commands[i])
#print(commands)
i = 0
for i in range(3,10):
    noun=i
    fp.close()
    loadfile()
    run()
#run()

score

I didn’t start this until much later in the day and spent about 45 minutes total – well far away from getting any score even if I had done it at midnight. I didn’t work on it solidly and the time is sort of just an estimate. Looking forward to tomorrow’s puzzle but this one was fairly tough for me so probably these will get to be too much soon.

Print Friendly, PDF & Email