Skip to content

Instantly share code, notes, and snippets.

@geoffb
Created February 24, 2017 03:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geoffb/4bb940c9d0fc430c93a8d6389a367ac4 to your computer and use it in GitHub Desktop.
Save geoffb/4bb940c9d0fc430c93a8d6389a367ac4 to your computer and use it in GitHub Desktop.
Calculate permutations
var permut = function (array, count, initial, output) {
if (initial.length >= count) {
output.push(initial);
} else {
for (var i = 0; i < array.length; ++i) {
permut(array, count, initial.concat(array[i]), output);
}
}
};
var getPermutations = function (array, count) {
var output = [];
permut(array, count, [], output);
return output;
};
var PART_VALUES = [0, 1, 2, 3, 4]; // 0 = Rabbit, 1 = Deer, etc
var PART_COUNT = 5; // Number of "slots", head, eyes, feet, etc
var perms = getPermutations(PART_VALUES, PART_COUNT);
console.log(perms.length + " permutations");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment