Created
December 3, 2019 19:39
-
-
Save joaoqalves/a35986426a84339f830f79bc9c2fe658 to your computer and use it in GitHub Desktop.
AoC 2019 #2
This file contains hidden or 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
def add(lst, idx): | |
lst[lst[3 + idx]] = lst[lst[1 + idx]] + lst[lst[2 + idx]] | |
return -1 | |
def multiply(lst, idx): | |
lst[lst[3 + idx]] = lst[lst[1 + idx]] * lst[lst[2 + idx]] | |
return -1 | |
def halt(lst, idx): | |
return lst[0] | |
OPCODES = { | |
'1': add, | |
'2': multiply, | |
'99': halt | |
} | |
def run_program(values, noun, verb): | |
values[1] = noun | |
values[2] = verb | |
i = 0 | |
ret = -1 | |
while ret == -1 : | |
ret = OPCODES.get("{}".format(values[i]), halt)(values, i) | |
i += 4 | |
if ret == 19690720: | |
print("SIIIII") | |
print(noun, verb) | |
print(100 * noun + verb) | |
with open('/Users/joaoqalves/Desktop/puzzle2.txt') as read_file: | |
for line in read_file.readlines(): | |
values = [int(x) for x in line.split(",")] | |
for noun in range(99): | |
for verb in range(99): | |
run_program(values.copy(), noun, verb) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment