Created
June 28, 2016 21:02
-
-
Save colingourlay/b56c6a165f5c5b243b7aacfa02e5ffc9 to your computer and use it in GitHub Desktop.
Reduce list of 11 multiple (4) choice question answers to 4 character code, and re-inflate it again.
This file contains 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
const CHARSET = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz$_'; | |
const MASK = parseInt('111100', 2); | |
let guesses = [...Array(11)].map((item, index) => { | |
return Math.floor(Math.random() * 4); | |
// return index % 4; | |
}); | |
console.log(guesses); | |
let remaining = guesses; | |
let triple; | |
let reducedTriple; | |
const reducedTriples = []; | |
while ((triple = remaining.slice(0,3)).length) { | |
reducedTriples.push(triple.reduce((memo, guess, index) => { | |
return memo + (guess << (index * 2)); | |
}, 0)); | |
remaining = remaining.slice(3); | |
} | |
const code = reducedTriples.map((num) => { | |
return CHARSET.charAt(num); | |
}).join(''); | |
console.log(code); | |
const inflatedGuesses = []; | |
code.split('').map((char) => { | |
return CHARSET.indexOf(char); | |
}).forEach((reducedTriple) => { | |
[...Array(3)].map((item, index) => { | |
inflatedGuesses.push((reducedTriple >>> (index * 2)) & ~MASK); | |
}); | |
}); | |
console.log(inflatedGuesses.slice(0, guesses.length)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment