Skip to content

Commit 244012e

Browse files
committed
Solved 2023 day 4 part 2 in Swift
1 parent 34d62bc commit 244012e

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

.vscode/launch.json

+30
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,36 @@
241241
"name": "Release day04 (2023\\swift\\04)",
242242
"program": "${workspaceFolder:adventofcode}\\2023\\swift\\04\\.build\\release\\day04.exe",
243243
"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)"
244274
}
245275
]
246276
}

2023/swift/04/Package.swift

+7
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,12 @@ let package = Package(
1818
],
1919
path: "Sources/a"
2020
),
21+
.executableTarget(
22+
name: "day04b",
23+
dependencies: [
24+
.product(name: "ArgumentParser", package: "swift-argument-parser"),
25+
],
26+
path: "Sources/b"
27+
),
2128
]
2229
)

2023/swift/04/Sources/b/main.swift

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
}

0 commit comments

Comments
 (0)