1
0
mirror of https://gitlab.com/MisterBiggs/advent-of-code-2019.git synced 2025-06-15 22:56:47 +00:00

part 2 solution

This commit is contained in:
Anson 2019-12-01 23:01:31 -07:00
parent 9dbe247a02
commit d20d134f47
2 changed files with 97 additions and 54 deletions

View File

@ -21,68 +21,78 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 37,
"metadata": {},
"outputs": [],
"source": [
"with open(\"C:\\Coding\\Advent Of Code 2019\\Day 2\\input.txt\") as input:\n",
" sequence = list(map(int, input.read().split(\",\")))"
" main_sequence = list(map(int, input.read().split(\",\")))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 38,
"metadata": {},
"outputs": [],
"source": [
"sequence = [1,9,10,3,2,3,11,0,99,30,40,50]"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"def intcode(seq):\n",
" opcode = seq[0]\n",
" vals = seq[1:3]\n",
" index = seq[3]\n",
"def intcode(seq, offset=0):\n",
" opcode = seq[0 + offset]\n",
" if opcode == 99:\n",
" return seq\n",
" vals = [seq[seq[1 + offset]], seq[seq[2 + offset]]]\n",
" index = seq[3 + offset]\n",
" if index == 0:\n",
" print(offset)\n",
"\n",
" if opcode == 1:\n",
" seq[index] = sum(vals)\n",
" return intcode(seq)\n",
" return intcode(seq, offset + 4)\n",
" elif opcode == 2:\n",
" seq[index] = vals[0] * vals[1]\n",
" return intcode(seq)\n",
" elif opcode == 99:\n",
" return seq\n",
" return intcode(seq, offset + 4)\n",
"\n",
" else:\n",
" raise \"invalid opcode\"\n",
"\n"
" # raise \"invalid opcode\"\n",
" return seq"
]
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 39,
"metadata": {},
"outputs": [
{
"ename": "IndexError",
"evalue": "list assignment index out of range",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-12-2e4fc025cfb0>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mintcode\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msequence\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;32m<ipython-input-11-f07b5c2f2048>\u001b[0m in \u001b[0;36mintcode\u001b[1;34m(seq)\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mopcode\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[0mseq\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0msum\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mvals\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mintcode\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mseq\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 9\u001b[0m \u001b[1;32melif\u001b[0m \u001b[0mopcode\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 10\u001b[0m \u001b[0mseq\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mvals\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m*\u001b[0m \u001b[0mvals\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32m<ipython-input-11-f07b5c2f2048>\u001b[0m in \u001b[0;36mintcode\u001b[1;34m(seq)\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mopcode\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m \u001b[0mseq\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0msum\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mvals\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 8\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mintcode\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mseq\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 9\u001b[0m \u001b[1;32melif\u001b[0m \u001b[0mopcode\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mIndexError\u001b[0m: list assignment index out of range"
]
"name": "stdout",
"output_type": "stream",
"text": "172\n"
}
],
"source": [
"intcode(sequence)"
"for i in range(100):\n",
" for j in range(100):\n",
" sequence = main_sequence\n",
" sequence[1] = i\n",
" sequence[2] = j\n",
" if intcode(sequence)[0] == 19690720:\n",
" print(i,j)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": "[1, 171, 13, 0, 99]"
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sequence[172:177]"
]
},
{

View File

@ -1,25 +1,58 @@
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:
raise "invalid opcode"
# else:
# return seq
with open("C:\Coding\Advent Of Code 2019\Day 2\input.txt") as input:
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(",")))
sequence[1] = 12
sequence[2] = 2
# 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)
print(intcode(sequence))