Day 25
This commit is contained in:
parent
7b35583b63
commit
057729328f
2 changed files with 128 additions and 0 deletions
35
day25/main.swift
Normal file
35
day25/main.swift
Normal file
|
@ -0,0 +1,35 @@
|
|||
import Foundation
|
||||
|
||||
let input = loadData(day: 25).lines().compactMap(Int.init)
|
||||
|
||||
func loop(subject: Int, times: Int) -> Int {
|
||||
var value = 1
|
||||
for _ in 0..<times {
|
||||
value = (value * subject) % 20201227
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func findLoopSize(subject: Int, expected: Int) -> Int {
|
||||
var value = 1
|
||||
for i in 0...Int.max {
|
||||
if value == expected {
|
||||
return i
|
||||
}
|
||||
|
||||
value = (value * subject) % 20201227
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
let sizes = input.map { findLoopSize(subject: 7, expected: $0) }
|
||||
print(sizes)
|
||||
|
||||
let (pub1, pub2) = (input[0], input[1])
|
||||
let (loop1, loop2) = (sizes[0], sizes[1])
|
||||
|
||||
let secret1 = loop(subject: pub2, times: loop1)
|
||||
let secret2 = loop(subject: pub1, times: loop2)
|
||||
|
||||
print(secret1, secret2)
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue