Skip to content

Instantly share code, notes, and snippets.

@ingshtrom
Created February 16, 2018 17:11
Show Gist options
  • Save ingshtrom/be18ecd0e3655b4aadccac98efb23a18 to your computer and use it in GitHub Desktop.
Save ingshtrom/be18ecd0e3655b4aadccac98efb23a18 to your computer and use it in GitHub Desktop.
mastermind
function getMasterSequences(colors, sequenceLength) {
return genSequences(colors, sequenceLength);
}
function genSequences(colors, totalLength, indices = []) {
// console.log('genSequences');
if (indices.length === totalLength) {
const seq = genSeq(colors, indices);
indices = [];
return [seq];
}
let results = [];
for (let i = 0; i < colors.length; i++) {
genSequences(colors, totalLength, indices.concat([i])).forEach(item => results.push(item));
}
// results = results.reduce((prev, next) => prev.concat(next));
return results;
}
function genSeq(colors, indices) {
const result = indices.map(i => colors[i]);
// console.log('genSeq', indices, result);
return result;
}
let result = getMasterSequences(['P', 'O', 'G', 'B'], 1);
console.log('FINAL', result, result.length);
result = getMasterSequences(['P', 'O', 'G', 'B'], 2);
console.log('FINAL', result, result.length);
result = getMasterSequences(['P', 'O', 'G', 'B'], 3);
console.log('FINAL', result, result.length);
result = getMasterSequences(['P', 'O', 'G', 'B'], 4);
console.log('FINAL', result, result.length);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment