- "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)))"