Last active
August 29, 2015 14:04
-
-
Save math314/4a71e5a429ffc4ba7715 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#coding=utf-8 | |
import os | |
import socket | |
import select | |
from time import sleep | |
import binascii | |
from subprocess import Popen,STDOUT,PIPE | |
import os | |
import math | |
import itertools | |
import string | |
def next_line(stdout): | |
line = "" | |
while True: | |
r = stdout.read(1) | |
if r == '\n': | |
break | |
if r == "?": | |
stdout.read(1) | |
return r | |
line += r | |
print "<read> : " + line | |
line = line.strip('\x00') | |
return line | |
def write(stdin,val): | |
stdin.write(val) | |
print "<write> : " + val | |
cmdline = "./amida" | |
p = Popen(cmdline, shell=True, cwd="./", stdin=PIPE, | |
stdout=PIPE, stderr=STDOUT,close_fds=True) | |
def nl(): return next_line(p.stdout) | |
def wr(val): write(p.stdin,val) | |
# start | |
print "-" * 80 | |
def solve(amida,idxs,ast): | |
use_idx = [i for i,j in enumerate(amida[0]) if j == '|'] | |
for line in amida: | |
for i in xrange(len(idxs) - 1): | |
see_idx = (use_idx[i] + use_idx[i+1]) / 2 | |
if line[see_idx] == '-': | |
idxs[i],idxs[i+1] = idxs[i+1],idxs[i] | |
return idxs[use_idx.index(ast.find('*'))] | |
def read_stage(): | |
while True: | |
No = int(nl().split(".")[-1]) | |
def nl_gen(): | |
while True: yield nl() | |
ret = list(itertools.takewhile(lambda line: line != '?',nl_gen())) | |
return ret | |
def rotate(stage): | |
ret = map(lambda l: "".join(l).translate(string.maketrans('|-','-|')) \ | |
,zip(*stage)) | |
return list(ret) | |
while True: | |
stage = read_stage() | |
if "|" not in stage[0] and "-" in stage[0]: | |
stage = rotate(stage) | |
first = stage[0] | |
last = stage[-1] | |
amida = stage[1:-1] | |
ast = "" | |
idxs = [] | |
if "*" in last: | |
ast = last | |
idxs = map(int,first.split()) | |
else: | |
ast = first | |
idxs = map(int,last.split()) | |
amida = amida[::-1] | |
wr(str(solve(amida,idxs,ast)) + "\n") | |
#end | |
print "-" * 80 | |
ret = p.wait() # return code | |
print "Return code: %d" % ret | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment