AoC20/lib/day15/test.swift

23 lines
576 B
Swift
Raw Normal View History

2020-12-16 22:43:05 +00:00
import Foundation
func vanEckSequence(start: [Int], maxLength: Int) -> [Int] {
let toCompute = maxLength - start.count
var seq = start + Array(repeating: 0, count: toCompute)
// print(seq.count)
for i in start.count..<maxLength {
// print("i: \(i)")
if let prevIndex = seq[0..<(i - 1)].lastIndex(of: seq[i - 1]) {
seq[i] = (i - 1) - prevIndex
} else {
seq[i] = 0
}
}
return seq
}
let seq = vanEckSequence(start: [9,12,1,4,17,0,18], maxLength: 30000000)
// print(seq)
print(seq.last!)