11 "#!/usr/bin/python\n",
13 "# Brainfuck Interpreter\n",
14 "# Copyright 2011 Sebastian Kaspari\n",
16 "# Usage: ./brainfuck.py [FILE]\n",
20 "def execute(filename):\n",
21 " f = open(filename, \"r\")\n",
22 " evaluate(f.read())\n",
26 "def evaluate(code, inp=None, debug=False):\n",
27 " code = cleanup(list(code))\n",
28 " bracemap = buildbracemap(code)\n",
30 " cells, codeptr, cellptr = [0], 0, 0\n",
35 " while codeptr < len(code):\n",
36 " command = code[codeptr]\n",
39 " print(command, cellptr, cells)\n",
41 " if command == \">\":\n",
43 " if cellptr == len(cells): cells.append(0)\n",
45 " if command == \"<\":\n",
46 " cellptr = 0 if cellptr <= 0 else cellptr - 1\n",
48 " if command == \"+\":\n",
49 " cells[cellptr] = cells[cellptr] + 1 if cells[cellptr] < 255 else 0\n",
51 " if command == \"-\":\n",
52 " cells[cellptr] = cells[cellptr] - 1 if cells[cellptr] > 0 else 255\n",
54 " if command == \"[\" and cells[cellptr] == 0: codeptr = bracemap[codeptr]\n",
55 " if command == \"]\" and cells[cellptr] != 0: codeptr = bracemap[codeptr]\n",
56 " if command == \".\": output += [cells[cellptr]] # sys.stdout.write(chr(cells[cellptr]))\n",
57 " if command == \",\": \n",
58 " if inp is not None:\n",
59 " if inputptr >= len(inp):\n",
60 " # raise EOFError\n",
61 " cells[cellptr] = 0\n",
63 " cells[cellptr] = ord(inp[inputptr])\n",
66 " cells[cellptr] = ord(getch.getch())\n",
69 " except EOFError:\n",
71 " return cells, codeptr, cellptr, output\n",
74 "def cleanup(code):\n",
75 " return list(filter(lambda x: x in ['.', ',', '[', ']', '<', '>', '+', '-'], code))\n",
78 "def buildbracemap(code):\n",
79 " temp_bracestack, bracemap = [], {}\n",
81 " for position, command in enumerate(code):\n",
82 " if command == \"[\": temp_bracestack.append(position)\n",
83 " if command == \"]\":\n",
84 " start = temp_bracestack.pop()\n",
85 " bracemap[start] = position\n",
86 " bracemap[position] = start\n",
91 "# if len(sys.argv) == 2: execute(sys.argv[1])\n",
92 "# else: print(\"Usage:\", sys.argv[0], \"filename\")\n",
94 "# if __name__ == \"__main__\": main()\n"
108 "execution_count": 3,
110 "output_type": "execute_result"
114 "ord('v'), ord('^'), ord('=')"
118 "cell_type": "markdown",
122 "set cell 1 to 94\n",
123 "set cell 2 to 118-94=24\n",
124 "copy cell 1 into cell 0, using cell 3\n",
127 "read character into cell 6\n",
128 "while cell 6 != 0\n",
129 " subtract cell 0 from cell 6\n",
131 " increment cell 5\n",
133 " copy cell 2 into cell 0, using cell 3\n",
134 " subtract cell 0 from cell 6\n",
136 " decrement cell 5\n",
137 " read character into cell 6\n",
144 "execution_count": 4,
153 "execution_count": 4,
155 "output_type": "execute_result"
164 "execution_count": 5,
170 "helloworld= '++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.'"
175 "execution_count": 6,
181 "([0, 0, 72, 100, 87, 33, 10],\n",
184 " [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 10])"
187 "execution_count": 6,
189 "output_type": "execute_result"
193 "evaluate(helloworld)"
198 "execution_count": 7,
204 "([0], 5, 0, [104, 101, 108, 108, 111])"
207 "execution_count": 7,
209 "output_type": "execute_result"
213 "evaluate(',[.,]', inp='hello')"
218 "execution_count": 8,
224 "program = '>' + '+' * 94 + '>' + '+' * 24\n",
225 "program += \"\"\"\n",
226 "copy cell 1 into cell 0 using cell 3\n",
234 "read into cell 5\n",
238 " subtract cell 0 from cell 5\n",
239 " <<<<<[->>>>>-<<<<<]\n",
242 " if cell 5 != 0 do more\n",
245 " copy cell 2 into cell 0 using cell 3\n",
248 " move cell 3 into cell 2\n",
252 " subtract cell 0 from cell 5\n",
253 " <<[->>>>>-<<<<<]\n",
258 " increment cell 4 by 1\n",
261 " clear cell 5 to stop the loop\n",
264 " decrement cell 4 by 2\n",
269 " increment cell 4 by 1\n",
272 " copy cell 1 into cell 0 using cell 3\n",
275 " move cell 3 into cell 1\n",
280 " read next input\n",
283 "write the output\n",
291 "execution_count": 9,
297 "([94, 94, 24, 0, 3, 0], 259, 4, [3])"
300 "execution_count": 9,
302 "output_type": "execute_result"
306 "evaluate(program, inp='^^v=^^^v')"
311 "execution_count": 10,
317 "'>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++<[-<+>>>+<<]>>[-<<+>>]>>,[<<<<<[->>>>>-<<<<<]>>>>>[<<<[->+<<<+>>]>[-<+>]<<<[->>>>>-<<<<<]>>>>>[<+>[-]]<-->]<+<<<[-<+>>>+<<]>>[-<<+>>]>>,]<.'"
320 "execution_count": 10,
322 "output_type": "execute_result"
326 "''.join(cleanup(program))"
331 "execution_count": 11,
335 "with open('part1.clean.bf', 'w') as f:\n",
336 " for i, c in enumerate(''.join(cleanup(program))):\n",
337 " f.write('{:03} {}\\n'.format(i, c))"
342 "execution_count": 20,
351 "execution_count": 20,
353 "output_type": "execute_result"
357 "open('part1.clean.bf', 'w').write(''.join(cleanup(program))+'\\n')"
362 "execution_count": 12,
368 "([94, 94, 24, 0, 209, 0], 259, 4, [209])"
371 "execution_count": 12,
373 "output_type": "execute_result"
377 "inp = open('02-lifts.txt').read().strip()\n",
378 "evaluate(program, inp=inp)"
383 "execution_count": 13,
389 "def value(instr):\n",
390 " if instr == '^':\n",
392 " elif instr == 'v':\n",
400 "execution_count": 14,
406 "def final(sequence):\n",
408 " for c in sequence:\n",
409 " current += value(c)\n",
415 "execution_count": 15,
424 "execution_count": 15,
426 "output_type": "execute_result"
435 "execution_count": 16,
442 "output_type": "stream",
444 "0 ([94, 94, 24, 0, 0, 0], 259, 4, [0]) \n",
445 "-1 ([94, 94, 24, 0, 255, 0], 259, 4, [255]) v\n",
446 "-2 ([94, 94, 24, 0, 254, 0], 259, 4, [254]) vv\n",
447 "-3 ([94, 94, 24, 0, 253, 0], 259, 4, [253]) vvv\n",
448 "-2 ([94, 94, 24, 0, 254, 0], 259, 4, [254]) vvv^\n",
449 "-1 ([94, 94, 24, 0, 255, 0], 259, 4, [255]) vvv^^\n",
450 "-2 ([94, 94, 24, 0, 254, 0], 259, 4, [254]) vvv^^v\n",
451 "-1 ([94, 94, 24, 0, 255, 0], 259, 4, [255]) vvv^^v^\n",
452 "-2 ([94, 94, 24, 0, 254, 0], 259, 4, [254]) vvv^^v^v\n",
453 "-2 ([94, 94, 24, 0, 254, 0], 259, 4, [254]) vvv^^v^v=\n",
454 "-3 ([94, 94, 24, 0, 253, 0], 259, 4, [253]) vvv^^v^v=v\n",
455 "-4 ([94, 94, 24, 0, 252, 0], 259, 4, [252]) vvv^^v^v=vv\n",
456 "-3 ([94, 94, 24, 0, 253, 0], 259, 4, [253]) vvv^^v^v=vv^\n",
457 "-4 ([94, 94, 24, 0, 252, 0], 259, 4, [252]) vvv^^v^v=vv^v\n",
458 "-3 ([94, 94, 24, 0, 253, 0], 259, 4, [253]) vvv^^v^v=vv^v^\n",
459 "-2 ([94, 94, 24, 0, 254, 0], 259, 4, [254]) vvv^^v^v=vv^v^^\n",
460 "-3 ([94, 94, 24, 0, 253, 0], 259, 4, [253]) vvv^^v^v=vv^v^^v\n",
461 "-4 ([94, 94, 24, 0, 252, 0], 259, 4, [252]) vvv^^v^v=vv^v^^vv\n",
462 "-5 ([94, 94, 24, 0, 251, 0], 259, 4, [251]) vvv^^v^v=vv^v^^vvv\n",
463 "-6 ([94, 94, 24, 0, 250, 0], 259, 4, [250]) vvv^^v^v=vv^v^^vvvv\n",
464 "-5 ([94, 94, 24, 0, 251, 0], 259, 4, [251]) vvv^^v^v=vv^v^^vvvv^\n",
465 "-4 ([94, 94, 24, 0, 252, 0], 259, 4, [252]) vvv^^v^v=vv^v^^vvvv^^\n",
466 "-5 ([94, 94, 24, 0, 251, 0], 259, 4, [251]) vvv^^v^v=vv^v^^vvvv^^v\n",
467 "-5 ([94, 94, 24, 0, 251, 0], 259, 4, [251]) vvv^^v^v=vv^v^^vvvv^^v=\n",
468 "-4 ([94, 94, 24, 0, 252, 0], 259, 4, [252]) vvv^^v^v=vv^v^^vvvv^^v=^\n",
469 "-4 ([94, 94, 24, 0, 252, 0], 259, 4, [252]) vvv^^v^v=vv^v^^vvvv^^v=^=\n",
470 "-3 ([94, 94, 24, 0, 253, 0], 259, 4, [253]) vvv^^v^v=vv^v^^vvvv^^v=^=^\n",
471 "-2 ([94, 94, 24, 0, 254, 0], 259, 4, [254]) vvv^^v^v=vv^v^^vvvv^^v=^=^^\n",
472 "-2 ([94, 94, 24, 0, 254, 0], 259, 4, [254]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=\n",
473 "-1 ([94, 94, 24, 0, 255, 0], 259, 4, [255]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^\n",
474 "0 ([94, 94, 24, 0, 0, 0], 259, 4, [0]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^\n",
475 "1 ([94, 94, 24, 0, 1, 0], 259, 4, [1]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^\n",
476 "1 ([94, 94, 24, 0, 1, 0], 259, 4, [1]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=\n",
477 "2 ([94, 94, 24, 0, 2, 0], 259, 4, [2]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^\n",
478 "3 ([94, 94, 24, 0, 3, 0], 259, 4, [3]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^\n",
479 "4 ([94, 94, 24, 0, 4, 0], 259, 4, [4]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^\n",
480 "3 ([94, 94, 24, 0, 3, 0], 259, 4, [3]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v\n",
481 "3 ([94, 94, 24, 0, 3, 0], 259, 4, [3]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=\n",
482 "4 ([94, 94, 24, 0, 4, 0], 259, 4, [4]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^\n",
483 "5 ([94, 94, 24, 0, 5, 0], 259, 4, [5]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^\n",
484 "6 ([94, 94, 24, 0, 6, 0], 259, 4, [6]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^\n",
485 "6 ([94, 94, 24, 0, 6, 0], 259, 4, [6]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^=\n",
486 "7 ([94, 94, 24, 0, 7, 0], 259, 4, [7]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^=^\n",
487 "8 ([94, 94, 24, 0, 8, 0], 259, 4, [8]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^=^^\n",
488 "7 ([94, 94, 24, 0, 7, 0], 259, 4, [7]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^=^^v\n",
489 "8 ([94, 94, 24, 0, 8, 0], 259, 4, [8]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^=^^v^\n",
490 "9 ([94, 94, 24, 0, 9, 0], 259, 4, [9]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^=^^v^^\n",
491 "8 ([94, 94, 24, 0, 8, 0], 259, 4, [8]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^=^^v^^v\n",
492 "9 ([94, 94, 24, 0, 9, 0], 259, 4, [9]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^=^^v^^v^\n",
493 "8 ([94, 94, 24, 0, 8, 0], 259, 4, [8]) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^=^^v^^v^v\n"
498 "for i in range(50):\n",
499 " print(final(inp[:i]), evaluate(program, inp[:i]), inp[:i])"
504 "execution_count": 17,
512 "(209, ([94, 94, 24, 0, 209, 0], 259, 4, [209]))"
515 "execution_count": 17,
517 "output_type": "execute_result"
521 "final(inp), evaluate(program, inp)"
526 "execution_count": 21,
532 "! bf -n part1.clean.bf < 02-lifts.txt > part1.bf.out"
537 "execution_count": 23,
546 "execution_count": 23,
548 "output_type": "execute_result"
552 "[int(b) for b in open('part1.bf.out', 'rb').read()]"
557 "execution_count": null,
567 "display_name": "Python 3",
568 "language": "python",
576 "file_extension": ".py",
577 "mimetype": "text/x-python",
579 "nbconvert_exporter": "python",
580 "pygments_lexer": "ipython3",