AoC/2020/common/LoadData.swift

29 lines
916 B
Swift
Raw Normal View History

2020-12-11 22:25:14 +01:00
import Foundation
func loadData(day: Int) -> String {
let session = (try! String(contentsOf: FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent(".config/aoc2020session"))).trimmingCharacters(in: .whitespacesAndNewlines)
2020-12-11 22:25:14 +01:00
var request = URLRequest(url: URL(string: "https://adventofcode.com/2020/day/\(day)/input")!)
request.setValue("session=\(session)", forHTTPHeaderField: "Cookie")
2020-12-11 22:25:14 +01:00
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!
}