Skip to content

Instantly share code, notes, and snippets.

@yutopp

yutopp/aaaa.rb Secret

Created July 19, 2014 12:18
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 yutopp/6b8ffa473fb69d9f3739 to your computer and use it in GitHub Desktop.
Save yutopp/6b8ffa473fb69d9f3739 to your computer and use it in GitHub Desktop.
amida
require "popen4"
def make_amida_index(ll)
cton = {} # column to number
ntoc = {} # number to column
c = 0
i = 1
loop do
break if c == ll.length
if ll[c] == "|"
cton[c] = i
ntoc[i] = c
i += 1
end
c += 1
end
return cton, ntoc
end
def solve(arr, fl)
cton, ntoc = make_amida_index(arr[2])
p cton, ntoc
start = cton[arr[arr.length-1].index("*")]
puts "start is => #{start}"
arr.reverse[1...arr.length].map{|l| " #{l} "}.each do |a|
l = a[ntoc[start]+1-1]
r = a[ntoc[start]+1+1]
start -= 1 if l == "-"
start += 1 if r == "-"
puts "to => #{start}"
end
rel = {}
fl.split(" ").each_with_index do |f, i|
rel[i+1] = f.to_i
end
return rel[start]
end
def transform(arr)
lines = []
arr[0].length.times do
lines << ""
end
arr.each do |a|
a.length.times do |l|
c = a[l]
c = "-" if a[l] == "|"
c = "|" if a[l] == "-"
lines[l] << c
end
end
return lines
end
anss = []
POpen4::popen4("./amida") {|stdout, stderr, stdin, pid|
no = 1
skip_no = false
loop do
amida = []
fl = ""
normal_flag = true
puts "= amida = (normal #{normal_flag})"
lines = []
unless skip_no
l = stdout.readline # No.
p "No => #{l}"
skip_no = false
end
# get first line
f = stdout.readline.chomp.gsub(/[^1234567890 No.|*-]/, "")
p "first = #{f}"
#
if f =~ /-/
# vert
puts "!!!!tate!!!!"
tmp = []
tmp << f
loop do
begin
timeout(0.02) do
l = stdout.readline.chomp.gsub(/[^1234567890 No.|*-]/, "")
tmp << l
end
rescue Timeout::Error
break
end
end
puts tmp
lines = transform(tmp)
else
# hol
puts "!!!!yoko!!!!"
lines << f
20.times do
l = stdout.readline.chomp.gsub(/[^1234567890 No.|*-]/, "")
lines << l
end
end
lines.each_with_index do |l, i|
if i == 0
normal_flag = false if l[0] == " " || l[0] == "*"
end
if normal_flag
fl = l if i == 0
else
fl = l if i == 20
end
amida << l
p l
end
puts "index is => #{fl}"
ans = nil
if normal_flag
ans = solve(amida, fl)
else
ans = solve(amida.reverse, fl)
end
anss << ans
puts "answer is #{ans}"
stdin.puts ans.to_i
no += 1
end
}
p anss
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment