Skip to content

Instantly share code, notes, and snippets.

@davidwallacejackson
Created June 1, 2015 16:29
Show Gist options
  • Save davidwallacejackson/07c8dbce9f41e1bc20cb to your computer and use it in GitHub Desktop.
Save davidwallacejackson/07c8dbce9f41e1bc20cb to your computer and use it in GitHub Desktop.
program = "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++."
program_index = 0
data = []
for i in range(0, 30000):
data.append(0)
data_index = 0
while program_index < len(program):
command = program[program_index]
if command == '>':
data_index += 1
elif command == '<':
data_index -= 1
elif command == '+':
data[data_index] += 1
elif command == '-':
data[data_index] -= 1
elif command == '.':
print(chr(data[data_index]))
elif command == ',':
user_input = ord(raw_input()[0])
data[data_index] = user_input
elif command == '[':
if data[data_index] == 0:
nesting_counter = 0
while nesting_counter > 0 or program[program_index] != ']':
if program[program_index] == '[':
nesting_counter += 1
elif program[program_index] == ']':
nesting_counter -= 1
program_index += 1
elif command == ']':
if data[data_index] != 0:
nesting_counter = 0
program_index -= 1
while nesting_counter > 0 or program[program_index] != '[':
if program[program_index] == ']':
nesting_counter += 1
elif program[program_index] == '[':
nesting_counter -= 1
program_index -= 1
program_index += 1
print('Program complete.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment