Skip to content

Instantly share code, notes, and snippets.

@warriordog
Created December 25, 2020 21:52
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 warriordog/8652c2432abc0c11120526ff692c7f58 to your computer and use it in GitHub Desktop.
Save warriordog/8652c2432abc0c11120526ff692c7f58 to your computer and use it in GitHub Desktop.
Solution to Advent of Code 2020 Day 25
const publicKeys = [ 12232269, 19452773 ]; // PROD input
//const publicKeys = [ 5764801, 17807724 ]; // TEST input 1
function runLoop(subject, loopSize) {
let value = 1;
for (let i = 0; i < loopSize; i++) {
value *= subject;
value %= 20201227;
}
return value;
}
function findLoopSize() {
let value = 1;
let loopSize = 0;
while (!publicKeys.includes(value)) {
loopSize++;
value *= 7;
value %= 20201227;
}
return [ loopSize, value ];
}
function findEncryptionKey() {
const [ loopSize, value ] = findLoopSize();
const otherPubKey = publicKeys.find(pubKey => pubKey !== value);
return runLoop(otherPubKey, loopSize);
}
console.log(`The encryption key is ${ findEncryptionKey() }`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment