},
{
"cell_type": "code",
- "execution_count": 4,
- "metadata": {},
+ "execution_count": 1,
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"def new_machine():\n",
},
{
"cell_type": "code",
- "execution_count": 5,
+ "execution_count": 2,
"metadata": {
"collapsed": true
},
},
{
"cell_type": "code",
- "execution_count": 6,
+ "execution_count": 3,
"metadata": {
"collapsed": true
},
},
{
"cell_type": "code",
- "execution_count": 7,
+ "execution_count": 4,
"metadata": {
"collapsed": true
},
},
{
"cell_type": "code",
- "execution_count": 8,
+ "execution_count": 5,
"metadata": {
"collapsed": true
},
},
{
"cell_type": "code",
- "execution_count": 9,
+ "execution_count": 6,
"metadata": {
"collapsed": true
},
},
{
"cell_type": "code",
- "execution_count": 10,
+ "execution_count": 7,
"metadata": {
"collapsed": true
},
},
{
"cell_type": "code",
- "execution_count": 11,
+ "execution_count": 8,
"metadata": {
"collapsed": true
},
},
{
"cell_type": "code",
- "execution_count": 12,
+ "execution_count": 9,
"metadata": {
"collapsed": true
},
},
{
"cell_type": "code",
- "execution_count": 13,
+ "execution_count": 10,
"metadata": {
"collapsed": true
},
},
{
"cell_type": "code",
- "execution_count": 14,
+ "execution_count": 11,
"metadata": {},
"outputs": [
{
"{'a': 2, 'b': 1, 'c': 0, 'd': 0, 'instructions': [], 'pc': 3}"
]
},
- "execution_count": 14,
+ "execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
},
{
"cell_type": "code",
- "execution_count": 15,
+ "execution_count": 12,
"metadata": {
"collapsed": true
},
},
{
"cell_type": "code",
- "execution_count": 16,
+ "execution_count": 45,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
- "def program_from_listing(listing, machine):\n",
+ "def unlabel_listing(listing):\n",
" labelled_instructions = [i.strip() for i in listing.split('\\n') \n",
" if i.strip() \n",
" if not i.strip().startswith('#')]\n",
- " instructions = replace_labels(labelled_instructions)\n",
+ " return replace_labels(labelled_instructions) "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 13,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "# def program_from_listing(listing, machine):\n",
+ "# labelled_instructions = [i.strip() for i in listing.split('\\n') \n",
+ "# if i.strip() \n",
+ "# if not i.strip().startswith('#')]\n",
+ "# instructions = replace_labels(labelled_instructions)\n",
+ "# program_from_instructions(instructions, machine)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 53,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "def program_from_listing(listing, machine):\n",
+ " instructions = unlabel_listing(listing)\n",
" program_from_instructions(instructions, machine)"
]
},
{
"cell_type": "code",
- "execution_count": 17,
+ "execution_count": 14,
"metadata": {
"collapsed": true
},
},
{
"cell_type": "code",
- "execution_count": 18,
+ "execution_count": 15,
"metadata": {},
"outputs": [
{
"['inc', 'a']"
]
},
- "execution_count": 18,
+ "execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
},
{
"cell_type": "code",
- "execution_count": 19,
+ "execution_count": 47,
"metadata": {},
"outputs": [
{
},
{
"cell_type": "code",
- "execution_count": 20,
+ "execution_count": 49,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "set a 10\n",
+ "dec a\n",
+ "inc b\n",
+ "jpz a 2\n",
+ "jmp -3\n"
+ ]
+ }
+ ],
+ "source": [
+ "print('\\n'.join(unlabel_listing(program)))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 17,
"metadata": {
"collapsed": true
},
},
{
"cell_type": "code",
- "execution_count": 21,
+ "execution_count": 18,
"metadata": {
"collapsed": true
},
},
{
"cell_type": "code",
- "execution_count": 22,
+ "execution_count": 19,
"metadata": {},
"outputs": [
{
" 'pc': 4}"
]
},
- "execution_count": 22,
+ "execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
},
{
"cell_type": "code",
- "execution_count": 24,
+ "execution_count": 20,
"metadata": {},
"outputs": [
{
" 'pc': 5}"
]
},
- "execution_count": 24,
+ "execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
},
{
"cell_type": "code",
- "execution_count": 25,
+ "execution_count": 21,
"metadata": {},
"outputs": [
{
" 'pc': 5}"
]
},
- "execution_count": 25,
+ "execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
},
{
"cell_type": "code",
- "execution_count": 26,
+ "execution_count": 22,
"metadata": {},
"outputs": [
{
" 'pc': 10}"
]
},
- "execution_count": 26,
+ "execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
},
{
"cell_type": "code",
- "execution_count": 27,
+ "execution_count": 71,
"metadata": {},
"outputs": [
{
" 'pc': 9}"
]
},
- "execution_count": 27,
+ "execution_count": 71,
"metadata": {},
"output_type": "execute_result"
}
},
{
"cell_type": "code",
- "execution_count": 28,
+ "execution_count": 64,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "cpy c a\n",
+ "set b 0\n",
+ "dec a\n",
+ "jpz b 3\n",
+ "dec b\n",
+ "jmp 2\n",
+ "inc b\n",
+ "jpz a 2\n",
+ "jmp -6\n"
+ ]
+ }
+ ],
+ "source": [
+ "print('\\n'.join(unlabel_listing(program)))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 65,
"metadata": {},
"outputs": [
{
"'a: 0, b: 1, c: 8, d: 0, pc: 10'"
]
},
- "execution_count": 28,
+ "execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
},
{
"cell_type": "code",
- "execution_count": 29,
+ "execution_count": 66,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "set c 0\n",
+ "set b 0\n",
+ "dec a\n",
+ "jpz b 4\n",
+ "dec b\n",
+ "inc c\n",
+ "jmp 2\n",
+ "inc b\n",
+ "jpz a 2\n",
+ "jmp -7\n"
+ ]
+ }
+ ],
+ "source": [
+ "print('\\n'.join(unlabel_listing(program)))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 25,
"metadata": {},
"outputs": [
{
"'a: 4, b: 0, c: 12, d: 0, pc: 9'"
]
},
- "execution_count": 29,
+ "execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
},
{
"cell_type": "code",
- "execution_count": 38,
+ "execution_count": 67,
"metadata": {},
"outputs": [
{
"'a: 0, b: 0, c: 27, d: 0, pc: 11'"
]
},
- "execution_count": 38,
+ "execution_count": 67,
"metadata": {},
"output_type": "execute_result"
}
},
{
"cell_type": "code",
- "execution_count": 39,
+ "execution_count": 68,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "set c 0\n",
+ "cpy a d\n",
+ "jpz b 8\n",
+ "dec b\n",
+ "cpy d a\n",
+ "jpz a 4\n",
+ "inc c\n",
+ "dec a\n",
+ "jmp -3\n",
+ "jmp -7\n",
+ "set d 0\n"
+ ]
+ }
+ ],
+ "source": [
+ "print('\\n'.join(unlabel_listing(program)))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 27,
"metadata": {},
"outputs": [
{
},
{
"cell_type": "code",
- "execution_count": 31,
+ "execution_count": 28,
"metadata": {},
"outputs": [
{
"'a: 2, b: 0, c: 10, d: 2, pc: 13'"
]
},
- "execution_count": 31,
+ "execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
},
{
"cell_type": "code",
- "execution_count": 32,
+ "execution_count": 29,
"metadata": {},
"outputs": [
{
},
{
"cell_type": "code",
- "execution_count": 33,
+ "execution_count": 30,
"metadata": {},
"outputs": [
{
"'a: 52, b: 0, c: 0, d: 0, pc: 48'"
]
},
- "execution_count": 33,
+ "execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
},
{
"cell_type": "code",
- "execution_count": 32,
+ "execution_count": 31,
"metadata": {},
"outputs": [
{
"40"
]
},
- "execution_count": 32,
+ "execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
},
{
"cell_type": "code",
- "execution_count": 33,
+ "execution_count": 32,
"metadata": {
"collapsed": true
},
},
{
"cell_type": "code",
- "execution_count": 34,
+ "execution_count": 33,
"metadata": {},
"outputs": [
{
"52"
]
},
- "execution_count": 34,
+ "execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
},
{
"cell_type": "code",
- "execution_count": 35,
+ "execution_count": 34,
"metadata": {},
"outputs": [
{
"(250504, 937)"
]
},
- "execution_count": 35,
+ "execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
{
"data": {
"text/plain": [
- "[(1, 0, 1),\n",
- " (2, 0, 2),\n",
- " (3, 0, 16),\n",
- " (4, 0, 4),\n",
- " (5, 0, 16),\n",
- " (6, 0, 16),\n",
- " (7, 0, 52),\n",
- " (8, 0, 8),\n",
- " (9, 0, 52),\n",
- " (10, 0, 16),\n",
- " (11, 0, 52),\n",
- " (12, 0, 16),\n",
- " (13, 0, 40),\n",
- " (14, 0, 52),\n",
- " (15, 0, 160),\n",
- " (16, 0, 16),\n",
- " (17, 0, 52),\n",
- " (18, 0, 52),\n",
- " (19, 0, 88),\n",
- " (20, 0, 20),\n",
- " (21, 0, 64),\n",
- " (22, 0, 52),\n",
- " (23, 0, 160),\n",
- " (24, 0, 24),\n",
- " (25, 0, 88),\n",
- " (26, 0, 40),\n",
- " (27, 0, 9232),\n",
- " (28, 0, 52),\n",
- " (29, 0, 88),\n",
- " (30, 0, 160),\n",
- " (31, 0, 9232),\n",
- " (32, 0, 32),\n",
- " (33, 0, 100),\n",
- " (34, 0, 52),\n",
- " (35, 0, 160),\n",
- " (36, 0, 52),\n",
- " (37, 0, 112),\n",
- " (38, 0, 88),\n",
- " (39, 0, 304),\n",
- " (40, 0, 40),\n",
- " (41, 0, 9232),\n",
- " (42, 0, 64),\n",
- " (43, 0, 196),\n",
- " (44, 0, 52),\n",
- " (45, 0, 136),\n",
- " (46, 0, 160),\n",
- " (47, 0, 9232),\n",
- " (48, 0, 48),\n",
- " (49, 0, 148),\n",
- " (50, 0, 88),\n",
- " (51, 0, 232),\n",
- " (52, 0, 52),\n",
- " (53, 0, 160),\n",
- " (54, 0, 9232),\n",
- " (55, 0, 9232),\n",
- " (56, 0, 56),\n",
- " (57, 0, 196),\n",
- " (58, 0, 88),\n",
- " (59, 0, 304),\n",
- " (60, 0, 160),\n",
- " (61, 0, 184),\n",
- " (62, 0, 9232),\n",
- " (63, 0, 9232),\n",
- " (64, 0, 64),\n",
- " (65, 0, 196),\n",
- " (66, 0, 100),\n",
- " (67, 0, 304),\n",
- " (68, 0, 68),\n",
- " (69, 0, 208),\n",
- " (70, 0, 160),\n",
- " (71, 0, 9232),\n",
- " (72, 0, 72),\n",
- " (73, 0, 9232),\n",
- " (74, 0, 112),\n",
- " (75, 0, 340),\n",
- " (76, 0, 88),\n",
- " (77, 0, 232),\n",
- " (78, 0, 304),\n",
- " (79, 0, 808),\n",
- " (80, 0, 80),\n",
- " (81, 0, 244),\n",
- " (82, 0, 9232),\n",
- " (83, 0, 9232),\n",
- " (84, 0, 84),\n",
- " (85, 0, 256),\n",
- " (86, 0, 196),\n",
- " (87, 0, 592),\n",
- " (88, 0, 88),\n",
- " (89, 0, 304),\n",
- " (90, 0, 136),\n",
- " (91, 0, 9232),\n",
- " (92, 0, 160),\n",
- " (93, 0, 280),\n",
- " (94, 0, 9232),\n",
- " (95, 0, 9232),\n",
- " (96, 0, 96),\n",
- " (97, 0, 9232),\n",
- " (98, 0, 148),\n",
- " (99, 0, 448)]"
+ "[]"
]
},
"execution_count": 36,
"for i in range(1, 100):\n",
" m = execute(program, initial_state={'a': i})\n",
" c = max_collatz(i)\n",
- " if m[1] != c:\n",
- " ans += [(i, m[1], c)]\n",
+ " if m['a'] != c:\n",
+ " ans += [(i, m['a'], c)]\n",
"ans"
]
},
{
"cell_type": "code",
- "execution_count": 34,
+ "execution_count": 37,
+ "metadata": {},
+ "outputs": [
+ {
+ "ename": "KeyboardInterrupt",
+ "evalue": "",
+ "output_type": "error",
+ "traceback": [
+ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
+ "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
+ "\u001b[0;32m<ipython-input-37-a9f4d977edea>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mshow_machine\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mexecute\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprogram\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minitial_state\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m{\u001b[0m\u001b[0;34m'a'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;36m937\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
+ "\u001b[0;32m<ipython-input-18-d22f089366e4>\u001b[0m in \u001b[0;36mexecute\u001b[0;34m(listing, initial_state, trace)\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mm\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnew_machine\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mprogram_from_listing\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlisting\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mm\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mrun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mm\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minitial_state\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0minitial_state\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtrace\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtrace\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mm\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
+ "\u001b[0;32m<ipython-input-17-5478d48920ba>\u001b[0m in \u001b[0;36mrun\u001b[0;34m(machine, initial_state, trace)\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0minitial_state\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mmachine\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mupdate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minitial_state\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0;32mwhile\u001b[0m \u001b[0mmachine\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'pc'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmachine\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'instructions'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mtrace\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mshow_machine\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmachine\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
+ "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
+ ]
+ }
+ ],
+ "source": [
+ "show_machine(execute(program, initial_state={'a': 937}))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# labelled_instructions = [i.strip() for i in program.split('\\n') \n",
+ "# if i.strip() \n",
+ "# if not i.strip().startswith('#')]\n",
+ "# instructions = replace_labels(labelled_instructions)\n",
+ "# print('\\n'.join(instructions))\n",
+ "# open('07-program.txt', 'w').write('\\n'.join(instructions))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 58,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
- "'a: 250504, b: 0, c: 0, d: 0, pc: 48'"
+ "{'a': 0,\n",
+ " 'b': 3,\n",
+ " 'c': 0,\n",
+ " 'd': 0,\n",
+ " 'instructions': [(<function __main__.dec>, ['a']),\n",
+ " (<function __main__.inc>, ['b']),\n",
+ " (<function __main__.jpz>, ['a', 2]),\n",
+ " (<function __main__.jmp>, [-3])],\n",
+ " 'pc': 4}"
]
},
- "execution_count": 34,
+ "execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
- "show_machine(execute(program, initial_state={'a': 937}))"
+ "# Adds a to b\n",
+ "program = \"\"\"\n",
+ "loop: dec a\n",
+ " inc b\n",
+ " jpz a 2\n",
+ " jmp loop\n",
+ "\"\"\"\n",
+ "execute(program, initial_state={'a': 3})"
]
},
{
"cell_type": "code",
- "execution_count": 35,
+ "execution_count": 59,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
- "cpy a d\n",
- "set b 0\n",
- "dec a\n",
- "jpz a 42\n",
- "inc a\n",
- "cpy a c\n",
- "set b 0\n",
- "dec c\n",
- "jpz b 3\n",
- "dec b\n",
- "jmp 2\n",
- "inc b\n",
- "jpz c 2\n",
- "jmp -6\n",
- "jpz b 11\n",
- "cpy a b\n",
- "jpz b 6\n",
- "dec b\n",
- "inc c\n",
- "inc c\n",
- "inc c\n",
- "jmp -5\n",
- "inc c\n",
- "cpy c a\n",
- "jmp 12\n",
- "set c 0\n",
- "set b 0\n",
"dec a\n",
- "jpz b 4\n",
- "dec b\n",
- "inc c\n",
- "jmp 2\n",
"inc b\n",
"jpz a 2\n",
- "jmp -7\n",
- "cpy c a\n",
- "cpy a b\n",
- "cpy d c\n",
- "jpz c 5\n",
- "jpz b 5\n",
- "dec b\n",
- "dec c\n",
- "jmp -4\n",
- "cpy a d\n",
- "jmp -42\n",
- "cpy d a\n",
- "set c 0\n",
- "set d 0\n"
+ "jmp -3\n"
]
- },
+ }
+ ],
+ "source": [
+ "print('\\n'.join(unlabel_listing(program)))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 55,
+ "metadata": {},
+ "outputs": [
{
"data": {
"text/plain": [
- "344"
+ "{'a': 0,\n",
+ " 'b': 7,\n",
+ " 'c': 0,\n",
+ " 'd': 0,\n",
+ " 'instructions': [(<function __main__.dec>, ['a']),\n",
+ " (<function __main__.inc>, ['b']),\n",
+ " (<function __main__.jpz>, ['a', 2]),\n",
+ " (<function __main__.jmp>, [-3])],\n",
+ " 'pc': 4}"
]
},
- "execution_count": 35,
+ "execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
- "labelled_instructions = [i.strip() for i in program.split('\\n') \n",
- " if i.strip() \n",
- " if not i.strip().startswith('#')]\n",
- "instructions = replace_labels(labelled_instructions)\n",
- "print('\\n'.join(instructions))\n",
- "open('07-program.txt', 'w').write('\\n'.join(instructions))"
+ "# Adds a to b\n",
+ "program = \"\"\"\n",
+ "loop: dec a\n",
+ " inc b\n",
+ " jpz a 2\n",
+ " jmp loop\n",
+ "\"\"\"\n",
+ "execute(program, initial_state={'a': 3, 'b': 4})"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 61,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "{'a': 4,\n",
+ " 'b': 8,\n",
+ " 'c': 0,\n",
+ " 'd': 0,\n",
+ " 'instructions': [(<function __main__.set_literal>, ['b', 0]),\n",
+ " (<function __main__.set_literal>, ['c', 0]),\n",
+ " (<function __main__.jpz>, ['a', 11]),\n",
+ " (<function __main__.dec>, ['a']),\n",
+ " (<function __main__.inc>, ['b']),\n",
+ " (<function __main__.inc>, ['b']),\n",
+ " (<function __main__.inc>, ['c']),\n",
+ " (<function __main__.jpz>, ['a', 2]),\n",
+ " (<function __main__.jmp>, [-5]),\n",
+ " (<function __main__.dec>, ['c']),\n",
+ " (<function __main__.inc>, ['a']),\n",
+ " (<function __main__.jpz>, ['c', 2]),\n",
+ " (<function __main__.jmp>, [-3]),\n",
+ " (<function __main__.set_literal>, ['c', 0])],\n",
+ " 'pc': 14}"
+ ]
+ },
+ "execution_count": 61,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Puts double a in b, leaves a unchanged\n",
+ "program = \"\"\"\n",
+ " set b 0\n",
+ " set c 0\n",
+ " jpz a end\n",
+ "loop: dec a\n",
+ " inc b\n",
+ " inc b\n",
+ " inc c\n",
+ " jpz a 2\n",
+ " jmp loop\n",
+ "lp2: dec c\n",
+ " inc a\n",
+ " jpz c 2\n",
+ " jmp lp2\n",
+ "end: set c 0 \n",
+ "\"\"\"\n",
+ "execute(program, initial_state={'a': 4})"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 62,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "set b 0\n",
+ "set c 0\n",
+ "jpz a 11\n",
+ "dec a\n",
+ "inc b\n",
+ "inc b\n",
+ "inc c\n",
+ "jpz a 2\n",
+ "jmp -5\n",
+ "dec c\n",
+ "inc a\n",
+ "jpz c 2\n",
+ "jmp -3\n",
+ "set c 0\n"
+ ]
+ }
+ ],
+ "source": [
+ "print('\\n'.join(unlabel_listing(program)))"
]
},
{