Move to subdirectory
This commit is contained in:
parent
c4ae807a5f
commit
c7deb3c71d
47 changed files with 1208 additions and 0 deletions
38
2020/common/Extensions.swift
Normal file
38
2020/common/Extensions.swift
Normal file
|
@ -0,0 +1,38 @@
|
|||
import Foundation
|
||||
|
||||
extension Scanner {
|
||||
@discardableResult
|
||||
func string(_ string: String) -> Bool {
|
||||
return scanString(string) != nil
|
||||
}
|
||||
|
||||
func integers() -> [Int] {
|
||||
var numbers: [Int] = []
|
||||
while let num = scanInt() {
|
||||
numbers.append(num)
|
||||
}
|
||||
return numbers
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
extension String {
|
||||
func lines() -> [String] {
|
||||
var result: [String] = []
|
||||
enumerateLines { line, _ in result.append(line) }
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
extension Sequence {
|
||||
func print(_ prefix: String = "") -> Self {
|
||||
Swift.print(prefix, Array(self))
|
||||
return self
|
||||
}
|
||||
}
|
||||
|
||||
extension Collection where Element: Collection {
|
||||
func flatten() -> [Element.Element] {
|
||||
flatMap { $0 }
|
||||
}
|
||||
}
|
28
2020/common/LoadData.swift
Normal file
28
2020/common/LoadData.swift
Normal file
|
@ -0,0 +1,28 @@
|
|||
import Foundation
|
||||
|
||||
func loadData(day: Int) -> String {
|
||||
let session = (try! String(contentsOf: FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent(".config/aoc2020session"))).trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
|
||||
var request = URLRequest(url: URL(string: "https://adventofcode.com/2020/day/\(day)/input")!)
|
||||
request.setValue("session=\(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!
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue