4 program
= '>' + '+' * 61 + '>' + '+' * 33 + '>' + '+' * 24
8 read character into cell 9
13 while cell 9 != 0 # have an input
20 # copy cell 1 to cell 0 using cell 4
25 subtract cell 0 from cell 9
26 [->>>>>>>>>-<<<<<<<<<]
29 while cell 9 != 0 # we're not at an exit
34 copy cell 2 to cell 0 using cell 4
39 subtract cell 0 from cell 9
40 [->>>>>>>>>-<<<<<<<<<]
48 while cell 9 != 0 # we're going down
52 copy cell 7 to cell 11 using cell 12
58 while cell 11 != 0 # above ground
84 have now dealt with the input so clear cell 9
90 while cell 5 != 0 # we're going up
100 ### if 6 == 0 or 7 != 0
103 # copy cell 6 to cell 11 using cell 12
104 <<<<<<[->>>>>+>+<<<<<<]
105 >>>>>>[-<<<<<<+>>>>>>]
123 # copy cell 7 to cell 11 using cell 13
124 <<<<[->>>>+>>+<<<<<<]
125 >>>>>>[-<<<<<<+>>>>>>]
138 # add cell 12 to cell 7
147 have now dealt with the non exit node
154 while cell 10 != 0 # at an exit
157 copy cell 7 to cell 12 using cell 13 (highest)
159 >>>>>>[-<<<<<<+>>>>>>]
161 while cell 12 != 0 (above ground level)
164 copy cell 8 to cell 11 using cell 13 (highest)
170 ### subtract 11 from 12 ensuring 12 gte 0
176 copy cell 12 to cell 14 using cell 15
205 add cell 12 to cell 8
214 read character into cell 9
225 def execute(filename
):
226 f
= open(filename
, "r")
231 def evaluate(code
, inp
=None, debug
=False, execution_limit
=0):
232 code
= cleanup(list(code
))
233 bracemap
= buildbracemap(code
)
235 cells
, codeptr
, cellptr
= [0], 0, 0
241 while codeptr
< len(code
):
242 command
= code
[codeptr
]
245 print(command
, codeptr
, cellptr
, cells
)
249 if cellptr
== len(cells
): cells
.append(0)
252 cellptr
= 0 if cellptr
<= 0 else cellptr
- 1
255 cells
[cellptr
] = cells
[cellptr
] + 1 if cells
[cellptr
] < 255 else 0
258 cells
[cellptr
] = cells
[cellptr
] - 1 if cells
[cellptr
] > 0 else 255
260 if command
== "[" and cells
[cellptr
] == 0: codeptr
= bracemap
[codeptr
]
261 if command
== "]" and cells
[cellptr
] != 0: codeptr
= bracemap
[codeptr
]
263 print(cells
[cellptr
]) # sys.stdout.write(chr(cells[cellptr]))
264 outputs
+= [cells
[cellptr
]]
266 if inputptr
>= len(inp
):
270 cells
[cellptr
] = ord(inp
[inputptr
])
275 if execution_limit
!= 0 and execution_count
> execution_limit
:
279 print(execution_count
)
280 return cells
, codeptr
, cellptr
, outputs
284 return list(filter(lambda x
: x
in '.,[]<>+-', code
))
287 def buildbracemap(code
):
288 temp_bracestack
, bracemap
= [], {}
290 for position
, command
in enumerate(code
):
291 if command
== "[": temp_bracestack
.append(position
)
293 start
= temp_bracestack
.pop()
294 bracemap
[start
] = position
295 bracemap
[position
] = start
300 open('part2.bf', 'w').write(program
+ '\n')
301 inpt
= '^=vvv=^^^=^=vv'
303 inpt
= open('02-lifts.txt').read().strip()
304 # cells, codeptr, cellptr, outputs = evaluate(program, inp=inpt, execution_limit=200000, debug=True)
305 # cells, codeptr, cellptr, outputs = evaluate(program, inp=inpt, debug=True)
306 cells
, codeptr
, cellptr
, outputs
= evaluate(program
, inp
=inpt
)
307 print(cells
, codeptr
, cellptr
, outputs
)
309 if __name__
== "__main__": main()