Skip to content

Instantly share code, notes, and snippets.

@QuantumFractal
Created December 17, 2015 07:26
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 QuantumFractal/932d197bbcc30821a39e to your computer and use it in GitHub Desktop.
Save QuantumFractal/932d197bbcc30821a39e to your computer and use it in GitHub Desktop.
import itertools as i
import sys
def main():
with open("test.pr", 'r') as f:
n = int(f.read(1))
crystal = {"".join(s) : 0 for s in i.product("01", repeat=n)}
cursor = "0"*n
jump = 1
while True:
c = f.read(1)
if c == ">" or c == "<":
shift = 0
p = f.tell()
while f.read(1) == c: shift +=1
f.seek(p+shift)
b = "1" if c == ">" else "0"
cursor = cursor[:shift]+b+cursor[shift+1:]
elif c == "|":
shift = 0
p = f.tell()
while f.read(1) == c: shift +=1
f.seek(p+shift)
b = cursor[shift]
b = "1" if b == "0" else "0"
cursor = cursor[:shift]+b+cursor[shift+1:]
elif c == "[":
jump = f.tell()
if crystal[cursor] == 0:
while f.read(1) != "]": pass
print("[! jump:",jump)
elif c == "]":
if crystal[cursor] != 0:
f.seek(jump-1)
print("]! jump:",jump)
elif c == "+":
crystal[cursor] += 1
elif c == "-":
crystal[cursor] -= 1
elif c == ",":
crystal[cursor] = ord(input('')[0])
elif c == ".":
print(chr(crystal[cursor]))
if not c:
break
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment