mirror of
https://gitlab.com/MisterBiggs/advent-of-code-2019.git
synced 2025-06-15 14:46:43 +00:00
working solution
This commit is contained in:
parent
d20d134f47
commit
e644a6565d
@ -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()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user