1
0
mirror of https://gitlab.com/MisterBiggs/advent-of-code-2019.git synced 2025-06-16 15:17:15 +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": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 37,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"with open(\"C:\\Coding\\Advent Of Code 2019\\Day 2\\input.txt\") as input:\n", "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", "cell_type": "code",
"execution_count": 6, "execution_count": 38,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"sequence = [1,9,10,3,2,3,11,0,99,30,40,50]" "def intcode(seq, offset=0):\n",
] " opcode = seq[0 + offset]\n",
}, " if opcode == 99:\n",
{ " return seq\n",
"cell_type": "code", " vals = [seq[seq[1 + offset]], seq[seq[2 + offset]]]\n",
"execution_count": 11, " index = seq[3 + offset]\n",
"metadata": {}, " if index == 0:\n",
"outputs": [], " print(offset)\n",
"source": [
"def intcode(seq):\n",
" opcode = seq[0]\n",
" vals = seq[1:3]\n",
" index = seq[3]\n",
"\n", "\n",
" if opcode == 1:\n", " if opcode == 1:\n",
" seq[index] = sum(vals)\n", " seq[index] = sum(vals)\n",
" return intcode(seq)\n", " return intcode(seq, offset + 4)\n",
" elif opcode == 2:\n", " elif opcode == 2:\n",
" seq[index] = vals[0] * vals[1]\n", " seq[index] = vals[0] * vals[1]\n",
" return intcode(seq)\n", " return intcode(seq, offset + 4)\n",
" elif opcode == 99:\n", "\n",
" return seq\n",
" else:\n", " else:\n",
" raise \"invalid opcode\"\n", " # raise \"invalid opcode\"\n",
"\n" " return seq"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 12, "execution_count": 39,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"ename": "IndexError", "name": "stdout",
"evalue": "list assignment index out of range", "output_type": "stream",
"output_type": "error", "text": "172\n"
"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"
]
} }
], ],
"source": [ "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): # def intcode(seq, offset=0):
opcode = seq[0 + offset] # opcode = seq[0 + offset]
if opcode == 99: # if opcode == 99:
return seq # return seq
vals = [seq[seq[1 + offset]], seq[seq[2 + offset]]] # vals = [seq[seq[1 + offset]], seq[seq[2 + offset]]]
index = seq[3 + 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)
# 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(",")))
# 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: if opcode == 1:
seq[index] = sum(vals) P[i3] = P[i1] + P[i2]
return intcode(seq, offset + 4)
elif opcode == 2: elif opcode == 2:
seq[index] = vals[0] * vals[1] P[i3] = P[i1] * P[i2]
return intcode(seq, offset + 4)
else: else:
raise "invalid opcode" assert opcode == 99
break
ip += 4
if P[0] == 19690720:
print(x1, x2)
print(100 * x1 + x2)
with open("C:\Coding\Advent Of Code 2019\Day 2\input.txt") as input:
sequence = list(map(int, input.read().split(",")))
sequence[1] = 12
sequence[2] = 2
print(intcode(sequence))