-
Notifications
You must be signed in to change notification settings - Fork 12
/
setup
executable file
·155 lines (119 loc) · 3.19 KB
/
setup
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
147
148
149
150
151
152
153
154
155
#!/usr/bin/xcrun --toolchain default swift
import Foundation
let today = Date()
let calendar = Calendar(identifier: .gregorian)
let day = calendar.component(.day, from: today)
let month = calendar.component(.month, from: today)
let year = calendar.component(.year, from: today)
let todayString = "\(month)/\(day)/\(year % 100)"
let u = URL(fileURLWithPath: FileManager.default.currentDirectoryPath)
let sources = u.appendingPathComponent("Sources")
let yearFolder = sources.appendingPathComponent("AOC\(year)")
print("Creating \(yearFolder.path)")
mkdir(yearFolder)
let mainContents = """
//
// AOC\(year).swift
//
//
// Created by Dave DeLong on \(todayString).
//
import Foundation
@_exported import AOCCore
public struct AOC\(year): Year {
public static let days: Array<any Day> = [
Day1(),
Day2(),
Day3(),
Day4(),
Day5(),
Day6(),
Day7(),
Day8(),
Day9(),
Day10(),
Day11(),
Day12(),
Day13(),
Day14(),
Day15(),
Day16(),
Day17(),
Day18(),
Day19(),
Day20(),
Day21(),
Day22(),
Day23(),
Day24(),
Day25(),
]
}
"""
mainContents >> yearFolder.appendingPathComponent("AOC\(year).swift")
for day in 1 ... 25 {
let dayFolder = yearFolder.appendingPathComponent("Day \(day)")
mkdir(dayFolder)
let dayContents = """
//
// Day\(day).swift
// AOC\(year)
//
// Created by Dave DeLong on \(todayString).
// Copyright © \(year) Dave DeLong. All rights reserved.
//
struct Day\(day): Day {
typealias Part1 = String
typealias Part2 = String
static var rawInput: String? { nil }
func run() async throws -> (Part1, Part2) {
let p1 = try await part1()
let p2 = try await part2()
return (p1, p2)
}
func part1() async throws -> Part1 {
return #function
}
func part2() async throws -> Part2 {
return #function
}
}
"""
dayContents >> dayFolder.appendingPathComponent("\(year)-Day\(day).swift")
"" >> dayFolder.appendingPathComponent("input.txt")
}
let testFile = u.appendingPathComponent("Tests").appendingPathComponent("AOCTests").appendingPathComponent("Test\(year).swift")
var contents = """
//
// Test\(year).swift
// AOCTests
//
// Created by Dave DeLong on \(todayString).
// Copyright © \(year) Dave DeLong. All rights reserved.
//
import XCTest
@testable import AOC\(year)
class Test\(year): XCTestCase {
"""
for day in 1 ... 25 {
let testContents = """
func testDay\(day)() async throws {
let d = Day\(day)()
let (p1, p2) = try await d.run()
XCTAssertEqual(p1, "")
XCTAssertEqual(p2, "")
}
"""
contents += testContents
}
contents += """
}
"""
contents >> testFile
func mkdir(_ path: URL) {
try? FileManager.default.createDirectory(at: path, withIntermediateDirectories: true, attributes: nil)
}
infix operator >>
func >> (lhs: String, rhs: URL) {
try! Data(lhs.utf8).write(to: rhs, options: [])
}