def ternary_search_tree(left, right) loop_cnt = 25 loop_cnt.times do l = (left * 2 + right / 3.0).floor r = ((left + right * 2) / 3.0).ceil if yield(l) < yield(r) right = r else left = l end end l end cx = 0 cy = 0 cx = ternary_search_tree(0, 100_000) { |x| STDOUT.puts("%d %d" % [x, cy]) STDOUT.flush d = gets.to_i if d == 0 exit end d } cy = ternary_search_tree(0, 100_000) { |y| STDOUT.puts("%d %d" % [cx, y]) STDOUT.flush d = gets.to_i if d == 0 exit end d }