Skip to content

Instantly share code, notes, and snippets.

@felix-larsen
Created December 25, 2020 08:50
Show Gist options
  • Save felix-larsen/895f5c7073c5e09bc9214baa3ba21ec6 to your computer and use it in GitHub Desktop.
Save felix-larsen/895f5c7073c5e09bc9214baa3ba21ec6 to your computer and use it in GitHub Desktop.
25th December solution - Advent of code - swift
let cardsPublicKey = 9033205
let doorsPublicKey = 9281649
let subjectNumber = 7
let cardLoopSize = findLoopSizeFor(publicKey: cardsPublicKey, subjectNumber: subjectNumber)
let doorLoopSize = findLoopSizeFor(publicKey: doorsPublicKey, subjectNumber: subjectNumber)
print(cardLoopSize, doorLoopSize)
let encryptionKey = transformWithLoopSize(subjectNumber: cardsPublicKey, loopSize: doorLoopSize)
print(encryptionKey)
func findLoopSizeFor(publicKey: Int, subjectNumber: Int) -> Int {
var loopSize = 0
var value = 1
while value != publicKey {
value = transform(value: value, subjectNumber: subjectNumber)
loopSize += 1
}
return loopSize
}
func transform(value: Int, subjectNumber: Int) -> Int {
var returnValue = value
returnValue *= subjectNumber
returnValue %= 20201227
return returnValue
}
func transformWithLoopSize(subjectNumber: Int, loopSize: Int) -> Int {
var value = 1
for _ in 0 ..< loopSize {
value = transform(value: value, subjectNumber: subjectNumber)
}
return value
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment