Created
September 2, 2010 11:34
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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