"cells": [
{
"cell_type": "code",
- "execution_count": 121,
- "metadata": {},
+ "execution_count": 2,
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"#!/usr/bin/python\n",
"\n",
" cells, codeptr, cellptr = [0], 0, 0\n",
" inputptr = 0\n",
+ " output = []\n",
"\n",
" try:\n",
" while codeptr < len(code):\n",
"\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",
},
{
"cell_type": "code",
- "execution_count": 128,
+ "execution_count": 3,
"metadata": {},
"outputs": [
{
"(118, 94, 61)"
]
},
- "execution_count": 128,
+ "execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
},
{
"cell_type": "code",
- "execution_count": 129,
+ "execution_count": 4,
"metadata": {},
"outputs": [
{
"24"
]
},
- "execution_count": 129,
+ "execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
},
{
"cell_type": "code",
- "execution_count": 7,
+ "execution_count": 5,
"metadata": {
"collapsed": true
},
},
{
"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"
}
},
{
"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",
},
{
"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"
}
},
{
"cell_type": "code",
- "execution_count": 198,
+ "execution_count": 10,
"metadata": {},
"outputs": [
{
"'>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++<[-<+>>>+<<]>>[-<<+>>]>>,[<<<<<[->>>>>-<<<<<]>>>>>[<<<[->+<<<+>>]>[-<+>]<<<[->>>>>-<<<<<]>>>>>[<+>[-]]<-->]<+<<<[-<+>>>+<<]>>[-<<+>>]>>,]<.'"
]
},
- "execution_count": 198,
+ "execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
},
{
"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"
}
},
{
"cell_type": "code",
- "execution_count": 124,
+ "execution_count": 13,
"metadata": {
"collapsed": true
},
},
{
"cell_type": "code",
- "execution_count": 125,
+ "execution_count": 14,
"metadata": {
"collapsed": true
},
},
{
"cell_type": "code",
- "execution_count": 126,
+ "execution_count": 15,
"metadata": {},
"outputs": [
{
"209"
]
},
- "execution_count": 126,
+ "execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
},
{
"cell_type": "code",
- "execution_count": 193,
+ "execution_count": 16,
"metadata": {
"scrolled": true
},
"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"
]
}
],
},
{
"cell_type": "code",
- "execution_count": 194,
+ "execution_count": 17,
"metadata": {
"scrolled": true
},
{
"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"
}
},
{
"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()]"
]
},
{
"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
},
"\n",
" cells, codeptr, cellptr = [0], 0, 0\n",
" inputptr = 0\n",
+ " outputs = []\n",
"\n",
" try:\n",
" while codeptr < len(code):\n",
"\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",
},
{
"cell_type": "code",
- "execution_count": 128,
+ "execution_count": 2,
"metadata": {},
"outputs": [
{
"(118, 94, 61)"
]
},
- "execution_count": 128,
+ "execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
" 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",
" 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"
]
},
"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": {
"118"
]
},
- "execution_count": 3,
+ "execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
},
{
"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')"
]
},
{
},
{
"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()]"
]
},
{