Skip to content

Instantly share code, notes, and snippets.

@wasabili
Created September 2, 2010 11:34
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 wasabili/562178 to your computer and use it in GitHub Desktop.
Save wasabili/562178 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# coding: utf-8
import sys
def preprocessor(orders):
import re
x = re.compile(r"(うん|たん)").findall(orders)
l = "".join(x)
orders = ''
while len(l) >= 6:
t = l[:6]
if t == 'うんうんうん':
orders += '>'
elif t == 'うんうんたん':
orders += '<'
elif t == 'うんたんうん':
orders += '+'
elif t == 'うんたんたん':
orders += '-'
elif t == 'たんうんうん':
orders += '.'
elif t == 'たんうんたん':
orders += ','
elif t == 'たんたんうん':
orders += '['
else:
orders += ']'
l = l[6:]
return orders
MAXSIZE = 10000
orders = ""
data = [0 for x in range(MAXSIZE)]
pointer = 0
filename = sys.argv[1]
with open(filename) as f:
orders = f.read()
orders = preprocessor(orders)
size = len(orders)
# Extract braces positions
braces = {}
rec = []
for i in range(size):
if orders[i] == '[':
rec += [i]
elif orders[i] == ']':
begin, end = rec.pop(), i
braces[begin] = end
braces[end] = begin
seeker = 0
while seeker < size:
c = orders[seeker]
if c == '>': pointer += 1
elif c == '<': pointer -= 1
elif c == '+': data[pointer] += 1
elif c == '-': data[pointer] -= 1
elif c == '.': sys.stdout.write(chr(data[pointer]))
elif c == ',': data[pointer] = ord(input())
elif c == '[' and data[pointer] == 0: seeker = braces[seeker]
elif c == ']' and data[pointer] != 0: seeker = braces[seeker]
assert 0 <= pointer < MAXSIZE, 'Out of memory index'
seeker += 1
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment