Created
February 16, 2018 17:11
-
-
Save ingshtrom/be18ecd0e3655b4aadccac98efb23a18 to your computer and use it in GitHub Desktop.
mastermind
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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