def send_query(nums) STDOUT.puts(nums.join(' ')) STDOUT.flush a, b = gets.chomp.split.map(&:to_i) if a == 4 exit 0 end [a, b] end excepted_nums = [] nums = [*0..9] nums.combination(4).to_a.shuffle.each do |list| a, b = send_query(list) if a + b == 0 excepted_nums = list.dup break end end cand = [] 10.times do |i| next if excepted_nums.include?(i) excepted_nums[3] = i a, b = send_query(excepted_nums) if a + b > 0 cand << i end end cand.permutation(4).each do |n| send_query(n) end