From: Neil Smith Date: Sun, 16 Jul 2017 18:10:39 +0000 (+0100) Subject: Tidying up day 2 polyglot X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=35334be6542c54514e0a1bf11d247063c2c1ed51;p=ou-summer-of-code-2017.git Tidying up day 2 polyglot --- diff --git a/02-lifts/bf-meme.jpg b/02-lifts/bf-meme.jpg new file mode 100644 index 0000000..5f3ebea Binary files /dev/null and b/02-lifts/bf-meme.jpg differ diff --git a/02-lifts/bf.out b/02-lifts/bf.out new file mode 100644 index 0000000..e69de29 diff --git a/02-lifts/bf2.py b/02-lifts/bf2.py new file mode 100644 index 0000000..4170111 --- /dev/null +++ b/02-lifts/bf2.py @@ -0,0 +1,309 @@ +import sys + + +program = '>' + '+' * 61 + '>' + '+' * 33 + '>' + '+' * 24 +program += """ + + +read character into cell 9 +>>>>>>, + + + +while cell 9 != 0 # have an input +[ + set cell 10 to 1 + >[-]+ + + # clear cells 4 and 0 + <<<<<<[-]<<<<[-]> + # copy cell 1 to cell 0 using cell 4 + [-<+>>>>+<<<] + >>>[-<<<+>>>] + <<<< + + subtract cell 0 from cell 9 + [->>>>>>>>>-<<<<<<<<<] + >>>>>>>>> + + while cell 9 != 0 # we're not at an exit + [ + set cell 10 to 0 + >[-] + + copy cell 2 to cell 0 using cell 4 + <<<<<<<<[-<<+>>>>+<<] + >>[-<<+>>] + <<<< + + subtract cell 0 from cell 9 + [->>>>>>>>>-<<<<<<<<<] + >>>>> + + set cell 5 to 1 + [-]+ + >>>> + + + while cell 9 != 0 # we're going down + [ + clear cell 5 + <<<<[-] + copy cell 7 to cell 11 using cell 12 + >>[->>>>+>+<<<<<] + >>>>>[-<<<<<+>>>>>] + + cell 12 is zero + + while cell 11 != 0 # above ground + < + [ + set cell 12 to 1 + >[-]+ + + clear cell 11 + <[-] + end + ] 11 + + while cell 12 != 0 + > + [ + decrement cell 7 + <<<<<- + + set cell 12 to zero + >>>>>[-] + end + ] 12 + + <<<<<< + decrement cell 6 + - + + have now dealt with the input so clear cell 9 + >>>[-] + end + ] 9 + + + while cell 5 != 0 # we're going up + <<<< + [ + clear cell 5 + [-] + + + # set cell 12 to 0 + >>>>>>>[-] + + ### if 6 == 0 or 7 != 0 + ### set cell 12 to 1 + + # copy cell 6 to cell 11 using cell 12 + <<<<<<[->>>>>+>+<<<<<<] + >>>>>>[-<<<<<<+>>>>>>] + + set cell 12 to 1 + [-]+ + + while cell 11 != 0 + < + [ + clear cell 12 + >[-] + + set cell 11 to 0 + <[-] + end 11 + ] 11 + + + + # copy cell 7 to cell 11 using cell 13 + <<<<[->>>>+>>+<<<<<<] + >>>>>>[-<<<<<<+>>>>>>] + + # while cell 11 != 0 + << + [ + set cell 12 to 1 + >[-]+ + + set cell 11 to 0 + <[-] + + # end 11 + ] 11 + # add cell 12 to cell 7 + >[-<<<<<+>>>>>] + + # increment cell 6 + <<<<<<+ + < + end + ] 5 + + have now dealt with the non exit node + clear cell 9 + >>>> + [-] + end + ] 9 + + while cell 10 != 0 # at an exit + > + [ + copy cell 7 to cell 12 using cell 13 (highest) + <<<[->>>>>+>+<<<<<<] + >>>>>>[-<<<<<<+>>>>>>] + + while cell 12 != 0 (above ground level) + < + [ + copy cell 8 to cell 11 using cell 13 (highest) + <<<<[->>>+>>+<<<<<] + >>>>>[-<<<<<+>>>>>] + + cell 13 is zero + + ### subtract 11 from 12 ensuring 12 gte 0 + ### add 12 to 8 + + while cell 11 != 0 + << + [ + copy cell 12 to cell 14 using cell 15 + >[->>+>+<<<] + >>>[-<<<+>>>] + + while cell 14 != 0 + < + [ + set cell 13 to 1 + <[-]+ + decrement cell 14 + >- + end + ] 14 + while cell 13 != 0 + < + [ + decrement cell 12 + <- + decrement cell 13 + >- + end + ] 13 + + decrement cell 11 + <<- + end + ] 11 + + >[-<<<<+>>>>] + add cell 12 to cell 8 + ] 12 + << + clear 10 + [-] + ] 10 + + + + read character into cell 9 + <, +end +] 9 + +output cell 8 +<. + +""" + + +def execute(filename): + f = open(filename, "r") + evaluate(f.read()) + f.close() + + +def evaluate(code, inp=None, debug=False, execution_limit=0): + code = cleanup(list(code)) + bracemap = buildbracemap(code) + + cells, codeptr, cellptr = [0], 0, 0 + inputptr = 0 + execution_count = 0 + outputs = [] + + try: + while codeptr < len(code): + command = code[codeptr] + + if debug: + print(command, codeptr, cellptr, cells) + + if command == ">": + cellptr += 1 + if cellptr == len(cells): cells.append(0) + + if command == "<": + cellptr = 0 if cellptr <= 0 else cellptr - 1 + + if command == "+": + cells[cellptr] = cells[cellptr] + 1 if cells[cellptr] < 255 else 0 + + if command == "-": + cells[cellptr] = cells[cellptr] - 1 if cells[cellptr] > 0 else 255 + + if command == "[" and cells[cellptr] == 0: codeptr = bracemap[codeptr] + if command == "]" and cells[cellptr] != 0: codeptr = bracemap[codeptr] + if command == ".": + print(cells[cellptr]) # sys.stdout.write(chr(cells[cellptr])) + outputs += [cells[cellptr]] + if command == ",": + if inputptr >= len(inp): + # raise EOFError + cells[cellptr] = 0 + else: + cells[cellptr] = ord(inp[inputptr]) + inputptr += 1 + + codeptr += 1 + execution_count += 1 + if execution_limit != 0 and execution_count > execution_limit: + break + except EOFError: + pass + return cells, codeptr, cellptr, outputs + + +def cleanup(code): + return list(filter(lambda x: x in '.,[]<>+-', code)) + + +def buildbracemap(code): + temp_bracestack, bracemap = [], {} + + for position, command in enumerate(code): + if command == "[": temp_bracestack.append(position) + if command == "]": + start = temp_bracestack.pop() + bracemap[start] = position + bracemap[position] = start + return bracemap + +def main(): + + open('part2.bf', 'w').write(program + '\n') + inpt = '^=vvv=^^^=^=vv' + inpt = 'v^^^^^v=v=' + inpt = open('02-lifts.txt').read().strip() + # cells, codeptr, cellptr, outputs = evaluate(program, inp=inpt, execution_limit=200000, debug=True) + # cells, codeptr, cellptr, outputs = evaluate(program, inp=inpt, debug=True) + cells, codeptr, cellptr, outputs = evaluate(program, inp=inpt) + print(cells, codeptr, cellptr, outputs) + +if __name__ == "__main__": main() + \ No newline at end of file diff --git a/02-lifts/lifts-solution.ipynb b/02-lifts/lifts-solution.ipynb index bf60b78..723d33e 100644 --- a/02-lifts/lifts-solution.ipynb +++ b/02-lifts/lifts-solution.ipynb @@ -34,7 +34,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "metadata": { "collapsed": true }, @@ -51,7 +51,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 2, "metadata": { "collapsed": true }, @@ -66,7 +66,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -75,7 +75,7 @@ "209" ] }, - "execution_count": 4, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } @@ -89,7 +89,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "metadata": { "collapsed": true }, @@ -106,7 +106,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -115,7 +115,7 @@ "(10002, 216, -6)" ] }, - "execution_count": 6, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -136,7 +136,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -145,7 +145,7 @@ "209" ] }, - "execution_count": 7, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -168,7 +168,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 7, "metadata": { "collapsed": true }, @@ -187,7 +187,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -196,7 +196,7 @@ "215" ] }, - "execution_count": 9, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -207,7 +207,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -216,7 +216,7 @@ "-5" ] }, - "execution_count": 10, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -227,7 +227,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 10, "metadata": {}, "outputs": [ { @@ -236,7 +236,7 @@ "209" ] }, - "execution_count": 11, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } @@ -247,7 +247,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 11, "metadata": {}, "outputs": [ { @@ -256,7 +256,7 @@ "-2" ] }, - "execution_count": 12, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } @@ -267,7 +267,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 12, "metadata": {}, "outputs": [ { @@ -276,7 +276,7 @@ "1259" ] }, - "execution_count": 13, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -287,7 +287,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 13, "metadata": {}, "outputs": [ { @@ -479,7 +479,7 @@ " 215: 2})" ] }, - "execution_count": 14, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } @@ -489,6 +489,26 @@ "collections.Counter(exits(instructions))" ] }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "-2" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "max(exits(instructions[:20]))" + ] + }, { "cell_type": "code", "execution_count": null, diff --git a/02-lifts/part1-brainfuck.ipynb b/02-lifts/part1-brainfuck.ipynb index 608180d..06487a8 100644 --- a/02-lifts/part1-brainfuck.ipynb +++ b/02-lifts/part1-brainfuck.ipynb @@ -2,8 +2,10 @@ "cells": [ { "cell_type": "code", - "execution_count": 121, - "metadata": {}, + "execution_count": 2, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "#!/usr/bin/python\n", @@ -27,6 +29,7 @@ "\n", " cells, codeptr, cellptr = [0], 0, 0\n", " inputptr = 0\n", + " output = []\n", "\n", " try:\n", " while codeptr < len(code):\n", @@ -50,20 +53,22 @@ "\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 == \".\": output += [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, output\n", "\n", "\n", "def cleanup(code):\n", @@ -91,7 +96,7 @@ }, { "cell_type": "code", - "execution_count": 128, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -100,7 +105,7 @@ "(118, 94, 61)" ] }, - "execution_count": 128, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } @@ -136,7 +141,7 @@ }, { "cell_type": "code", - "execution_count": 129, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -145,7 +150,7 @@ "24" ] }, - "execution_count": 129, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -156,7 +161,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 5, "metadata": { "collapsed": true }, @@ -167,23 +172,19 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 6, "metadata": {}, "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hello World!\n" - ] - }, { "data": { "text/plain": [ - "([0, 0, 72, 100, 87, 33, 10], 106, 6)" + "([0, 0, 72, 100, 87, 33, 10],\n", + " 106,\n", + " 6,\n", + " [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 10])" ] }, - "execution_count": 20, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -194,35 +195,30 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 7, "metadata": {}, "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "hello" - ] - }, { "data": { "text/plain": [ - "([111], 3, 0)" + "([0], 5, 0, [104, 101, 108, 108, 111])" ] }, - "execution_count": 19, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "evaluate(',[.,]', input='hello')" + "evaluate(',[.,]', inp='hello')" ] }, { "cell_type": "code", - "execution_count": 196, - "metadata": {}, + "execution_count": 8, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "program = '>' + '+' * 94 + '>' + '+' * 24\n", @@ -292,16 +288,16 @@ }, { "cell_type": "code", - "execution_count": 200, + "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "([94, 94, 24, 0, 3, 0], 255, 5)" + "([94, 94, 24, 0, 3, 0], 259, 4, [3])" ] }, - "execution_count": 200, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -312,7 +308,7 @@ }, { "cell_type": "code", - "execution_count": 198, + "execution_count": 10, "metadata": {}, "outputs": [ { @@ -321,7 +317,7 @@ "'>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++<[-<+>>>+<<]>>[-<<+>>]>>,[<<<<<[->>>>>-<<<<<]>>>>>[<<<[->+<<<+>>]>[-<+>]<<<[->>>>>-<<<<<]>>>>>[<+>[-]]<-->]<+<<<[-<+>>>+<<]>>[-<<+>>]>>,]<.'" ] }, - "execution_count": 198, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } @@ -332,16 +328,47 @@ }, { "cell_type": "code", - "execution_count": 123, + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "with open('part1.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": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "([60, 60, 0, 15, 0], 152, 4)" + "260" ] }, - "execution_count": 123, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "open('part1.clean.bf', 'w').write(''.join(cleanup(program))+'\\n')" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "([94, 94, 24, 0, 209, 0], 259, 4, [209])" + ] + }, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -353,7 +380,7 @@ }, { "cell_type": "code", - "execution_count": 124, + "execution_count": 13, "metadata": { "collapsed": true }, @@ -370,7 +397,7 @@ }, { "cell_type": "code", - "execution_count": 125, + "execution_count": 14, "metadata": { "collapsed": true }, @@ -385,7 +412,7 @@ }, { "cell_type": "code", - "execution_count": 126, + "execution_count": 15, "metadata": {}, "outputs": [ { @@ -394,7 +421,7 @@ "209" ] }, - "execution_count": 126, + "execution_count": 15, "metadata": {}, "output_type": "execute_result" } @@ -405,7 +432,7 @@ }, { "cell_type": "code", - "execution_count": 193, + "execution_count": 16, "metadata": { "scrolled": true }, @@ -414,56 +441,56 @@ "name": "stdout", "output_type": "stream", "text": [ - "0 ([94, 94, 24, 0, 0, 0], 144, 5) \n", - "-1 ([94, 94, 24, 0, 255, 0], 255, 5) v\n", - "-2 ([94, 94, 24, 0, 254, 0], 255, 5) vv\n", - "-3 ([94, 94, 24, 0, 253, 0], 255, 5) vvv\n", - "-2 ([94, 94, 24, 0, 254, 0], 255, 5) vvv^\n", - "-1 ([94, 94, 24, 0, 255, 0], 255, 5) vvv^^\n", - "-2 ([94, 94, 24, 0, 254, 0], 255, 5) vvv^^v\n", - "-1 ([94, 94, 24, 0, 255, 0], 255, 5) vvv^^v^\n", - "-2 ([94, 94, 24, 0, 254, 0], 255, 5) vvv^^v^v\n", - "-2 ([94, 94, 24, 0, 254, 0], 255, 5) vvv^^v^v=\n", - "-3 ([94, 94, 24, 0, 253, 0], 255, 5) vvv^^v^v=v\n", - "-4 ([94, 94, 24, 0, 252, 0], 255, 5) vvv^^v^v=vv\n", - "-3 ([94, 94, 24, 0, 253, 0], 255, 5) vvv^^v^v=vv^\n", - "-4 ([94, 94, 24, 0, 252, 0], 255, 5) vvv^^v^v=vv^v\n", - "-3 ([94, 94, 24, 0, 253, 0], 255, 5) vvv^^v^v=vv^v^\n", - "-2 ([94, 94, 24, 0, 254, 0], 255, 5) vvv^^v^v=vv^v^^\n", - "-3 ([94, 94, 24, 0, 253, 0], 255, 5) vvv^^v^v=vv^v^^v\n", - "-4 ([94, 94, 24, 0, 252, 0], 255, 5) vvv^^v^v=vv^v^^vv\n", - "-5 ([94, 94, 24, 0, 251, 0], 255, 5) vvv^^v^v=vv^v^^vvv\n", - "-6 ([94, 94, 24, 0, 250, 0], 255, 5) vvv^^v^v=vv^v^^vvvv\n", - "-5 ([94, 94, 24, 0, 251, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^\n", - "-4 ([94, 94, 24, 0, 252, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^\n", - "-5 ([94, 94, 24, 0, 251, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v\n", - "-5 ([94, 94, 24, 0, 251, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=\n", - "-4 ([94, 94, 24, 0, 252, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^\n", - "-4 ([94, 94, 24, 0, 252, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=\n", - "-3 ([94, 94, 24, 0, 253, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^\n", - "-2 ([94, 94, 24, 0, 254, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^\n", - "-2 ([94, 94, 24, 0, 254, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=\n", - "-1 ([94, 94, 24, 0, 255, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^\n", - "0 ([94, 94, 24, 0, 0, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^\n", - "1 ([94, 94, 24, 0, 1, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^\n", - "1 ([94, 94, 24, 0, 1, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=\n", - "2 ([94, 94, 24, 0, 2, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^\n", - "3 ([94, 94, 24, 0, 3, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^\n", - "4 ([94, 94, 24, 0, 4, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^\n", - "3 ([94, 94, 24, 0, 3, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v\n", - "3 ([94, 94, 24, 0, 3, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=\n", - "4 ([94, 94, 24, 0, 4, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^\n", - "5 ([94, 94, 24, 0, 5, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^\n", - "6 ([94, 94, 24, 0, 6, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^\n", - "6 ([94, 94, 24, 0, 6, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^=\n", - "7 ([94, 94, 24, 0, 7, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^=^\n", - "8 ([94, 94, 24, 0, 8, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^=^^\n", - "7 ([94, 94, 24, 0, 7, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^=^^v\n", - "8 ([94, 94, 24, 0, 8, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^=^^v^\n", - "9 ([94, 94, 24, 0, 9, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^=^^v^^\n", - "8 ([94, 94, 24, 0, 8, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^=^^v^^v\n", - "9 ([94, 94, 24, 0, 9, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^=^^v^^v^\n", - "8 ([94, 94, 24, 0, 8, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^=^^v^^v^v\n" + "0 ([94, 94, 24, 0, 0, 0], 259, 4, [0]) \n", + "-1 ([94, 94, 24, 0, 255, 0], 259, 4, [255]) v\n", + "-2 ([94, 94, 24, 0, 254, 0], 259, 4, [254]) vv\n", + "-3 ([94, 94, 24, 0, 253, 0], 259, 4, [253]) vvv\n", + "-2 ([94, 94, 24, 0, 254, 0], 259, 4, [254]) vvv^\n", + "-1 ([94, 94, 24, 0, 255, 0], 259, 4, [255]) vvv^^\n", + "-2 ([94, 94, 24, 0, 254, 0], 259, 4, [254]) vvv^^v\n", + "-1 ([94, 94, 24, 0, 255, 0], 259, 4, [255]) vvv^^v^\n", + "-2 ([94, 94, 24, 0, 254, 0], 259, 4, [254]) vvv^^v^v\n", + "-2 ([94, 94, 24, 0, 254, 0], 259, 4, [254]) vvv^^v^v=\n", + "-3 ([94, 94, 24, 0, 253, 0], 259, 4, [253]) vvv^^v^v=v\n", + "-4 ([94, 94, 24, 0, 252, 0], 259, 4, [252]) vvv^^v^v=vv\n", + "-3 ([94, 94, 24, 0, 253, 0], 259, 4, [253]) vvv^^v^v=vv^\n", + "-4 ([94, 94, 24, 0, 252, 0], 259, 4, [252]) vvv^^v^v=vv^v\n", + "-3 ([94, 94, 24, 0, 253, 0], 259, 4, [253]) vvv^^v^v=vv^v^\n", + "-2 ([94, 94, 24, 0, 254, 0], 259, 4, [254]) vvv^^v^v=vv^v^^\n", + "-3 ([94, 94, 24, 0, 253, 0], 259, 4, [253]) vvv^^v^v=vv^v^^v\n", + "-4 ([94, 94, 24, 0, 252, 0], 259, 4, [252]) vvv^^v^v=vv^v^^vv\n", + "-5 ([94, 94, 24, 0, 251, 0], 259, 4, [251]) vvv^^v^v=vv^v^^vvv\n", + "-6 ([94, 94, 24, 0, 250, 0], 259, 4, [250]) vvv^^v^v=vv^v^^vvvv\n", + "-5 ([94, 94, 24, 0, 251, 0], 259, 4, [251]) vvv^^v^v=vv^v^^vvvv^\n", + "-4 ([94, 94, 24, 0, 252, 0], 259, 4, [252]) vvv^^v^v=vv^v^^vvvv^^\n", + "-5 ([94, 94, 24, 0, 251, 0], 259, 4, [251]) vvv^^v^v=vv^v^^vvvv^^v\n", + "-5 ([94, 94, 24, 0, 251, 0], 259, 4, [251]) vvv^^v^v=vv^v^^vvvv^^v=\n", + "-4 ([94, 94, 24, 0, 252, 0], 259, 4, [252]) vvv^^v^v=vv^v^^vvvv^^v=^\n", + "-4 ([94, 94, 24, 0, 252, 0], 259, 4, [252]) vvv^^v^v=vv^v^^vvvv^^v=^=\n", + "-3 ([94, 94, 24, 0, 253, 0], 259, 4, [253]) vvv^^v^v=vv^v^^vvvv^^v=^=^\n", + "-2 ([94, 94, 24, 0, 254, 0], 259, 4, [254]) vvv^^v^v=vv^v^^vvvv^^v=^=^^\n", + "-2 ([94, 94, 24, 0, 254, 0], 259, 4, [254]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=\n", + "-1 ([94, 94, 24, 0, 255, 0], 259, 4, [255]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^\n", + "0 ([94, 94, 24, 0, 0, 0], 259, 4, [0]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^\n", + "1 ([94, 94, 24, 0, 1, 0], 259, 4, [1]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^\n", + "1 ([94, 94, 24, 0, 1, 0], 259, 4, [1]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=\n", + "2 ([94, 94, 24, 0, 2, 0], 259, 4, [2]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^\n", + "3 ([94, 94, 24, 0, 3, 0], 259, 4, [3]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^\n", + "4 ([94, 94, 24, 0, 4, 0], 259, 4, [4]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^\n", + "3 ([94, 94, 24, 0, 3, 0], 259, 4, [3]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v\n", + "3 ([94, 94, 24, 0, 3, 0], 259, 4, [3]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=\n", + "4 ([94, 94, 24, 0, 4, 0], 259, 4, [4]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^\n", + "5 ([94, 94, 24, 0, 5, 0], 259, 4, [5]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^\n", + "6 ([94, 94, 24, 0, 6, 0], 259, 4, [6]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^\n", + "6 ([94, 94, 24, 0, 6, 0], 259, 4, [6]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^=\n", + "7 ([94, 94, 24, 0, 7, 0], 259, 4, [7]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^=^\n", + "8 ([94, 94, 24, 0, 8, 0], 259, 4, [8]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^=^^\n", + "7 ([94, 94, 24, 0, 7, 0], 259, 4, [7]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^=^^v\n", + "8 ([94, 94, 24, 0, 8, 0], 259, 4, [8]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^=^^v^\n", + "9 ([94, 94, 24, 0, 9, 0], 259, 4, [9]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^=^^v^^\n", + "8 ([94, 94, 24, 0, 8, 0], 259, 4, [8]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^=^^v^^v\n", + "9 ([94, 94, 24, 0, 9, 0], 259, 4, [9]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^=^^v^^v^\n", + "8 ([94, 94, 24, 0, 8, 0], 259, 4, [8]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^=^^v^^v^v\n" ] } ], @@ -474,7 +501,7 @@ }, { "cell_type": "code", - "execution_count": 194, + "execution_count": 17, "metadata": { "scrolled": true }, @@ -482,10 +509,10 @@ { "data": { "text/plain": [ - "(209, ([94, 94, 24, 0, 209, 0], 255, 5))" + "(209, ([94, 94, 24, 0, 209, 0], 259, 4, [209]))" ] }, - "execution_count": 194, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } @@ -496,13 +523,33 @@ }, { "cell_type": "code", - "execution_count": 199, + "execution_count": 21, "metadata": { "collapsed": true }, "outputs": [], "source": [ - "! bf -n part1.bf < 02-lifts.txt > part1.bf.out" + "! bf -n part1.clean.bf < 02-lifts.txt > part1.bf.out" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[209]" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[int(b) for b in open('part1.bf.out', 'rb').read()]" ] }, { diff --git a/02-lifts/part1.clean.bf b/02-lifts/part1.clean.bf new file mode 100644 index 0000000..2199595 --- /dev/null +++ b/02-lifts/part1.clean.bf @@ -0,0 +1 @@ +>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++<[-<+>>>+<<]>>[-<<+>>]>>,[<<<<<[->>>>>-<<<<<]>>>>>[<<<[->+<<<+>>]>[-<+>]<<<[->>>>>-<<<<<]>>>>>[<+>[-]]<-->]<+<<<[-<+>>>+<<]>>[-<<+>>]>>,]<. diff --git a/02-lifts/part2-brainfuck.ipynb b/02-lifts/part2-brainfuck.ipynb index 0452e3b..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" } @@ -135,7 +151,7 @@ " subtract cell 0 from cell 9\n", " if cell 9 == 0 we're at an exit\n", " if cell 7 != 0\n", - " if cell 6 is higher then cell 7\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", @@ -148,59 +164,183 @@ " 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", - "```\n", "read character into cell 9\n", "set cell 10 to 1\n", - "while cell 9 != 0\n", - " subtract cell 0 from cell 9\n", - " while cell 9 == 0 we're not at an exit\n", - " subtract cell 1 from cell 9\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", - " while cell 9 == 0 we're going up\n", - " increment cell 6\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", - " while cell 6 is zero\n", - " set cell 7 to zero \n", - " decrement 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 6 to cell 12 using cell 13 (current)\n", + " copy cell 7 to cell 12 using cell 13 (current)\n", + " \n", " set cell 14 to 0\n", " while cell 11 != 0\n", - " increment cell 14\n", " while cell 12 != 0\n", " decrement cell 12\n", - " set cell 14 to 0, keep pointer at 14 (this exits the inner loop)\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", - " set cell 7 to 0\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", + "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", - " \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", - " \n", - " \n", - " if cell 7 != 0\n", - " if cell 6 is higher then cell 7\n", - " copy cell 6 into cell 7\n", - " else\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 6\n", - "```\n", - "\n", - "```\n", - "Need a flag to say if below ground level, and only update the highest exit level if positive\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", + " read character into cell 9\n", + " set cell 10 to 1\n", + "end \n", + "output cell 8\n", "```\n" ] }, @@ -208,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": { @@ -215,7 +591,7 @@ "118" ] }, - "execution_count": 3, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -226,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": 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": 198, + "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')" ] }, { @@ -568,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()]" ] }, { diff --git a/02-lifts/part2.bf b/02-lifts/part2.bf new file mode 100644 index 0000000..0b430f4 --- /dev/null +++ b/02-lifts/part2.bf @@ -0,0 +1,218 @@ +>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++>++++++++++++++++++++++++ + + +read character into cell 9 +>>>>>>, + + + +while cell 9 != 0 # have an input +[ + set cell 10 to 1 + >[-]+ + + # clear cells 4 and 0 + <<<<<<[-]<<<<[-]> + # copy cell 1 to cell 0 using cell 4 + [-<+>>>>+<<<] + >>>[-<<<+>>>] + <<<< + + subtract cell 0 from cell 9 + [->>>>>>>>>-<<<<<<<<<] + >>>>>>>>> + + while cell 9 != 0 # we're not at an exit + [ + set cell 10 to 0 + >[-] + + copy cell 2 to cell 0 using cell 4 + <<<<<<<<[-<<+>>>>+<<] + >>[-<<+>>] + <<<< + + subtract cell 0 from cell 9 + [->>>>>>>>>-<<<<<<<<<] + >>>>> + + set cell 5 to 1 + [-]+ + >>>> + + + while cell 9 != 0 # we're going down + [ + clear cell 5 + <<<<[-] + copy cell 7 to cell 11 using cell 12 + >>[->>>>+>+<<<<<] + >>>>>[-<<<<<+>>>>>] + + cell 12 is zero + + while cell 11 != 0 # above ground + < + [ + set cell 12 to 1 + >[-]+ + + clear cell 11 + <[-] + end + ] 11 + + while cell 12 != 0 + > + [ + decrement cell 7 + <<<<<- + + set cell 12 to zero + >>>>>[-] + end + ] 12 + + <<<<<< + decrement cell 6 + - + + have now dealt with the input so clear cell 9 + >>>[-] + end + ] 9 + + + while cell 5 != 0 # we're going up + <<<< + [ + clear cell 5 + [-] + + + # set cell 12 to 0 + >>>>>>>[-] + + ### if 6 == 0 or 7 != 0 + ### set cell 12 to 1 + + # copy cell 6 to cell 11 using cell 12 + <<<<<<[->>>>>+>+<<<<<<] + >>>>>>[-<<<<<<+>>>>>>] + + set cell 12 to 1 + [-]+ + + while cell 11 != 0 + < + [ + clear cell 12 + >[-] + + set cell 11 to 0 + <[-] + end 11 + ] 11 + + + + # copy cell 7 to cell 11 using cell 13 + <<<<[->>>>+>>+<<<<<<] + >>>>>>[-<<<<<<+>>>>>>] + + # while cell 11 != 0 + << + [ + set cell 12 to 1 + >[-]+ + + set cell 11 to 0 + <[-] + + # end 11 + ] 11 + # add cell 12 to cell 7 + >[-<<<<<+>>>>>] + + # increment cell 6 + <<<<<<+ + < + end + ] 5 + + have now dealt with the non exit node + clear cell 9 + >>>> + [-] + end + ] 9 + + while cell 10 != 0 # at an exit + > + [ + copy cell 7 to cell 12 using cell 13 (highest) + <<<[->>>>>+>+<<<<<<] + >>>>>>[-<<<<<<+>>>>>>] + + while cell 12 != 0 (above ground level) + < + [ + copy cell 8 to cell 11 using cell 13 (highest) + <<<<[->>>+>>+<<<<<] + >>>>>[-<<<<<+>>>>>] + + cell 13 is zero + + ### subtract 11 from 12 ensuring 12 gte 0 + ### add 12 to 8 + + while cell 11 != 0 + << + [ + copy cell 12 to cell 14 using cell 15 + >[->>+>+<<<] + >>>[-<<<+>>>] + + while cell 14 != 0 + < + [ + set cell 13 to 1 + <[-]+ + decrement cell 14 + >- + end + ] 14 + while cell 13 != 0 + < + [ + decrement cell 12 + <- + decrement cell 13 + >- + end + ] 13 + + decrement cell 11 + <<- + end + ] 11 + + >[-<<<<+>>>>] + add cell 12 to cell 8 + ] 12 + << + clear 10 + [-] + ] 10 + + + + read character into cell 9 + <, +end +] 9 + +output cell 8 +<. + + diff --git a/02-lifts/part2.bf.out b/02-lifts/part2.bf.out new file mode 100644 index 0000000..3416407 --- /dev/null +++ b/02-lifts/part2.bf.out @@ -0,0 +1 @@ +× \ No newline at end of file diff --git a/02-lifts/part2.clean.bf b/02-lifts/part2.clean.bf new file mode 100644 index 0000000..ad7e9af --- /dev/null +++ b/02-lifts/part2.clean.bf @@ -0,0 +1 @@ +>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++>++++++++++++++++++++++++>>>>>>,[>[-]+<<<<<<[-]<<<<[-]>[-<+>>>>+<<<]>>>[-<<<+>>>]<<<<[->>>>>>>>>-<<<<<<<<<]>>>>>>>>>[>[-]<<<<<<<<[-<<+>>>>+<<]>>[-<<+>>]<<<<[->>>>>>>>>-<<<<<<<<<]>>>>>[-]+>>>>[<<<<[-]>>[->>>>+>+<<<<<]>>>>>[-<<<<<+>>>>>]<[>[-]+<[-]]>[<<<<<->>>>>[-]]<<<<<<->>>[-]]<<<<[[-]>>>>>>>[-]<<<<<<[->>>>>+>+<<<<<<]>>>>>>[-<<<<<<+>>>>>>][-]+<[>[-]<[-]]<<<<[->>>>+>>+<<<<<<]>>>>>>[-<<<<<<+>>>>>>]<<[>[-]+<[-]]>[-<<<<<+>>>>>]<<<<<<+<]>>>>[-]]>[<<<[->>>>>+>+<<<<<<]>>>>>>[-<<<<<<+>>>>>>]<[<<<<[->>>+>>+<<<<<]>>>>>[-<<<<<+>>>>>]<<[>[->>+>+<<<]>>>[-<<<+>>>]<[<[-]+>-]<[<->-]<<-]>[-<<<<+>>>>]]<<[-]]<,]<.