Fixed typo
[advent-of-code-15.git] / advent23.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 13,
6 "metadata": {
7 "collapsed": false
8 },
9 "outputs": [
10 {
11 "data": {
12 "text/plain": [
13 "[['jio', 'a, +18'],\n",
14 " ['inc', 'a'],\n",
15 " ['tpl', 'a'],\n",
16 " ['inc', 'a'],\n",
17 " ['tpl', 'a'],\n",
18 " ['tpl', 'a'],\n",
19 " ['tpl', 'a'],\n",
20 " ['inc', 'a'],\n",
21 " ['tpl', 'a'],\n",
22 " ['inc', 'a'],\n",
23 " ['tpl', 'a'],\n",
24 " ['inc', 'a'],\n",
25 " ['inc', 'a'],\n",
26 " ['tpl', 'a'],\n",
27 " ['tpl', 'a'],\n",
28 " ['tpl', 'a'],\n",
29 " ['inc', 'a'],\n",
30 " ['jmp', '+22'],\n",
31 " ['tpl', 'a'],\n",
32 " ['inc', 'a'],\n",
33 " ['tpl', 'a'],\n",
34 " ['inc', 'a'],\n",
35 " ['inc', 'a'],\n",
36 " ['tpl', 'a'],\n",
37 " ['inc', 'a'],\n",
38 " ['tpl', 'a'],\n",
39 " ['inc', 'a'],\n",
40 " ['inc', 'a'],\n",
41 " ['tpl', 'a'],\n",
42 " ['tpl', 'a'],\n",
43 " ['inc', 'a'],\n",
44 " ['inc', 'a'],\n",
45 " ['tpl', 'a'],\n",
46 " ['inc', 'a'],\n",
47 " ['inc', 'a'],\n",
48 " ['tpl', 'a'],\n",
49 " ['inc', 'a'],\n",
50 " ['inc', 'a'],\n",
51 " ['tpl', 'a'],\n",
52 " ['jio', 'a, +8'],\n",
53 " ['inc', 'b'],\n",
54 " ['jie', 'a, +4'],\n",
55 " ['tpl', 'a'],\n",
56 " ['inc', 'a'],\n",
57 " ['jmp', '+2'],\n",
58 " ['hlf', 'a'],\n",
59 " ['jmp', '-7']]"
60 ]
61 },
62 "execution_count": 13,
63 "metadata": {},
64 "output_type": "execute_result"
65 }
66 ],
67 "source": [
68 "program = [i.strip().split(' ', 1) for i in open('advent23.txt').readlines()]\n",
69 "program"
70 ]
71 },
72 {
73 "cell_type": "code",
74 "execution_count": 3,
75 "metadata": {
76 "collapsed": true
77 },
78 "outputs": [],
79 "source": [
80 "registers = {'a': 0, 'b': 0, 'pc': 0}"
81 ]
82 },
83 {
84 "cell_type": "code",
85 "execution_count": 24,
86 "metadata": {
87 "collapsed": true
88 },
89 "outputs": [],
90 "source": [
91 "def hlf(args):\n",
92 " registers[args] >>= 1\n",
93 " registers['pc'] += 1\n",
94 " \n",
95 "def tpl(args):\n",
96 " registers[args] *= 3\n",
97 " registers['pc'] += 1\n",
98 "\n",
99 "def inc(args):\n",
100 " registers[args] += 1\n",
101 " registers['pc'] += 1\n",
102 "\n",
103 "def jmp(args):\n",
104 " registers['pc'] += int(args)\n",
105 "\n",
106 "def jie(args):\n",
107 " r, o = args.split(', ')\n",
108 " if registers[r] % 2 == 0:\n",
109 " registers['pc'] += int(o)\n",
110 " else:\n",
111 " registers['pc'] += 1\n",
112 "\n",
113 "def jio(args):\n",
114 " r, o = args.split(', ')\n",
115 " if registers[r] == 1:\n",
116 " registers['pc'] += int(o)\n",
117 " else:\n",
118 " registers['pc'] += 1\n"
119 ]
120 },
121 {
122 "cell_type": "code",
123 "execution_count": 25,
124 "metadata": {
125 "collapsed": false
126 },
127 "outputs": [],
128 "source": [
129 "instructions = {'hlf': hlf, 'tpl': tpl, 'inc': inc, 'jmp': jmp, 'jie': jie, 'jio': jio}"
130 ]
131 },
132 {
133 "cell_type": "code",
134 "execution_count": 26,
135 "metadata": {
136 "collapsed": false
137 },
138 "outputs": [],
139 "source": [
140 "while registers['pc'] < len(program):\n",
141 " instructions[program[registers['pc']][0]](program[registers['pc']][1])\n",
142 "registers"
143 ]
144 },
145 {
146 "cell_type": "code",
147 "execution_count": 27,
148 "metadata": {
149 "collapsed": false
150 },
151 "outputs": [
152 {
153 "data": {
154 "text/plain": [
155 "{'a': 1, 'b': 307, 'pc': 47}"
156 ]
157 },
158 "execution_count": 27,
159 "metadata": {},
160 "output_type": "execute_result"
161 }
162 ],
163 "source": [
164 "registers"
165 ]
166 },
167 {
168 "cell_type": "markdown",
169 "metadata": {},
170 "source": [
171 "# Part 2"
172 ]
173 },
174 {
175 "cell_type": "code",
176 "execution_count": 28,
177 "metadata": {
178 "collapsed": false
179 },
180 "outputs": [
181 {
182 "data": {
183 "text/plain": [
184 "{'a': 1, 'b': 160, 'pc': 47}"
185 ]
186 },
187 "execution_count": 28,
188 "metadata": {},
189 "output_type": "execute_result"
190 }
191 ],
192 "source": [
193 "registers = {'a': 1, 'b': 0, 'pc': 0}\n",
194 "while registers['pc'] < len(program):\n",
195 " instructions[program[registers['pc']][0]](program[registers['pc']][1])\n",
196 "registers"
197 ]
198 },
199 {
200 "cell_type": "code",
201 "execution_count": null,
202 "metadata": {
203 "collapsed": true
204 },
205 "outputs": [],
206 "source": []
207 }
208 ],
209 "metadata": {
210 "kernelspec": {
211 "display_name": "Python 3",
212 "language": "python",
213 "name": "python3"
214 },
215 "language_info": {
216 "codemirror_mode": {
217 "name": "ipython",
218 "version": 3
219 },
220 "file_extension": ".py",
221 "mimetype": "text/x-python",
222 "name": "python",
223 "nbconvert_exporter": "python",
224 "pygments_lexer": "ipython3",
225 "version": "3.4.3"
226 }
227 },
228 "nbformat": 4,
229 "nbformat_minor": 0
230 }