Initial commit
This commit is contained in:
commit
dae7c34265
17 changed files with 2001 additions and 0 deletions
30
common/LoadData.swift
Normal file
30
common/LoadData.swift
Normal file
|
@ -0,0 +1,30 @@
|
|||
import Foundation
|
||||
|
||||
func loadData(day: Int) -> String {
|
||||
guard let session = getenv("SESSION") else {
|
||||
fatalError("Missing session env var")
|
||||
}
|
||||
|
||||
var request = URLRequest(url: URL(string: "https://adventofcode.com/2020/day/\(day)/input")!)
|
||||
request.setValue("session=\(String(cString: session))", forHTTPHeaderField: "Cookie")
|
||||
|
||||
var result: String? = nil
|
||||
|
||||
let group = DispatchGroup()
|
||||
|
||||
group.enter()
|
||||
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
|
||||
guard error == nil, let data = data, let response = response as? HTTPURLResponse, response.statusCode == 200 else {
|
||||
fatalError("Cannot get input from server")
|
||||
}
|
||||
|
||||
result = String(data: data, encoding: .utf8)
|
||||
group.leave()
|
||||
}
|
||||
|
||||
task.resume()
|
||||
|
||||
group.wait()
|
||||
|
||||
return result!
|
||||
}
|
26
common/Scanner+Extensions.swift
Normal file
26
common/Scanner+Extensions.swift
Normal file
|
@ -0,0 +1,26 @@
|
|||
import Foundation
|
||||
|
||||
extension Scanner {
|
||||
@discardableResult
|
||||
func string(_ string: String) -> Bool {
|
||||
return scanString(string) != nil
|
||||
}
|
||||
|
||||
func integers() -> [Int]? {
|
||||
var numbers: [Int] = []
|
||||
while !isAtEnd {
|
||||
guard let num = scanInt() else { return nil }
|
||||
numbers.append(num)
|
||||
}
|
||||
return numbers
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
extension String {
|
||||
func lines() -> [String] {
|
||||
var result: [String] = []
|
||||
enumerateLines { line, _ in result.append(line) }
|
||||
return result
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue