Skip to content

Commit e446d79

Browse files
committed
Cleanup
1 parent 7ed1158 commit e446d79

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ DerivedData/
66
.swiftpm/configuration/registries.json
77
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
88
.netrc
9+
input/

Sources/AdventOfCode.swift

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
// The Swift Programming Language
2-
// https://docs.swift.org/swift-book
3-
//
4-
// Swift Argument Parser
5-
// https://swiftpackageindex.com/apple/swift-argument-parser/documentation
6-
71
import ArgumentParser
82
import Foundation
3+
import Parsing
94

105
extension ParsableCommand {
116
var input: AnyIterator<String> {
@@ -33,7 +28,19 @@ extension ParsableCommand {
3328
default: `default`
3429
)
3530
}
31+
}
32+
33+
protocol ParsingCommand: ParsableCommand {
34+
associatedtype Output
35+
associatedtype ParserType where ParserType: Parser<Substring.UTF8View, Output>
36+
static var parser: ParserType { get }
37+
func parsed() throws -> Output
38+
}
3639

40+
extension ParsingCommand {
41+
func parsed() throws -> Output {
42+
try Self.parser.parse(stdin)
43+
}
3744
}
3845

3946
@main struct AdventOfCode: ParsableCommand {

Sources/Day1.swift

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,25 @@ import ArgumentParser
22
import Foundation
33
import Parsing
44

5-
struct Day1: ParsableCommand {
6-
static var parser: some Parser<Substring, (Int, Int)> {
7-
Parse(input: Substring.self) {
8-
Int.parser()
9-
Skip {
10-
Whitespace()
5+
6+
struct Day1: ParsingCommand {
7+
static var parser: some Parser<Substring.UTF8View, [(Int, Int)]> {
8+
Parse(input: Substring.UTF8View.self) {
9+
Many {
10+
Int.parser()
11+
Whitespace(.horizontal)
12+
Int.parser()
13+
} separator: {
14+
Whitespace(.vertical)
15+
} terminator: {
16+
Whitespace(.vertical)
1117
}
12-
Int.parser()
1318
}
1419
}
1520

1621

1722
func run() throws {
18-
let numbers = try input.map { try Self.parser.parse($0) }
23+
let numbers = try parsed()
1924
let first = numbers.map(\.0)
2025
let second = numbers.map(\.1)
2126

0 commit comments

Comments
 (0)