@@ -13,6 +13,9 @@ struct AdventOfCode: AsyncParsableCommand {
13
13
@Flag ( help: " Benchmark the time taken by the solution " )
14
14
var benchmark : Bool = false
15
15
16
+ @Flag ( help: " Run all the days available " )
17
+ var all : Bool = false
18
+
16
19
/// The selected day, or the latest day if no selection is provided.
17
20
var selectedChallenge : any AdventDay {
18
21
get throws {
@@ -33,36 +36,46 @@ struct AdventOfCode: AsyncParsableCommand {
33
36
allChallenges. max ( by: { $0. day < $1. day } ) !
34
37
}
35
38
36
- func run( part: ( ) async throws -> Any , named: String ) async -> Duration {
37
- var result : Result < Any , Error > = . success ( " <unsolved> " )
39
+ func run< T > ( part: ( ) async throws -> T , named: String ) async -> Duration {
40
+ var result : Result < T , Error > ?
38
41
let timing = await ContinuousClock ( ) . measure {
39
42
do {
40
43
result = . success( try await part ( ) )
41
44
} catch {
42
45
result = . failure( error)
43
46
}
44
47
}
45
- switch result {
48
+ switch result! {
46
49
case . success( let success) :
47
50
print ( " \( named) : \( success) " )
51
+ case . failure( let failure as PartUnimplemented ) :
52
+ print ( " Day \( failure. day) part \( failure. part) unimplemented " )
48
53
case . failure( let failure) :
49
54
print ( " \( named) : Failed with error: \( failure) " )
50
55
}
51
56
return timing
52
57
}
53
58
54
59
func run( ) async throws {
55
- let challenge = try selectedChallenge
56
- print ( " Executing Advent of Code challenge \( challenge. day) ... " )
60
+ let challenges =
61
+ if all {
62
+ allChallenges
63
+ } else {
64
+ try [ selectedChallenge]
65
+ }
57
66
58
- let timing1 = await run ( part : challenge. part1 , named : " Part 1 " )
59
- let timing2 = await run ( part : challenge . part2 , named : " Part 2 " )
67
+ for challenge in challenges {
68
+ print ( " Executing Advent of Code challenge \( challenge . day ) ... " )
60
69
61
- if benchmark {
62
- print ( " Part 1 took \( timing1) , part 2 took \( timing2) . " )
63
- #if DEBUG
64
- print ( " Looks like you're benchmarking debug code. Try swift run -c release " )
65
- #endif
70
+ let timing1 = await run ( part: challenge. part1, named: " Part 1 " )
71
+ let timing2 = await run ( part: challenge. part2, named: " Part 2 " )
72
+
73
+ if benchmark {
74
+ print ( " Part 1 took \( timing1) , part 2 took \( timing2) . " )
75
+ #if DEBUG
76
+ print ( " Looks like you're benchmarking debug code. Try swift run -c release " )
77
+ #endif
78
+ }
66
79
}
67
80
}
68
81
}
0 commit comments