@@ -10,80 +10,128 @@ import Foundation
10
10
import Shared
11
11
12
12
13
- /**
14
- Day 1 : Trebuchet?!
15
-
16
- # Part One
17
-
18
- Something is wrong with global snow production, and you've been selected to
19
- take a look. The Elves have even given you a map; on it, they've used stars
20
- to mark the top fifty locations that are likely to be having problems.
21
-
22
- You've been doing this long enough to know that to restore snow operations,
23
- you need to check all **fifty stars** by December 25th.
24
-
25
- Collect stars by solving puzzles. Two puzzles will be made available on each
26
- day in the Advent calendar; the second puzzle is unlocked when you complete
27
- the first. Each puzzle grants **one star**. Good luck!
28
-
29
- You try to ask why they can't just use a weather machine ("not powerful
30
- enough") and where they're even sending you ("the sky") and why your map looks
31
- mostly blank ("you sure ask a lot of questions") and hang on did you just say
32
- the sky ("of course, where do you think snow comes from") when you realize
33
- that the Elves are already loading you into a trebuchet ("please hold still,
34
- we need to strap you in").
35
-
36
- As they're making the final adjustments, they discover that their calibration
37
- document (your puzzle input) has been **amended** by a very young Elf who was
38
- apparently just excited to show off her art skills. Consequently, the Elves
39
- are having trouble reading the values on the document.
40
-
41
- The newly-improved calibration document consists of lines of text; each line
42
- originally contained a specific **calibration value** that the Elves now need
43
- to recover. On each line, the calibration value can be found by combining
44
- the **first digit** and the **last digit** (in that order) to form a single
45
- **two-digit number**.
46
-
47
- For example:
48
-
49
- ```
50
- 1abc2
51
- pqr3stu8vwx
52
- a1b2c3d4e5f
53
- treb7uchet
54
- ```
55
-
56
- In this example, the calibration values of these four lines are `12`, `38`,
57
- `15`, and `77`. Adding these together produces `142`.
58
-
59
- Consider your entire calibration document. **What is the sum of all of the
60
- calibration values?**
61
- */
13
+ /// Day 1 : Trebuchet?!
14
+ ///
15
+ /// # Part One
16
+ ///
17
+ /// Something is wrong with global snow production, and you've been selected to
18
+ /// take a look. The Elves have even given you a map; on it, they've used stars
19
+ /// to mark the top fifty locations that are likely to be having problems.
20
+ ///
21
+ /// You've been doing this long enough to know that to restore snow operations,
22
+ /// you need to check all **fifty stars** by December 25th.
23
+ ///
24
+ /// Collect stars by solving puzzles. Two puzzles will be made available on each
25
+ /// day in the Advent calendar; the second puzzle is unlocked when you complete
26
+ /// the first. Each puzzle grants **one star**. Good luck!
27
+ ///
28
+ /// You try to ask why they can't just use a weather machine ("not powerful
29
+ /// enough") and where they're even sending you ("the sky") and why your map
30
+ /// looks mostly blank ("you sure ask a lot of questions") and hang on did you
31
+ /// just say, "the sky" ("of course, where do you think snow comes from") when
32
+ /// you realize that the Elves are already loading you into a trebuchet ("please
33
+ /// hold still, we need to strap you in").
34
+ ///
35
+ /// As they're making the final adjustments, they discover that their
36
+ /// calibration document (your puzzle input) has been **amended** by a very
37
+ /// young Elf who was apparently just excited to show off her art skills.
38
+ /// Consequently, the Elves are having trouble reading the values on
39
+ /// the document.
40
+ ///
41
+ /// The newly-improved calibration document consists of lines of text; each line
42
+ /// originally contained a specific **calibration value** that the Elves now
43
+ /// need to recover. On each line, the calibration value can be found by
44
+ /// combining the **first digit** and the **last digit** (in that order) to form
45
+ /// a single **two-digit number**.
46
+ ///
47
+ /// For example:
48
+ ///
49
+ /// ```
50
+ /// 1abc2
51
+ /// pqr3stu8vwx
52
+ /// a1b2c3d4e5f
53
+ /// treb7uchet
54
+ /// ```
55
+ ///
56
+ /// In this example, the calibration values of these four lines are `12`, `38`,
57
+ /// `15`, and `77`. Adding these together produces **`142`**.
58
+ ///
59
+ /// Consider your entire calibration document. **What is the sum of all of the
60
+ /// calibration values?**
61
+ ///
62
+ /// # Part Two
63
+ ///
64
+ /// Your calculation isn't quite right. It looks like some of the digits are
65
+ /// actually **spelled out with letters**: `one`, `two`, `three`, `four`,
66
+ /// `five`, `six`, `seven`, `eight`, and `nine` **also** count as
67
+ /// valid "digits".
68
+ ///
69
+ /// Equipped with this new information, you now need to find the real first and
70
+ /// last digit on each line. For example:
71
+ ///
72
+ /// ```
73
+ /// two1nine
74
+ /// eightwothree
75
+ /// abcone2threexyz
76
+ /// xtwone3four
77
+ /// 4nineeightseven2
78
+ /// zoneight234
79
+ /// 7pqrstsixteen
80
+ /// ```
81
+ ///
82
+ /// In this example, the calibration values are `29`, `83`, `13`, `24`, `42`,
83
+ /// `14`, and `76`. Adding these together produces **`281`**.
84
+ ///
85
+ /// **What is the sum of all of the calibration values?**
62
86
@main
63
87
struct Trebuchet : AsyncParsableCommand
64
88
{
65
- /// Enumeration for argument which activates "Part Two" behavior
66
- enum Mode : String , ExpressibleByArgument , CaseIterable
67
- {
68
- case modeA
69
- case modeB
70
- }
71
-
72
- @Option ( help: " <#Argument used to activate “Part Two” behavior.#> " )
73
- var mode : Mode
89
+ @Flag ( help: " Search for both cardinal values ('one', 'two', ...) and integers. " )
90
+ var includeCardinals : Bool = false
74
91
}
75
92
76
93
77
94
// MARK: - Command Execution
78
95
79
96
extension Trebuchet
80
97
{
98
+ static let cardinalMapping : [ ( cardinalString: String , arabicString: String , integerString: Int ) ] = [
99
+ ( " zero " , " 0 " , 0 ) ,
100
+ ( " one " , " 1 " , 1 ) ,
101
+ ( " two " , " 2 " , 2 ) ,
102
+ ( " three " , " 3 " , 3 ) ,
103
+ ( " four " , " 4 " , 4 ) ,
104
+ ( " five " , " 5 " , 5 ) ,
105
+ ( " six " , " 6 " , 6 ) ,
106
+ ( " seven " , " 7 " , 7 ) ,
107
+ ( " eight " , " 8 " , 8 ) ,
108
+ ( " nine " , " 9 " , 9 ) ,
109
+ ]
110
+
111
+
81
112
mutating func run( ) async throws
82
113
{
83
- while let inputLine = readLine ( )
114
+ let input : AsyncLineSequence = FileHandle . standardInput. bytes. lines // URL.homeDirectory.appending(path: "Desktop/input.txt").lines
115
+ let calibrationSum : Int = try await input. reduce ( 0 )
84
116
{
85
- print ( " \( inputLine) " )
117
+ currentSum, inputLine in
118
+
119
+ let integerIndices : [ ( index: String . Index , intValue: Int ) ] = Trebuchet . cardinalMapping. reduce ( into: [ ] )
120
+ {
121
+ result, mapping in
122
+ result. append ( contentsOf: inputLine. indices ( of: mapping. arabicString) . map { ( $0, mapping. integerString) } )
123
+
124
+ if ( self . includeCardinals)
125
+ {
126
+ result. append ( contentsOf: inputLine. indices ( of: mapping. cardinalString) . map { ( $0, mapping. integerString) } )
127
+ }
128
+ }
129
+ . sorted { $0. index < $1. index }
130
+
131
+ return ( currentSum + ( integerIndices. first!. intValue * 10 ) + integerIndices. last!. intValue)
86
132
}
133
+
134
+ print ( calibrationSum)
87
135
}
88
136
}
89
137
0 commit comments