generated from swiftlang/swift-aoc-starter-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetDay
executable file
·146 lines (110 loc) · 3.91 KB
/
GetDay
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#! /usr/bin/env swift
// First argument appears to be the path (get-day.swift), so we have to include (and skip) this
guard CommandLine.argc == 2 else {
fatalError("Invalid number of arguments. Please input one argument (day number)")
}
guard let day = Int(CommandLine.arguments[1]) else {
fatalError("Could not parse day number. Please try a different input")
}
var sessionData: String = ""
let saveFileURL = URL(fileURLWithPath: ".advent-of-code")
if (FileManager.default.fileExists(atPath: ".advent-of-code")) {
print("Loading session cookie from .advent-of-code file")
sessionData = try! String(contentsOf: saveFileURL, encoding: .utf8)
} else {
print("Session cookie value (from your browser, associated with your logged in account): ", separator: "")
sessionData = readLine()!.trimmingCharacters(in: .whitespacesAndNewlines)
print("Saving to .advent-of-code file")
try! sessionData.write(to: saveFileURL, atomically: true, encoding: .utf8)
print("Successfully wrote session data file, remember to add it to your .gitignore")
}
import Foundation
let cookieProperties: [HTTPCookiePropertyKey: Any] = [
.domain: ".adventofcode.com",
.path: "/",
.name: "session",
.value: sessionData,
.secure: true,
.expires: Date(timeIntervalSinceNow: 120)
]
if let cookie = HTTPCookie(properties: cookieProperties) {
HTTPCookieStorage.shared.setCookie(cookie)
} else {
fatalError("Something went wrong with your cookie")
}
guard let url = URL(string: "https://adventofcode.com/2024/day/\(day)/input") else {
fatalError("Bad URL")
}
var inputRequest = URLRequest(url: url)
inputRequest.setValue("Simple Swift AoC Tool by [email protected] as part of github.com/BruceMcRooster/aoc-2024", forHTTPHeaderField: "User-Agent")
let dayString = "Day" + String(format: "%02d", day)
do {
let (data, response) = try await URLSession.shared.data(for: inputRequest)
guard FileManager.default.createFile(
atPath: "Sources/Data/\(dayString).txt",
contents: data
) else {
fatalError("Could not successfully create input file")
}
} catch {
fatalError("Something went wrong while downloading input data")
}
print("Downloaded input file to Sources/Data/\(dayString).txt.")
let sourceFileDefault = """
import Algorithms
struct \(dayString): AdventDay {
var data: String
func part1() -> Any {
return 0
}
func part2() -> Any {
return 0
}
}
"""
guard FileManager.default.createFile(
atPath: "Sources/\(dayString).swift",
contents: sourceFileDefault.data(using: .utf8)
) else {
fatalError("Something went wrong while creating the solution source file")
}
print("Created source file (Sources/\(dayString).swift).")
let testFileDefault = """
import Testing
@testable import AdventOfCode
struct \(dayString)Tests {
@Test func testPart1() async throws {
#expect(true)
}
@Test func testPart2() async throws {
#expect(true)
}
}
"""
guard FileManager.default.createFile(
atPath: "Tests/\(dayString).swift",
contents: testFileDefault.data(using: .utf8)
) else {
fatalError("Something went wrong while creating the solution test file")
}
print("Created test file (Tests/\(dayString).swift).")
var adventOfCodeFile = try! String(contentsOf: URL(fileURLWithPath: "Sources/AdventOfCode.swift"), encoding: .utf8)
let editedAdventFile = adventOfCodeFile
.replacingOccurrences(
of: "Day\(String(format: "%02d", day-1))()",
with: "Day\(String(format: "%02d", day-1))(), \n \(dayString)()")
if editedAdventFile == adventOfCodeFile {
fatalError("Could not insert \(dayString)() to AdventOfCode.swift array. Check that Day\(String(format: "%02d", day-1))() is the last element")
}
do {
try editedAdventFile.write(
to: URL(fileURLWithPath: "Sources/AdventOfCode.swift"),
atomically: true,
encoding: .utf8
)
} catch {
fatalError("Could not write \(dayString)() to AdventOfCode.swift array")
}
print("Added \(dayString)() to AdventOfCode.swift array")
print("Done. Good luck!")
exit(0)