Skip to content

Instantly share code, notes, and snippets.

@akiym
Last active August 29, 2015 14:04
Show Gist options
  • Save akiym/335ae9083687d2169caf to your computer and use it in GitHub Desktop.
Save akiym/335ae9083687d2169caf to your computer and use it in GitHub Desktop.
あみだくじ (akiym, xrekkusu)
# -*- coding: utf-8 -*-
import os
import sys
import time
import re
from pwn import *
s = process('amida')
# thanks~~ http://arc006.contest.atcoder.jp/submissions/140243
def amida(N, L, res, state):
i = 0
for _ in range(L) :
row = res[i].replace('|', '')
for j in range(N-1) :
if row[j] == '-' :
state[j], state[j+1] = state[j+1], state[j]
i += 1
return state[res[i].find('*')/2]
def inverse(lines):
reform = []
for j in range(len(lines[0])):
reform.append('')
for i in range(len(lines)):
c = lines[i][j]
if (c == '-'):
reform[j] += '|'
elif (c == '|'):
reform[j] += '-'
else:
reform[j] += c
return reform
j = 1
while True:
print '=====> No. ' + str(j)
data = s.recv()
print data
data = re.sub(chr(0), '', data)
lines = data.split('\n')
lines = lines[1:-1] # "No. "の行と"?"の行はいらない
# 横なら
if lines[0].count('-'):
lines = inverse(lines)
# 逆なら
if lines[0].count('*'):
lines.reverse()
numbers = lines[0]
state = []
for e in numbers.split():
if '0' <= e and e <= '9':
state.append(int(e))
else:
state.append(e)
print 'state: ' + str(state)
goal_pos = -1
res = []
for l in lines[1:-1]: # 開始の数字と終了の"*"を飛ばす
# 横長の場合
l = re.sub(r'-+', '-', l)
l = re.sub(r'[\x20]+', ' ', l)
res.append(l)
# ゴールの数字
goal_n = int(lines[0][lines[-1].find('*')])
print 'goal number: ' + str(goal_n)
# ゴールをresに追加
if state[0] == 8:
res.append((' ' * (8 - goal_n)) + '*' + (' ' * 30))
else:
res.append((' ' * (goal_n - 1)) + '*' + (' ' * 30))
print 'res: '
for r in res:
print r
goal = str(amida(8, len(res) - 1, res, state))
print 'goal: ' + str(goal)
print '----------'
s.send(str(goal) + '\n')
j += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment