X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=02-lifts%2Fpart2-brainfuck.ipynb;h=af179f5543b3bd94ecaac26a4c5e6e472a7ab8d1;hb=2b1813563ff7cb94fa1c9811bf8dd93a8b39be23;hp=d43ab271a08b522601da2da0cbaa256cd534ec28;hpb=7550a1b72dff3529dd0b32ba8859db137bc125f8;p=ou-summer-of-code-2017.git diff --git a/02-lifts/part2-brainfuck.ipynb b/02-lifts/part2-brainfuck.ipynb index d43ab27..af179f5 100644 --- a/02-lifts/part2-brainfuck.ipynb +++ b/02-lifts/part2-brainfuck.ipynb @@ -2,7 +2,18 @@ "cells": [ { "cell_type": "code", - "execution_count": 121, + "execution_count": 7, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "from IPython.utils import io" + ] + }, + { + "cell_type": "code", + "execution_count": 1, "metadata": { "collapsed": true }, @@ -29,6 +40,7 @@ "\n", " cells, codeptr, cellptr = [0], 0, 0\n", " inputptr = 0\n", + " outputs = []\n", "\n", " try:\n", " while codeptr < len(code):\n", @@ -52,20 +64,24 @@ "\n", " if command == \"[\" and cells[cellptr] == 0: codeptr = bracemap[codeptr]\n", " if command == \"]\" and cells[cellptr] != 0: codeptr = bracemap[codeptr]\n", - " if command == \".\": sys.stdout.write(chr(cells[cellptr]))\n", + " if command == \".\": \n", + " outputs += [cells[cellptr]]\n", + " print(cells[cellptr]) # sys.stdout.write(chr(cells[cellptr]))\n", " if command == \",\": \n", " if inp is not None:\n", " if inputptr >= len(inp):\n", - " raise EOFError\n", - " cells[cellptr] = ord(inp[inputptr])\n", - " inputptr += 1\n", + "# raise EOFError\n", + " cells[cellptr] = 0\n", + " else:\n", + " cells[cellptr] = ord(inp[inputptr])\n", + " inputptr += 1\n", " else:\n", " cells[cellptr] = ord(getch.getch())\n", "\n", " codeptr += 1\n", " except EOFError:\n", " pass\n", - " return cells, codeptr, cellptr\n", + " return cells, codeptr, cellptr, outputs\n", "\n", "\n", "def cleanup(code):\n", @@ -93,7 +109,7 @@ }, { "cell_type": "code", - "execution_count": 128, + "execution_count": 2, "metadata": {}, "outputs": [ { @@ -102,7 +118,7 @@ "(118, 94, 61)" ] }, - "execution_count": 128, + "execution_count": 2, "metadata": {}, "output_type": "execute_result" } @@ -121,35 +137,210 @@ "set cell 3 to 118-94=24\n", "copy cell 1 into cell 0, using cell 4\n", "\n", + "cell 5 for ???? currently at an exit: 1 if at an exit, 0 otherwise\n", + "\n", "set cell 6 to 0 (current level)\n", - "set cell 7 to 0 (highest exit)\n", - "reserve cell 8 for scratch\n", + "set cell 7 to for non-negative flag: 0 for +ive, 1 for -ive, 0 for zero.\n", + "set cell 8 to 0 (highest exit)\n", + "cell 9 for input\n", + "cell 10 for whether input has been dealt with: 0 for yes, 1 for no\n", + "reserve cell 11 and higher for scratch\n", "\n", "read character into cell 9\n", "while cell 9 != 0\n", " subtract cell 0 from cell 9\n", " if cell 9 == 0 we're at an exit\n", - " if cell 6 is higher then cell 7\n", - " copy cell 6 into cell 7\n", + " if cell 7 != 0\n", + " if cell 6 is higher than cell 7\n", + " copy cell 6 into cell 7\n", " else\n", " subtract cell 2 from cell 9\n", " if cell 9 == 0 we're going up\n", " increment cell 6\n", + " if cell 6 is zero\n", + " if cell 7 != 0\n", + " decrement cell 7\n", " else\n", " decrement cell 6\n", " \n", " copy cell 1 into cell 0 using cell 4\n", - " read character into cell 6\n", + " read character into cell 9\n", " \n", "output cell 7\n", + "```" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ "```\n", + "set cell 1 to 61 # exit\n", + "set cell 2 to 94-61=33 # up\n", + "set cell 3 to 118-94=24 # down\n", + "copy cell 1 into cell 0, using cell 4\n", + "\n", + "cell 5 for ???? currently at an exit: 1 if at an exit 0 otherwise\n", "\n", + "set cell 6 to 0 (current level)\n", + "set cell 7 to for height above ground, min zero\n", + "set cell 8 to 0 (highest exit)\n", + "cell 9 for input\n", + "cell 10 for whether input has been dealt with: 0 for yes 1 for no\n", + "cell 11 for whether we've dealt with the height above zero cell\n", + "reserve cell 12 and higher for scratch\n", + "\n", + "\n", + "\n", + "\n", + "read character into cell 9\n", + "set cell 10 to 1\n", + "while cell 9 != 0 # have an input\n", + " subtract cell 1 from cell 9\n", + " while cell 9 != 0 # we're not at an exit\n", + " set cell 10 to 0\n", + " subtract cell 2 from cell 9\n", + " while cell 9 != 0 # we're going down\n", + " set cell 11 to 0\n", + " set cell 12 to 1\n", + " decrement cell 6\n", + " while cell 6 != 0 # haven't just descended to ground floor\n", + " while cell 7 != 0 # above ground\n", + " decrement cell 7\n", + " set cell 12 to 0 to finish loop\n", + " end\n", + " end\n", + " end\n", + " \n", + " while cell 11 != 0 # now deal with going up\n", + " set cell 12 to 1\n", + " while cell 6 != 0 # not on ground before going up\n", + " while cell 7 != 0 # above ground\n", + " increment cell 7\n", + " set cell 12 to 0 to finish the inner loop\n", + " end\n", + " set cell 12 to 0 to finish the loop\n", + " end\n", + " increment cell 6\n", + " end\n", + " end\n", + " \n", + " while cell 10 != 0 (at an exit)\n", + " while cell 7 != 0 (above ground level)\n", + " copy cell 8 to cell 11 using cell 13 (highest)\n", + " copy cell 7 to cell 12 using cell 13 (current)\n", + " \n", + " set cell 14 to 0\n", + " while cell 11 != 0\n", + " while cell 12 != 0\n", + " decrement cell 12\n", + " move pointer to 14 to terminate inner loop\n", + " end\n", + " decrement cell 11\n", + " end\n", + " \n", + " add cell 12 to cell 8\n", + " end\n", + " \n", + " copy cell 1 into cell 0 using cell 4\n", + " read character into cell 9\n", + " set cell 10 to 1\n", + "end \n", + "```\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ "```\n", - "Hold highest exit in unary, as seq 10, 9, 8... 1, 0.\n", - "When get a new exit, set level in leftmost cell, rebuild the sequence.\n", - "If when you get to the end, there's a non-zero cell to the right, it wasn't longest.\n", - " move to the right until you reach zero, then rebuild the longest sequence right-to-left\n", - " when to stop?\n", + "set cell 1 to 61 # exit\n", + "set cell 2 to 94-61=33 # up\n", + "set cell 3 to 118-94=24 # down\n", + "copy cell 1 into cell 0, using cell 4\n", + "\n", + "cell 5 for ???? currently at an exit: 1 if at an exit 0 otherwise\n", + "\n", + "set cell 6 to 0 (current level)\n", + "set cell 7 to for height above ground, min zero\n", + "set cell 8 to 0 (highest exit)\n", + "cell 9 for input\n", + "cell 10 for whether input has been dealt with: 0 for yes 1 for no\n", + "cell 11 for whether we've dealt with the height above zero cell\n", + "reserve cell 12 and higher for scratch\n", + "\n", + "\n", + "read character into cell 9\n", + "set cell 10 to 1\n", + "while cell 9 != 0 # have an input\n", + " subtract cell 1 from cell 9\n", + " while cell 9 != 0 # we're not at an exit\n", + " set cell 10 to 0\n", + " subtract cell 2 from cell 9\n", + " while cell 9 != 0 # we're going down\n", + " \n", + " while cell 6 != 0 # aren't descending from ground floor\n", + " copy cell 7 to cell 11 using cell 12\n", + " set cell 12 to 0\n", + " while cell 11 != 0 # above ground\n", + " set cell 12 to 1\n", + " decrement cell 11\n", + " end\n", + " while cell 12 != 0\n", + " decrement cell 7\n", + " decrement cell 12\n", + " end\n", + " end\n", + " decrement cell 6\n", + "\n", + " end\n", + " \n", + " subtract cell 3 from cell 9\n", + " while cell 9 != 0 # we're going up\n", + " increment cell 6\n", + " while cell 6 != 0 # haven't just ascended to ground floor\n", + " copy cell 7 to cell 11 using cell 12\n", + " set cell 12 to 0\n", + " while cell 11 != 0\n", + " set cell 12 to 1\n", + " decrement cell 11\n", + " end\n", + " while cell 12 != 0\n", + " increment cell 7\n", + " decrement cell 12\n", + " end\n", + " end\n", + " end\n", + " \n", + " while cell 10 != 0 # at an exit\n", + " while cell 7 != 0 (above ground level)\n", + " copy cell 8 to cell 11 using cell 13 (highest)\n", + " copy cell 7 to cell 12 using cell 13 (current)\n", + " \n", + " # subtract 11 from 12, ensuring 12 >= 0\n", + " # add 12 to 8\n", + " \n", + " while cell 11 != 0\n", + " set cell 13 to 0\n", + " copy cell 12 to cell 14 using cell 15\n", + " while cell 14 != 0\n", + " set cell 13 to 1\n", + " decrement cell 14\n", + " end\n", + " while cell 13 != 0\n", + " decrement cell 12\n", + " decrement cell 13\n", + " end\n", + " decrement cell 11\n", + " end\n", + " \n", + " add cell 12 to cell 8\n", + " \n", + " copy cell 1 into cell 0 using cell 4\n", + " read character into cell 9\n", + " set cell 10 to 1\n", + "end \n", + "output cell 8\n", "```\n" ] }, @@ -157,6 +348,242 @@ "cell_type": "code", "execution_count": 3, "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + ">+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++>++++++++++++++++++++++++\n", + "\n", + "\n", + "read character into cell 9\n", + ">>>>>>,\n", + "\n", + "\n", + "\n", + "while cell 9 != 0 # have an input\n", + "[\n", + " set cell 10 to 1\n", + " >[-]+\n", + "\n", + " # clear cells 4 and 0\n", + " <<<<<<[-]<<<<[-]>\n", + " # copy cell 1 to cell 0 using cell 4\n", + " [-<+>>>>+<<<]\n", + " >>>[-<<<+>>>]\n", + " <<<<\n", + " \n", + " subtract cell 0 from cell 9 \n", + " [->>>>>>>>>-<<<<<<<<<]\n", + " >>>>>>>>>\n", + " \n", + " while cell 9 != 0 # we're not at an exit\n", + " [\n", + " set cell 10 to 0\n", + " >[-]\n", + " \n", + " copy cell 2 to cell 0 using cell 4\n", + " <<<<<<<<[-<<+>>>>+<<]\n", + " >>[-<<+>>]\n", + " <<<<\n", + " \n", + " subtract cell 0 from cell 9\n", + " [->>>>>>>>>-<<<<<<<<<]\n", + " >>>>>\n", + "\n", + " set cell 5 to 1\n", + " [-]+\n", + " >>>>\n", + "\n", + " \n", + " while cell 9 != 0 # we're going down\n", + " [\n", + " clear cell 5\n", + " <<<<[-]\n", + " copy cell 7 to cell 11 using cell 12\n", + " >>[->>>>+>+<<<<<]\n", + " >>>>>[-<<<<<+>>>>>]\n", + " \n", + " cell 12 is zero\n", + " \n", + " while cell 11 != 0 # above ground\n", + " <\n", + " [\n", + " set cell 12 to 1\n", + " >[-]+\n", + " \n", + " clear cell 11\n", + " <[-]\n", + " end\n", + " ] 11\n", + " \n", + " while cell 12 != 0\n", + " >\n", + " [\n", + " decrement cell 7\n", + " <<<<<-\n", + " \n", + " set cell 12 to zero\n", + " >>>>>[-]\n", + " end\n", + " ] 12\n", + " \n", + " <<<<<<\n", + " decrement cell 6\n", + " -\n", + " \n", + " have now dealt with the input so clear cell 9\n", + " >>>[-]\n", + " end\n", + " ] 9\n", + " \n", + " \n", + " while cell 5 != 0 # we're going up\n", + " <<<<\n", + " [\n", + " clear cell 5\n", + " [-]\n", + "\n", + "\n", + " # set cell 12 to 0\n", + " >>>>>>>[-]\n", + "\n", + " ### if 6 == 0 or 7 != 0\n", + " ### set cell 12 to 1\n", + "\n", + " # copy cell 6 to cell 11 using cell 12\n", + " <<<<<<[->>>>>+>+<<<<<<]\n", + " >>>>>>[-<<<<<<+>>>>>>]\n", + " \n", + " set cell 12 to 1\n", + " [-]+\n", + "\n", + " while cell 11 != 0\n", + " <\n", + " [\n", + " clear cell 12\n", + " >[-]\n", + "\n", + " set cell 11 to 0\n", + " <[-]\n", + " end 11\n", + " ] 11\n", + "\n", + "\n", + "\n", + " # copy cell 7 to cell 11 using cell 13\n", + " <<<<[->>>>+>>+<<<<<<]\n", + " >>>>>>[-<<<<<<+>>>>>>]\n", + "\n", + " # while cell 11 != 0\n", + " <<\n", + " [\n", + " set cell 12 to 1\n", + " >[-]+\n", + "\n", + " set cell 11 to 0\n", + " <[-]\n", + " \n", + " # end 11\n", + " ] 11\n", + " # add cell 12 to cell 7\n", + " >[-<<<<<+>>>>>]\n", + "\n", + " # increment cell 6\n", + " <<<<<<+\n", + " <\n", + " end\n", + " ] 5\n", + " \n", + " have now dealt with the non exit node\n", + " clear cell 9\n", + " >>>> \n", + " [-]\n", + " end\n", + " ] 9\n", + " \n", + " while cell 10 != 0 # at an exit\n", + " >\n", + " [\n", + " copy cell 7 to cell 12 using cell 13 (highest)\n", + " <<<[->>>>>+>+<<<<<<]\n", + " >>>>>>[-<<<<<<+>>>>>>]\n", + " \n", + " while cell 12 != 0 (above ground level)\n", + " <\n", + " [\n", + " copy cell 8 to cell 11 using cell 13 (highest)\n", + " <<<<[->>>+>>+<<<<<]\n", + " >>>>>[-<<<<<+>>>>>]\n", + " \n", + " cell 13 is zero\n", + " \n", + " ### subtract 11 from 12 ensuring 12 gte 0\n", + " ### add 12 to 8\n", + " \n", + " while cell 11 != 0\n", + " <<\n", + " [\n", + " copy cell 12 to cell 14 using cell 15\n", + " >[->>+>+<<<]\n", + " >>>[-<<<+>>>]\n", + " \n", + " while cell 14 != 0\n", + " <\n", + " [\n", + " set cell 13 to 1\n", + " <[-]+\n", + " decrement cell 14\n", + " >-\n", + " end\n", + " ] 14\n", + " while cell 13 != 0\n", + " <\n", + " [\n", + " decrement cell 12\n", + " <-\n", + " decrement cell 13\n", + " >-\n", + " end\n", + " ] 13\n", + " \n", + " decrement cell 11\n", + " <<-\n", + " end\n", + " ] 11\n", + " \n", + " >[-<<<<+>>>>]\n", + " add cell 12 to cell 8\n", + " ] 12\n", + " <<\n", + " clear 10\n", + " [-]\n", + " ] 10\n", + " \n", + " \n", + " \n", + " read character into cell 9\n", + " <,\n", + "end \n", + "] 9\n", + "\n", + "output cell 8\n", + "<.\n", + "\n", + "\n", + "\n" + ] + } + ], + "source": [ + "program = open('part2.bf').read()\n", + "print(program)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, "outputs": [ { "data": { @@ -164,7 +591,7 @@ "118" ] }, - "execution_count": 3, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -175,180 +602,132 @@ }, { "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "helloworld= '++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.'" - ] - }, - { - "cell_type": "code", - "execution_count": 20, + "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Hello World!\n" + "1\n" ] }, { "data": { "text/plain": [ - "([0, 0, 72, 100, 87, 33, 10], 106, 6)" + "([0, 61, 33, 24, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0], 698, 8, [1])" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "evaluate(program, inp='^=')" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "176223" ] }, - "execution_count": 20, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "evaluate(helloworld)" + "with io.capture_output() as captured:\n", + " evaluate(program, inp='^', debug=True)\n", + "\n", + "open('bf.log', 'w').write(captured.stdout)" ] }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "hello" + "1\n" ] }, { "data": { "text/plain": [ - "([111], 3, 0)" + "([0, 61, 33, 24, 0, 0, 3, 3, 1, 0, 0, 0, 0, 0], 698, 8, [1])" ] }, - "execution_count": 19, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "evaluate(',[.,]', input='hello')" - ] - }, - { - "cell_type": "code", - "execution_count": 196, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "program = '>' + '+' * 94 + '>' + '+' * 24\n", - "program += \"\"\"\n", - "copy cell 1 into cell 0 using cell 3\n", - "\n", - "<\n", - "[-<+>>>+<<]\n", - "\n", - ">>\n", - "[-<<+>>]\n", - "\n", - "read into cell 5\n", - ">>,\n", - "\n", - "[\n", - " subtract cell 0 from cell 5\n", - " <<<<<[->>>>>-<<<<<]\n", - " >>>>>\n", - "\n", - " if cell 5 != 0 do more\n", - " [\n", - "\n", - " copy cell 2 into cell 0 using cell 3\n", - " <<<[->+<<<+>>]\n", - "\n", - " move cell 3 into cell 2\n", - " >[-<+>]\n", - " <\n", - "\n", - " subtract cell 0 from cell 5\n", - " <<[->>>>>-<<<<<]\n", - " >>>>>\n", - "\n", - " if cell 5 != 0\n", - " [\n", - " increment cell 4 by 1\n", - " <+>\n", - "\n", - " clear cell 5 to stop the loop\n", - " [-]\n", - " ]\n", - " decrement cell 4 by 2\n", - " <-->\n", - "\n", - " ]\n", - "\n", - " increment cell 4 by 1\n", - " <+\n", - "\n", - " copy cell 1 into cell 0 using cell 3\n", - " <<<[-<+>>>+<<]\n", - "\n", - " move cell 3 into cell 1\n", - " >>[-<<+>>]\n", - "\n", - " >>\n", - "\n", - " read next input\n", - " ,\n", - "]\n", - "write the output\n", - "<.\n", - "\n", - "\"\"\"" + "evaluate(program, inp='^^v=^^^v')" ] }, { "cell_type": "code", - "execution_count": 197, + "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "([94, 94, 24, 0, 3, 0], 255, 5)" + "'>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++>++++++++++++++++++++++++>>>>>>,[>[-]+<<<<<<[-]<<<<[-]>[-<+>>>>+<<<]>>>[-<<<+>>>]<<<<[->>>>>>>>>-<<<<<<<<<]>>>>>>>>>[>[-]<<<<<<<<[-<<+>>>>+<<]>>[-<<+>>]<<<<[->>>>>>>>>-<<<<<<<<<]>>>>>[-]+>>>>[<<<<[-]>>[->>>>+>+<<<<<]>>>>>[-<<<<<+>>>>>]<[>[-]+<[-]]>[<<<<<->>>>>[-]]<<<<<<->>>[-]]<<<<[[-]>>>>>>>[-]<<<<<<[->>>>>+>+<<<<<<]>>>>>>[-<<<<<<+>>>>>>][-]+<[>[-]<[-]]<<<<[->>>>+>>+<<<<<<]>>>>>>[-<<<<<<+>>>>>>]<<[>[-]+<[-]]>[-<<<<<+>>>>>]<<<<<<+<]>>>>[-]]>[<<<[->>>>>+>+<<<<<<]>>>>>>[-<<<<<<+>>>>>>]<[<<<<[->>>+>>+<<<<<]>>>>>[-<<<<<+>>>>>]<<[>[->>+>+<<<]>>>[-<<<+>>>]<[<[-]+>-]<[<->-]<<-]>[-<<<<+>>>>]]<<[-]]<,]<.'" ] }, - "execution_count": 197, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "evaluate(program, inp='^^v=^^^v')" + "''.join(cleanup(program))" ] }, { "cell_type": "code", - "execution_count": 198, + "execution_count": 11, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "with open('part2.clean.bf', 'w') as f:\n", + " for i, c in enumerate(''.join(cleanup(program))):\n", + " f.write('{:03} {}\\n'.format(i, c))" + ] + }, + { + "cell_type": "code", + "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++<[-<+>>>+<<]>>[-<<+>>]>>,[<<<<<[->>>>>-<<<<<]>>>>>[<<<[->+<<<+>>]>[-<+>]<<<[->>>>>-<<<<<]>>>>>[<+>[-]]<-->]<+<<<[-<+>>>+<<]>>[-<<+>>]>>,]<.'" + "699" ] }, - "execution_count": 198, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "''.join(cleanup(program))" + "open('part2.clean.bf', 'w').write(''.join(cleanup(program))+'\\n')" ] }, { @@ -517,13 +896,33 @@ }, { "cell_type": "code", - "execution_count": 199, + "execution_count": 13, "metadata": { "collapsed": true }, "outputs": [], "source": [ - "! bf -n part1.bf < 02-lifts.txt > part1.bf.out" + "! bf -n part2.clean.bf < 02-lifts.txt > part2.bf.out" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[215]" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[int(b) for b in open('part2.bf.out', 'rb').read()]" ] }, {