Skip to content

Instantly share code, notes, and snippets.

@joaoqalves
Created December 3, 2019 19:39
Show Gist options
  • Save joaoqalves/a35986426a84339f830f79bc9c2fe658 to your computer and use it in GitHub Desktop.
Save joaoqalves/a35986426a84339f830f79bc9c2fe658 to your computer and use it in GitHub Desktop.
AoC 2019 #2
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