File tree 3 files changed +82
-0
lines changed
3 files changed +82
-0
lines changed Original file line number Diff line number Diff line change 241
241
"name" : " Release day04 (2023\\ swift\\ 04)" ,
242
242
"program" : " ${workspaceFolder:adventofcode}\\ 2023\\ swift\\ 04\\ .build\\ release\\ day04.exe" ,
243
243
"preLaunchTask" : " swift: Build Release day04 (2023\\ swift\\ 04)"
244
+ },
245
+ {
246
+ "type" : " swift-lldb" ,
247
+ "request" : " launch" ,
248
+ "sourceLanguages" : [
249
+ " swift"
250
+ ],
251
+ "args" : [
252
+ " --input" ,
253
+ " input.txt"
254
+ ],
255
+ "cwd" : " ${workspaceFolder:adventofcode}\\ 2023\\ swift\\ 04" ,
256
+ "name" : " Debug day04b (2023\\ swift\\ 04)" ,
257
+ "program" : " ${workspaceFolder:adventofcode}\\ 2023\\ swift\\ 04\\ .build\\ debug\\ day04b.exe" ,
258
+ "preLaunchTask" : " swift: Build Debug day04b (2023\\ swift\\ 04)"
259
+ },
260
+ {
261
+ "type" : " swift-lldb" ,
262
+ "request" : " launch" ,
263
+ "sourceLanguages" : [
264
+ " swift"
265
+ ],
266
+ "args" : [
267
+ " --input" ,
268
+ " input.txt"
269
+ ],
270
+ "cwd" : " ${workspaceFolder:adventofcode}\\ 2023\\ swift\\ 04" ,
271
+ "name" : " Release day04b (2023\\ swift\\ 04)" ,
272
+ "program" : " ${workspaceFolder:adventofcode}\\ 2023\\ swift\\ 04\\ .build\\ release\\ day04b.exe" ,
273
+ "preLaunchTask" : " swift: Build Release day04b (2023\\ swift\\ 04)"
244
274
}
245
275
]
246
276
}
Original file line number Diff line number Diff line change @@ -18,5 +18,12 @@ let package = Package(
18
18
] ,
19
19
path: " Sources/a "
20
20
) ,
21
+ . executableTarget(
22
+ name: " day04b " ,
23
+ dependencies: [
24
+ . product( name: " ArgumentParser " , package : " swift-argument-parser " ) ,
25
+ ] ,
26
+ path: " Sources/b "
27
+ ) ,
21
28
]
22
29
)
Original file line number Diff line number Diff line change
1
+ import ArgumentParser
2
+ import Foundation
3
+
4
+ struct Card {
5
+ var count = 1
6
+ var score = 0
7
+ }
8
+
9
+ @main
10
+ struct Day04b : ParsableCommand {
11
+ @Option ( help: " Specify the input " )
12
+ public var input : String
13
+
14
+ func numbers( fromString: String ) -> [ Int ] {
15
+ return fromString. components ( separatedBy: " " ) . compactMap { Int ( $0) }
16
+ }
17
+ public func run( ) throws {
18
+ guard let filecontent = try ? String ( contentsOfFile: input, encoding: . utf8)
19
+ else {
20
+ print ( " Failed to open file \( input) " )
21
+ return
22
+ }
23
+ var cards = [ Int: Card] ( )
24
+ for line in filecontent. components ( separatedBy: . newlines) {
25
+ let split = line. components ( separatedBy: [ " : " , " | " ] )
26
+ let card = Int ( split [ 0 ] . substring ( from: 4 ) . trimmingCharacters ( in: [ " " ] ) ) !
27
+ let winning = Set < Int > ( numbers ( fromString: split [ 1 ] ) )
28
+ let chosen = Set < Int > ( numbers ( fromString: split [ 2 ] ) )
29
+ let winningCount = winning. intersection ( chosen) . count
30
+ cards [ card] = Card ( count: 1 , score: winningCount)
31
+ }
32
+
33
+ var result = 0
34
+ for card in cards. keys. sorted ( ) {
35
+ let c = cards [ card] !
36
+ result += c. count
37
+ if c. score > 0 {
38
+ for i in 1 ... c. score {
39
+ cards [ card + i] !. count += c. count
40
+ }
41
+ }
42
+ }
43
+ print ( result)
44
+ }
45
+ }
You can’t perform that action at this time.
0 commit comments