1
0
mirror of https://gitlab.com/MisterBiggs/advent-of-code-2019.git synced 2025-06-16 15:17:15 +00:00
advent-of-code-2019/Day 2/program_alarm.ipynb
2019-12-01 23:01:31 -07:00

106 lines
2.3 KiB
Plaintext

{
"nbformat": 4,
"nbformat_minor": 2,
"metadata": {
"language_info": {
"name": "python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"version": "3.7.2"
},
"orig_nbformat": 2,
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"npconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": 3
},
"cells": [
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [],
"source": [
"with open(\"C:\\Coding\\Advent Of Code 2019\\Day 2\\input.txt\") as input:\n",
" main_sequence = list(map(int, input.read().split(\",\")))"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [],
"source": [
"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, offset + 4)\n",
" elif opcode == 2:\n",
" seq[index] = vals[0] * vals[1]\n",
" return intcode(seq, offset + 4)\n",
"\n",
" else:\n",
" # raise \"invalid opcode\"\n",
" return seq"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "172\n"
}
],
"source": [
"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]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
]
}