size = 4
scores = []
guesses = []
puts 
possible_guesses = [*'1'..'9'].permutation(size).to_a.shuffle

loop do
  guesses << current_guess = possible_guesses.pop
  print 
  scores << score = gets.split(',').map(&:to_i)

  
  break (puts ) if score == [size,0]

  
  possible_guesses.select! do |guess|
    bulls = guess.zip(current_guess).count{|g,cg| g == cg}
    cows = size - (guess - current_guess).size - bulls
    [bulls, cows] == score
  end

  
  if possible_guesses.empty?
    puts 
    guesses.zip(scores).each{|g, (b, c)| puts }
    break
  end
end