X-Git-Url: https://git.njae.me.uk/?p=advent-of-code-15.git;a=blobdiff_plain;f=advent23.ipynb;fp=advent23.ipynb;h=371f02e762a299c148f62fe6f9a1a86b5d2c8ac9;hp=0000000000000000000000000000000000000000;hb=8e419e5f33129fae84965a39e6f3f4338e15f6d9;hpb=2b7dc1356209b2a6c81145be47e20aa1480ad080 diff --git a/advent23.ipynb b/advent23.ipynb new file mode 100644 index 0000000..371f02e --- /dev/null +++ b/advent23.ipynb @@ -0,0 +1,230 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[['jio', 'a, +18'],\n", + " ['inc', 'a'],\n", + " ['tpl', 'a'],\n", + " ['inc', 'a'],\n", + " ['tpl', 'a'],\n", + " ['tpl', 'a'],\n", + " ['tpl', 'a'],\n", + " ['inc', 'a'],\n", + " ['tpl', 'a'],\n", + " ['inc', 'a'],\n", + " ['tpl', 'a'],\n", + " ['inc', 'a'],\n", + " ['inc', 'a'],\n", + " ['tpl', 'a'],\n", + " ['tpl', 'a'],\n", + " ['tpl', 'a'],\n", + " ['inc', 'a'],\n", + " ['jmp', '+22'],\n", + " ['tpl', 'a'],\n", + " ['inc', 'a'],\n", + " ['tpl', 'a'],\n", + " ['inc', 'a'],\n", + " ['inc', 'a'],\n", + " ['tpl', 'a'],\n", + " ['inc', 'a'],\n", + " ['tpl', 'a'],\n", + " ['inc', 'a'],\n", + " ['inc', 'a'],\n", + " ['tpl', 'a'],\n", + " ['tpl', 'a'],\n", + " ['inc', 'a'],\n", + " ['inc', 'a'],\n", + " ['tpl', 'a'],\n", + " ['inc', 'a'],\n", + " ['inc', 'a'],\n", + " ['tpl', 'a'],\n", + " ['inc', 'a'],\n", + " ['inc', 'a'],\n", + " ['tpl', 'a'],\n", + " ['jio', 'a, +8'],\n", + " ['inc', 'b'],\n", + " ['jie', 'a, +4'],\n", + " ['tpl', 'a'],\n", + " ['inc', 'a'],\n", + " ['jmp', '+2'],\n", + " ['hlf', 'a'],\n", + " ['jmp', '-7']]" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "program = [i.strip().split(' ', 1) for i in open('advent23.txt').readlines()]\n", + "program" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "registers = {'a': 0, 'b': 0, 'pc': 0}" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def hlf(args):\n", + " registers[args] >>= 1\n", + " registers['pc'] += 1\n", + " \n", + "def tpl(args):\n", + " registers[args] *= 3\n", + " registers['pc'] += 1\n", + "\n", + "def inc(args):\n", + " registers[args] += 1\n", + " registers['pc'] += 1\n", + "\n", + "def jmp(args):\n", + " registers['pc'] += int(args)\n", + "\n", + "def jie(args):\n", + " r, o = args.split(', ')\n", + " if registers[r] % 2 == 0:\n", + " registers['pc'] += int(o)\n", + " else:\n", + " registers['pc'] += 1\n", + "\n", + "def jio(args):\n", + " r, o = args.split(', ')\n", + " if registers[r] == 1:\n", + " registers['pc'] += int(o)\n", + " else:\n", + " registers['pc'] += 1\n" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "instructions = {'hlf': hlf, 'tpl': tpl, 'inc': inc, 'jmp': jmp, 'jie': jie, 'jio': jio}" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "while registers['pc'] < len(program):\n", + " instructions[program[registers['pc']][0]](program[registers['pc']][1])\n", + "registers" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'a': 1, 'b': 307, 'pc': 47}" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "registers" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Part 2" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'a': 1, 'b': 160, 'pc': 47}" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "registers = {'a': 1, 'b': 0, 'pc': 0}\n", + "while registers['pc'] < len(program):\n", + " instructions[program[registers['pc']][0]](program[registers['pc']][1])\n", + "registers" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.4.3" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +}