From e644a6565dff6ee6dfd881f746f3211eab3b3c06 Mon Sep 17 00:00:00 2001 From: Anson Date: Mon, 2 Dec 2019 14:40:42 -0700 Subject: [PATCH] working solution --- Day 2/program_alarm.py | 76 ++++++++++++++---------------------------- 1 file changed, 25 insertions(+), 51 deletions(-) diff --git a/Day 2/program_alarm.py b/Day 2/program_alarm.py index b918cec..ea555f0 100644 --- a/Day 2/program_alarm.py +++ b/Day 2/program_alarm.py @@ -1,58 +1,32 @@ -# def intcode(seq, offset=0): -# opcode = seq[0 + offset] -# if opcode == 99: -# return seq -# vals = [seq[seq[1 + offset]], seq[seq[2 + offset]]] -# index = seq[3 + offset] +def intcode(seq, offset=0): + opcode = seq[0 + offset] + if opcode == 99: + return seq + vals = [seq[seq[1 + offset]], seq[seq[2 + offset]]] + index = seq[3 + offset] -# if opcode == 1: -# seq[index] = sum(vals) -# return intcode(seq, offset + 4) -# elif opcode == 2: -# seq[index] = vals[0] * vals[1] -# return intcode(seq, offset + 4) + if opcode == 1: + seq[index] = sum(vals) + return intcode(seq, offset + 4) + elif opcode == 2: + seq[index] = vals[0] * vals[1] + return intcode(seq, offset + 4) -# else: -# return seq + else: + return seq -# with open("C:\Coding\Advent Of Code 2019\Day 2\input.txt") as input: -# main_sequence = list(map(int, input.read().split(","))) +with open("C:\Coding\Advent Of Code 2019\Day 2\input.txt") as input: + main_sequence = list(map(int, input.read().split(","))) -# for i in range(100): -# for j in range(100): +for i in range(100): + for j in range(100): -# sequence = main_sequence -# sequence[1] = j -# sequence[2] = i -# print(intcode(sequence)[0], i, j) -# if intcode(sequence)[0] == 19690720: -# print(i, j) -# break - -OP = [ - int(x) - for x in open("C:\Coding\Advent Of Code 2019\Day 2\input.txt").read().split(",") -] - -for x1 in range(100): - for x2 in range(100): - P = [x for x in OP] - P[1] = x1 - P[2] = x2 - ip = 0 - while True: - opcode = P[ip] - i1, i2, i3 = P[ip + 1], P[ip + 2], P[ip + 3] - if opcode == 1: - P[i3] = P[i1] + P[i2] - elif opcode == 2: - P[i3] = P[i1] * P[i2] - else: - assert opcode == 99 - break - ip += 4 - if P[0] == 19690720: - print(x1, x2) - print(100 * x1 + x2) + sequence = main_sequence[:] + sequence[1] = i + sequence[2] = j + print(intcode(sequence)[0], i, j) + if intcode(sequence)[0] == 19690720: + print("Answer: ", 100 * i + j) + quit()