def send_query(nums) puts(nums.join(' ')) STDOUT.flush gets.chomp.split.map(&:to_i) end counter = Hash.new(0) 10.times do |n| nums = [n] * 4 a, b = send_query(nums) counter[n] = a if a == 4 exit end end nums = [] counter.each do |v, cnt| cnt.times do nums << v end end nums.permutation(4) do |list| a, b = send_query(list) if a == 4 exit end end