AoC/2021/common.swift

28 lines
677 B
Swift
Raw Normal View History

2021-12-15 19:35:16 +01:00
import Foundation
2021-12-12 11:21:58 +01:00
protocol Puzzle {
mutating func run()
init()
}
extension Puzzle {
static func main() {
2021-12-15 19:35:16 +01:00
let start = Date()
2021-12-12 11:21:58 +01:00
var instance = Self()
instance.run()
2021-12-15 19:35:16 +01:00
let duration = Date().timeIntervalSince(start)
if duration > 1 {
print(String(format: "Took %.2f s", duration))
} else {
print(String(format: "Took %.2f ms", 1000 * duration))
}
2021-12-12 11:21:58 +01:00
}
}
extension RangeReplaceableCollection {
mutating func removeFirst(where predicate: (Element) -> Bool) -> Element? {
guard let index = firstIndex(where: predicate) else { return nil }
return remove(at: index)
}
}