a = [*0..9].combination(4).to_a def get(d) puts d.join(" ") STDOUT.flush b, c = gets.chomp.split(" ").map &:to_i exit if b == 4 && c == 0 [b, c] end while a.length > 1 do _a = a[0] r = get(_a) r = r[0] + r[1] a.select!{|c| (c & _a).length == r} end a = a[0].permutation(4).each do |_a| get(_a) end