Skip to content

Instantly share code, notes, and snippets.

@math314
Created July 19, 2014 12:15
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 math314/fab1445b9d91d88abca8 to your computer and use it in GitHub Desktop.
Save math314/fab1445b9d91d88abca8 to your computer and use it in GitHub Desktop.
#coding=utf-8
import os
import socket
import select
from time import sleep
import binascii
from subprocess import Popen,STDOUT,PIPE # Python 2.4以上が必要
import os
from math import *
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
cwd = "./" # 作業ディレクトリ
cmdline = "./amida" # スペースやタブで区切る
p = Popen(cmdline, shell=True, cwd=cwd, stdin=PIPE,
stdout=PIPE, stderr=STDOUT,close_fds=True)
print "-" * 80 # 区切り表示(開始)
def nl(): return next_line(p.stdout)
def wr(val): write(p.stdin,val)
def solve(amida,idxs,ast):
use_idx = []
for i in xrange(len(amida[0])):
if amida[0][i] == "|":
use_idx.append(i)
print use_idx
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] == '-':
tmp = idxs[i]
idxs[i] = idxs[i+1]
idxs[i+1] = tmp
ans_idx = ast.find('*')
if ans_idx == -1:
return -1
for i in xrange(len(use_idx)):
if ans_idx == use_idx[i]:
ans_idx = i
return idxs[ans_idx]
def read_stage():
while True:
No = int(nl().split(".")[-1])
ret =[]
while True:
line = nl()
if line == '?':
break
ret.append(line)
return (No,ret)
def rotate(stage):
n = len(stage)
m = len(stage[0])
ret = [[0] * n for _ in xrange(m)]
for i in xrange(n):
for j in xrange(m):
ret[j][i] = stage[i][j]
for i in xrange(m):
for j in xrange(n):
if ret[i][j] == '-':
ret[i][j] = '|'
elif ret[i][j] == '|':
ret[i][j] = '-'
ret[i] = "".join(ret[i])
return ret
while True:
No,stage = read_stage()
first = stage[0]
last = stage[-1]
amida = stage[1:-1]
ast = ""
idxs = []
try:
if "*" in last:
ast = last
idxs = map(int,first.split())
else:
ast = first
idxs = map(int,last.split())
amida = amida[::-1]
except:
stage = rotate(stage)
first = stage[0]
last = stage[-1]
amida = stage[1:-1]
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")
print "-" * 80 # 区切り表示(終了)
ret = p.wait() # 戻り値が入る
print "Return code: %d" % ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment