#!/usr/bin/ruby

#
## http://rosettacode.org/wiki/Bulls_and_cows#Sidef
#

var size = 4;
var num = (@(1..9) -> shuffle.first(size));

for (var guesses = 1; true; guesses++) {

    var bulls = 0;
    var cows  = 0;

    var input = Sys.scanln("Input: ").split(1)            \
                                     .unique              \
                                     .grep {.~~/^[1-9]$/} \
                                     .map  {.to_i};

    input.len == size || (
        warn "Invalid input!\n"
        guesses--;
        next;
    );

    if (input == num) {
        printf("You did it in %d attempts!\n", guesses);
        break;
    }

    num.range.each { |i|
        if (num[i] == input[i]) {
            bulls++;
        }
        elsif (num.contains(input[i])) {
            cows++;
        }
    }

    "Bulls: %d; Cows: %d\n".printf(bulls, cows);
}
