File tree Expand file tree Collapse file tree 3 files changed +27
-14
lines changed
Expand file tree Collapse file tree 3 files changed +27
-14
lines changed Original file line number Diff line number Diff line change @@ -6,3 +6,4 @@ DerivedData/
66.swiftpm /configuration /registries.json
77.swiftpm /xcode /package.xcworkspace /contents.xcworkspacedata
88.netrc
9+ input /
Original file line number Diff line number Diff line change 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-
71import ArgumentParser
82import Foundation
3+ import Parsing
94
105extension 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 {
Original file line number Diff line number Diff line change @@ -2,20 +2,25 @@ import ArgumentParser
22import Foundation
33import 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
You can’t perform that action at this time.
0 commit comments