Skip to content

Instantly share code, notes, and snippets.

@wasabili
Created September 2, 2010 11:34

Revisions

  1. @invalid-email-address Anonymous created this gist Sep 2, 2010.
    77 changes: 77 additions & 0 deletions gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,77 @@
    #!/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()