Skip to content

Instantly share code, notes, and snippets.

@colingourlay
Created June 28, 2016 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colingourlay/b56c6a165f5c5b243b7aacfa02e5ffc9 to your computer and use it in GitHub Desktop.
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.
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