"use strict";

var assert = require('assert');

var calculateCows = function(guess, secret) {
    var n = guess.length;
    var cows = 0;

    for (var i=0;i<n;++i) {
	var idx = secret.indexOf(guess[i]);
	if (idx !== -1 && idx !== i) 
	    cows++;
    }

    return cows;
};

var calculateBulls = function(guess, secret) {
    var n = guess.length;
    var bulls = 0;

    for (var i=0;i<n;++i)
	if (guess[i] === secret[i])
	    bulls++;

    return bulls;
};

var calculateScore = function(guess, secret) {
    return {
	cows: calculateCows(guess,secret),
	bulls: calculateBulls(guess,secret)
    };
};

var calculateInitialGuesses = function() {
    var digits = ['0','1','2','3','4','5','6','7','8','9'];

    var permutations = function(n) {
	if (n === 1) return digits;

	var rest = permutations(n - 1);
	var options = [];
	for (var i=0;i<rest.length;++i) {
	    for (var j=0;j<digits.length;++j) {
		if (rest[i].indexOf(digits[j]) === -1) {
		    options.push(digits[j] + rest[i]);
		}
	    }
	}

	return options;
    };

    return permutations(4);
};

var eliminateGuesses = function(guesses, guess, score) {
    return guesses;
};

describe('cows and bulls', function() {

    describe('eliminations', function() {
	it('should eliminate all not matching', function() {
	    var guesses = calculateInitialGuesses();

	    var rest = eliminateGuesses(guesses, '1234', {bulls: 0, cows: 0});

	    assert.equal(6*5*4*3, rest.length);
	});
    });

    describe('permutations', function() {
	it('should have 10*9*8*7 initial guesses', function() {
	    var setOfGuesses = calculateInitialGuesses();

	    assert.equal(10*9*8*7, setOfGuesses.length);
	});
    });

    describe('scoring', function() {
	it('scores nothing when no matches', function() {
	    var score = calculateScore('1234', '5678');

	    assert.equal(0, score.cows);
	    assert.equal(0, score.bulls);
	});

	it('scores bulls when all matches', function() {
	    var score = calculateScore('1234', '1234');
	    
	    assert.equal(0, score.cows);
	    assert.equal(4, score.bulls);
	});

	it('scores cows when permutation', function() {
	    var score = calculateScore('1234', '4321');
	    
	    assert.equal(4, score.cows);
	    assert.equal(0, score.bulls);
	});

	it('scores partially', function() {
	    var score = calculateScore('1234', '1243');

	    assert.equal(2, score.cows);
	    assert.equal(2, score.bulls);
	});
    });
});

@@ -1061,79 +1061,355 @@
 var 
-eliminateGuesses = function(guesses, guess, score) %7B%0A    return guesses
+scoreEquals = function(a,b) %7B%0A    return a.cows === b.cows && a.bulls === b.bulls;%0A%7D;%0A%0Avar eliminateGuesses = function(guesses, guess, score) %7B%0A    var rest = %5B%5D;%0A    var n = guesses.length;%0A%0A    for (var i=0;i%3Cn;++i) %7B%0A%09var sc = calculateScore(guesses%5Bi%5D,guess);%0A%09if (scoreEquals(sc,score))%0A%09    rest.push(guesses%5Bi%5D);%0A    %7D%0A    %0A%0A    return rest
 ;%0A%7D;

make eliminations work in the way I think they should.