def send_query(nums) raise if nums.uniq.size != 4 STDOUT.puts(nums.join(' ')) STDOUT.flush a, b = gets.chomp.split.map(&:to_i) if a == 4 exit end [a, b] end nums = [*0..9] [*0..9].combination(2) do |blo| list = [*0..9] - blo c = 0 a, b = send_query(list[0..3]) c += a + b a, b = send_query(list[4..7]) c += a + b if c == 4 nums -= blo end end raise if nums.uniq.size != 4 nums.permutation(4) do |list| a, b = send_query(list) break if a == 4 end