Created
December 25, 2020 21:52
-
-
Save warriordog/8652c2432abc0c11120526ff692c7f58 to your computer and use it in GitHub Desktop.
Solution to Advent of Code 2020 Day 25
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 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