Created
December 2, 2019 10:51
-
-
Save tapionx/01f7596be98c4b93b9681addac8d1a2b to your computer and use it in GitHub Desktop.
Advent of Code 2019 #3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with open('2_input.txt', 'r') as f: | |
input_program = [int(x) for x in f.read().strip().split(',')] | |
input_program[1] = 12 | |
input_program[2] = 2 | |
def execute_intcode(input_program): | |
program_counter = 0 | |
while program_counter < len(input_program) and input_program[program_counter] != 99: | |
#print(program_counter, input_program[program_counter]) | |
opcode = input_program[program_counter] | |
input1_address = input_program[program_counter + 1] | |
input2_address = input_program[program_counter + 2] | |
output_address = input_program[program_counter + 3] | |
if opcode == 1: | |
input_program[output_address] = input_program[input1_address] + input_program[input2_address] | |
if opcode == 2: | |
input_program[output_address] = input_program[input1_address] * input_program[input2_address] | |
program_counter += 4 | |
return input_program | |
print(execute_intcode([1,0,0,0,99])) | |
print(execute_intcode([2,3,0,3,99])) | |
print(execute_intcode([2,4,4,5,99,0])) | |
print(execute_intcode([1,1,1,4,99,5,6,0,99])) | |
print(execute_intcode(input_program)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment