5 "execution_count": 121,
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",
34 " while codeptr < len(code):\n",
35 " command = code[codeptr]\n",
38 " print(command, cellptr, cells)\n",
40 " if command == \">\":\n",
42 " if cellptr == len(cells): cells.append(0)\n",
44 " if command == \"<\":\n",
45 " cellptr = 0 if cellptr <= 0 else cellptr - 1\n",
47 " if command == \"+\":\n",
48 " cells[cellptr] = cells[cellptr] + 1 if cells[cellptr] < 255 else 0\n",
50 " if command == \"-\":\n",
51 " cells[cellptr] = cells[cellptr] - 1 if cells[cellptr] > 0 else 255\n",
53 " if command == \"[\" and cells[cellptr] == 0: codeptr = bracemap[codeptr]\n",
54 " if command == \"]\" and cells[cellptr] != 0: codeptr = bracemap[codeptr]\n",
55 " if command == \".\": sys.stdout.write(chr(cells[cellptr]))\n",
56 " if command == \",\": \n",
57 " if inp is not None:\n",
58 " if inputptr >= len(inp):\n",
60 " cells[cellptr] = ord(inp[inputptr])\n",
63 " cells[cellptr] = ord(getch.getch())\n",
66 " except EOFError:\n",
68 " return cells, codeptr, cellptr\n",
71 "def cleanup(code):\n",
72 " return list(filter(lambda x: x in ['.', ',', '[', ']', '<', '>', '+', '-'], code))\n",
75 "def buildbracemap(code):\n",
76 " temp_bracestack, bracemap = [], {}\n",
78 " for position, command in enumerate(code):\n",
79 " if command == \"[\": temp_bracestack.append(position)\n",
80 " if command == \"]\":\n",
81 " start = temp_bracestack.pop()\n",
82 " bracemap[start] = position\n",
83 " bracemap[position] = start\n",
88 "# if len(sys.argv) == 2: execute(sys.argv[1])\n",
89 "# else: print(\"Usage:\", sys.argv[0], \"filename\")\n",
91 "# if __name__ == \"__main__\": main()\n"
96 "execution_count": 128,
105 "execution_count": 128,
107 "output_type": "execute_result"
111 "ord('v'), ord('^'), ord('=')"
115 "cell_type": "markdown",
119 "set cell 1 to 61\n",
120 "set cell 2 to 94-61=33\n",
121 "set cell 3 to 118-94=24\n",
122 "copy cell 1 into cell 0, using cell 4\n",
124 "set cell 6 to 0 (current level)\n",
125 "set cell 7 to 0 (highest exit)\n",
126 "reserve cell 8 for scratch\n",
128 "read character into cell 9\n",
129 "while cell 9 != 0\n",
130 " subtract cell 0 from cell 9\n",
131 " if cell 9 == 0 we're at an exit\n",
132 " if cell 6 is higher then cell 7\n",
133 " copy cell 6 into cell 7\n",
135 " subtract cell 2 from cell 9\n",
136 " if cell 9 == 0 we're going up\n",
137 " increment cell 6\n",
139 " decrement cell 6\n",
141 " copy cell 1 into cell 0 using cell 4\n",
142 " read character into cell 6\n",
148 "Hold highest exit in unary, as seq 10, 9, 8... 1, 0.\n",
149 "When get a new exit, set level in leftmost cell, rebuild the sequence.\n",
150 "If when you get to the end, there's a non-zero cell to the right, it wasn't longest.\n",
151 " move to the right until you reach zero, then rebuild the longest sequence right-to-left\n",
158 "execution_count": 3,
167 "execution_count": 3,
169 "output_type": "execute_result"
178 "execution_count": 7,
184 "helloworld= '++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.'"
189 "execution_count": 20,
194 "output_type": "stream",
202 "([0, 0, 72, 100, 87, 33, 10], 106, 6)"
205 "execution_count": 20,
207 "output_type": "execute_result"
211 "evaluate(helloworld)"
216 "execution_count": 19,
221 "output_type": "stream",
232 "execution_count": 19,
234 "output_type": "execute_result"
238 "evaluate(',[.,]', input='hello')"
243 "execution_count": 196,
249 "program = '>' + '+' * 94 + '>' + '+' * 24\n",
250 "program += \"\"\"\n",
251 "copy cell 1 into cell 0 using cell 3\n",
259 "read into cell 5\n",
263 " subtract cell 0 from cell 5\n",
264 " <<<<<[->>>>>-<<<<<]\n",
267 " if cell 5 != 0 do more\n",
270 " copy cell 2 into cell 0 using cell 3\n",
273 " move cell 3 into cell 2\n",
277 " subtract cell 0 from cell 5\n",
278 " <<[->>>>>-<<<<<]\n",
283 " increment cell 4 by 1\n",
286 " clear cell 5 to stop the loop\n",
289 " decrement cell 4 by 2\n",
294 " increment cell 4 by 1\n",
297 " copy cell 1 into cell 0 using cell 3\n",
300 " move cell 3 into cell 1\n",
305 " read next input\n",
308 "write the output\n",
316 "execution_count": 197,
322 "([94, 94, 24, 0, 3, 0], 255, 5)"
325 "execution_count": 197,
327 "output_type": "execute_result"
331 "evaluate(program, inp='^^v=^^^v')"
336 "execution_count": 198,
342 "'>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++<[-<+>>>+<<]>>[-<<+>>]>>,[<<<<<[->>>>>-<<<<<]>>>>>[<<<[->+<<<+>>]>[-<+>]<<<[->>>>>-<<<<<]>>>>>[<+>[-]]<-->]<+<<<[-<+>>>+<<]>>[-<<+>>]>>,]<.'"
345 "execution_count": 198,
347 "output_type": "execute_result"
351 "''.join(cleanup(program))"
356 "execution_count": 123,
362 "([60, 60, 0, 15, 0], 152, 4)"
365 "execution_count": 123,
367 "output_type": "execute_result"
371 "inp = open('02-lifts.txt').read().strip()\n",
372 "evaluate(program, inp=inp)"
377 "execution_count": 124,
383 "def value(instr):\n",
384 " if instr == '^':\n",
386 " elif instr == 'v':\n",
394 "execution_count": 125,
400 "def final(sequence):\n",
402 " for c in sequence:\n",
403 " current += value(c)\n",
409 "execution_count": 126,
418 "execution_count": 126,
420 "output_type": "execute_result"
429 "execution_count": 193,
436 "output_type": "stream",
438 "0 ([94, 94, 24, 0, 0, 0], 144, 5) \n",
439 "-1 ([94, 94, 24, 0, 255, 0], 255, 5) v\n",
440 "-2 ([94, 94, 24, 0, 254, 0], 255, 5) vv\n",
441 "-3 ([94, 94, 24, 0, 253, 0], 255, 5) vvv\n",
442 "-2 ([94, 94, 24, 0, 254, 0], 255, 5) vvv^\n",
443 "-1 ([94, 94, 24, 0, 255, 0], 255, 5) vvv^^\n",
444 "-2 ([94, 94, 24, 0, 254, 0], 255, 5) vvv^^v\n",
445 "-1 ([94, 94, 24, 0, 255, 0], 255, 5) vvv^^v^\n",
446 "-2 ([94, 94, 24, 0, 254, 0], 255, 5) vvv^^v^v\n",
447 "-2 ([94, 94, 24, 0, 254, 0], 255, 5) vvv^^v^v=\n",
448 "-3 ([94, 94, 24, 0, 253, 0], 255, 5) vvv^^v^v=v\n",
449 "-4 ([94, 94, 24, 0, 252, 0], 255, 5) vvv^^v^v=vv\n",
450 "-3 ([94, 94, 24, 0, 253, 0], 255, 5) vvv^^v^v=vv^\n",
451 "-4 ([94, 94, 24, 0, 252, 0], 255, 5) vvv^^v^v=vv^v\n",
452 "-3 ([94, 94, 24, 0, 253, 0], 255, 5) vvv^^v^v=vv^v^\n",
453 "-2 ([94, 94, 24, 0, 254, 0], 255, 5) vvv^^v^v=vv^v^^\n",
454 "-3 ([94, 94, 24, 0, 253, 0], 255, 5) vvv^^v^v=vv^v^^v\n",
455 "-4 ([94, 94, 24, 0, 252, 0], 255, 5) vvv^^v^v=vv^v^^vv\n",
456 "-5 ([94, 94, 24, 0, 251, 0], 255, 5) vvv^^v^v=vv^v^^vvv\n",
457 "-6 ([94, 94, 24, 0, 250, 0], 255, 5) vvv^^v^v=vv^v^^vvvv\n",
458 "-5 ([94, 94, 24, 0, 251, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^\n",
459 "-4 ([94, 94, 24, 0, 252, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^\n",
460 "-5 ([94, 94, 24, 0, 251, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v\n",
461 "-5 ([94, 94, 24, 0, 251, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=\n",
462 "-4 ([94, 94, 24, 0, 252, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^\n",
463 "-4 ([94, 94, 24, 0, 252, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=\n",
464 "-3 ([94, 94, 24, 0, 253, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^\n",
465 "-2 ([94, 94, 24, 0, 254, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^\n",
466 "-2 ([94, 94, 24, 0, 254, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=\n",
467 "-1 ([94, 94, 24, 0, 255, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^\n",
468 "0 ([94, 94, 24, 0, 0, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^\n",
469 "1 ([94, 94, 24, 0, 1, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^\n",
470 "1 ([94, 94, 24, 0, 1, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=\n",
471 "2 ([94, 94, 24, 0, 2, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^\n",
472 "3 ([94, 94, 24, 0, 3, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^\n",
473 "4 ([94, 94, 24, 0, 4, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^\n",
474 "3 ([94, 94, 24, 0, 3, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v\n",
475 "3 ([94, 94, 24, 0, 3, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=\n",
476 "4 ([94, 94, 24, 0, 4, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^\n",
477 "5 ([94, 94, 24, 0, 5, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^\n",
478 "6 ([94, 94, 24, 0, 6, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^\n",
479 "6 ([94, 94, 24, 0, 6, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^=\n",
480 "7 ([94, 94, 24, 0, 7, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^=^\n",
481 "8 ([94, 94, 24, 0, 8, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^=^^\n",
482 "7 ([94, 94, 24, 0, 7, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^=^^v\n",
483 "8 ([94, 94, 24, 0, 8, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^=^^v^\n",
484 "9 ([94, 94, 24, 0, 9, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^=^^v^^\n",
485 "8 ([94, 94, 24, 0, 8, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^=^^v^^v\n",
486 "9 ([94, 94, 24, 0, 9, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^=^^v^^v^\n",
487 "8 ([94, 94, 24, 0, 8, 0], 255, 5) vvv^^v^v=vv^v^^vvvv^^v=^=^^=^^^=^^^v=^^^=^^v^^v^v\n"
492 "for i in range(50):\n",
493 " print(final(inp[:i]), evaluate(program, inp[:i]), inp[:i])"
498 "execution_count": 194,
506 "(209, ([94, 94, 24, 0, 209, 0], 255, 5))"
509 "execution_count": 194,
511 "output_type": "execute_result"
515 "final(inp), evaluate(program, inp)"
520 "execution_count": 199,
526 "! bf -n part1.bf < 02-lifts.txt > part1.bf.out"
531 "execution_count": null,
541 "display_name": "Python 3",
542 "language": "python",
550 "file_extension": ".py",
551 "mimetype": "text/x-python",
553 "nbconvert_exporter": "python",
554 "pygments_lexer": "ipython3",