Skip to content

Instantly share code, notes, and snippets.

@mdamien
Created October 24, 2013 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdamien/7144876 to your computer and use it in GitHub Desktop.
Save mdamien/7144876 to your computer and use it in GitHub Desktop.
import sys
def execute(code):
code = ''.join([x for x in code if x in '.,+-<>[]'])
cells,ptr,i = [0]*3000,0,0
while i < len(code):
c = code[i]
if c == '>': ptr += 1
if c == '<': ptr -= 1
if c == '+': cells[ptr] += 1
if c == '-': cells[ptr] -= 1
if c == '.': sys.stdout.write(chr(cells[ptr]))
if c == ',': cells[ptr] = ord(sys.stdin.read(1))
if c == '[' and cells[ptr] == 0: i = code.find("]",i)
if c == ']': i = code[:i].find('[')-1
i += 1
if __name__ == '__main__':
hello_world = "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>."
execute(hello_world)
@bryant
Copy link

bryant commented Jun 5, 2014

bug on lines 14 and 15. think about nested loops.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment