def send_query(nums) $stdout.puts(nums.join(' ')) $stdout.flush a, b = gets.chomp.split.map(&:to_i) [a, b] end counter = Hash.new(0) 10.times do |n| nums = [n] * 4 a, _ = send_query(nums) counter[n] = a if a == 4 exit 0 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) break if a == 4 end