Skip to content

Commit 78a8046

Browse files
committed
Solved 2023 day 7 in Swift.
1 parent 7c68a9e commit 78a8046

File tree

8 files changed

+1413
-4
lines changed

8 files changed

+1413
-4
lines changed

.vscode/launch.json

+60
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,66 @@
391391
"name": "Release day06b (2023\\swift\\06)",
392392
"program": "${workspaceFolder:adventofcode}\\2023\\swift\\06\\.build\\release\\day06b.exe",
393393
"preLaunchTask": "swift: Build Release day06b (2023\\swift\\06)"
394+
},
395+
{
396+
"type": "swift-lldb",
397+
"request": "launch",
398+
"sourceLanguages": [
399+
"swift"
400+
],
401+
"args": [
402+
"--input",
403+
"input.txt"
404+
],
405+
"cwd": "${workspaceFolder:adventofcode}\\2023\\swift\\07",
406+
"name": "Debug day07 (2023\\swift\\07)",
407+
"program": "${workspaceFolder:adventofcode}\\2023\\swift\\07\\.build\\debug\\day07.exe",
408+
"preLaunchTask": "swift: Build Debug day07 (2023\\swift\\07)"
409+
},
410+
{
411+
"type": "swift-lldb",
412+
"request": "launch",
413+
"sourceLanguages": [
414+
"swift"
415+
],
416+
"args": [
417+
"--input",
418+
"input.txt"
419+
],
420+
"cwd": "${workspaceFolder:adventofcode}\\2023\\swift\\07",
421+
"name": "Release day07 (2023\\swift\\07)",
422+
"program": "${workspaceFolder:adventofcode}\\2023\\swift\\07\\.build\\release\\day07.exe",
423+
"preLaunchTask": "swift: Build Release day07 (2023\\swift\\07)"
424+
},
425+
{
426+
"type": "swift-lldb",
427+
"request": "launch",
428+
"sourceLanguages": [
429+
"swift"
430+
],
431+
"args": [
432+
"--input",
433+
"input.txt"
434+
],
435+
"cwd": "${workspaceFolder:adventofcode}\\2023\\swift\\07",
436+
"name": "Debug day07b (2023\\swift\\07)",
437+
"program": "${workspaceFolder:adventofcode}\\2023\\swift\\07\\.build\\debug\\day07b.exe",
438+
"preLaunchTask": "swift: Build Debug day07b (2023\\swift\\07)"
439+
},
440+
{
441+
"type": "swift-lldb",
442+
"request": "launch",
443+
"sourceLanguages": [
444+
"swift"
445+
],
446+
"args": [
447+
"--input",
448+
"input.txt"
449+
],
450+
"cwd": "${workspaceFolder:adventofcode}\\2023\\swift\\07",
451+
"name": "Release day07b (2023\\swift\\07)",
452+
"program": "${workspaceFolder:adventofcode}\\2023\\swift\\07\\.build\\release\\day07b.exe",
453+
"preLaunchTask": "swift: Build Release day07b (2023\\swift\\07)"
394454
}
395455
]
396456
}

2023/swift/06/Sources/b/main.swift

-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ struct Day06b: ParsableCommand {
66
@Option(help: "Specify the input")
77
public var input: String
88

9-
func numbers(_ fromString: String) -> [Int] {
10-
return fromString.components(separatedBy: " ").compactMap { Int($0) }
11-
}
12-
139
public func run() throws {
1410
guard let filecontent = try? String(contentsOfFile: input, encoding: .utf8)
1511
else {

2023/swift/07/.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
xcuserdata/
5+
DerivedData/
6+
.swiftpm/configuration/registries.json
7+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
8+
.netrc

2023/swift/07/Package.resolved

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"originHash" : "d108cb52e8b032cfe0edc6e3088c58e98a1e2e487bda4a6f366ab262ad70ea01",
3+
"pins" : [
4+
{
5+
"identity" : "swift-argument-parser",
6+
"kind" : "remoteSourceControl",
7+
"location" : "https://github.com/apple/swift-argument-parser",
8+
"state" : {
9+
"revision" : "c8ed701b513cf5177118a175d85fbbbcd707ab41",
10+
"version" : "1.3.0"
11+
}
12+
}
13+
],
14+
"version" : 3
15+
}

2023/swift/07/Package.swift

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// swift-tools-version: 5.10
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "day07",
8+
dependencies: [
9+
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.0.0")
10+
],
11+
targets: [
12+
// Targets are the basic building blocks of a package, defining a module or a test suite.
13+
// Targets can depend on other targets in this package and products from dependencies.
14+
.executableTarget(
15+
name: "day07",
16+
dependencies: [
17+
.product(name: "ArgumentParser", package: "swift-argument-parser")
18+
],
19+
path: "Sources/a"),
20+
.executableTarget(
21+
name: "day07b",
22+
dependencies: [
23+
.product(name: "ArgumentParser", package: "swift-argument-parser")
24+
],
25+
path: "Sources/b")
26+
]
27+
)

2023/swift/07/Sources/a/main.swift

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import ArgumentParser
2+
import Foundation
3+
4+
extension String {
5+
var length: Int {
6+
return count
7+
}
8+
9+
subscript (i: Int) -> String {
10+
return self[i ..< i + 1]
11+
}
12+
13+
subscript (r: Range<Int>) -> String {
14+
let range = Range(uncheckedBounds: (lower: max(0, min(length, r.lowerBound)),
15+
upper: min(length, max(0, r.upperBound))))
16+
let start = index(startIndex, offsetBy: range.lowerBound)
17+
let end = index(start, offsetBy: range.upperBound - range.lowerBound)
18+
return String(self[start ..< end])
19+
}
20+
}
21+
22+
enum HandType: Int {
23+
case fiveOfKind = 6
24+
case fourOfKind = 5
25+
case fullHouse = 4
26+
case threeOfKind = 3
27+
case twoPairs = 2
28+
case onePair = 1
29+
case highCard = 0
30+
}
31+
32+
struct Hand {
33+
let cards: String
34+
let type: HandType
35+
}
36+
37+
let values = ["2", "3", "4", "5", "6", "7", "8", "9", "T" ,"J", "Q", "K", "A"]
38+
39+
func compareHands (lhs: String, rhs: String) -> Bool {
40+
for i in 0..<lhs.count {
41+
if lhs[i] == rhs[i] {
42+
continue
43+
}
44+
return values.firstIndex(of: lhs[i])! < values.firstIndex(of: rhs[i])!
45+
}
46+
return false
47+
}
48+
49+
struct Bid {
50+
let hand: Hand
51+
let bid: Int
52+
}
53+
54+
extension Hand: Comparable {
55+
static func < (lhs: Hand, rhs: Hand) -> Bool {
56+
if lhs.type.rawValue == rhs.type.rawValue {
57+
return compareHands(lhs: lhs.cards, rhs: rhs.cards)
58+
}
59+
return lhs.type.rawValue < rhs.type.rawValue
60+
}
61+
}
62+
63+
extension Bid: Comparable {
64+
static func < (lhs: Bid, rhs: Bid) -> Bool {
65+
return lhs.hand < rhs.hand
66+
}
67+
}
68+
69+
@main
70+
struct Day07: ParsableCommand {
71+
@Option(help: "Specify the input")
72+
public var input: String
73+
74+
func type(_ hand: String) -> HandType {
75+
let cardsValues = Array(hand)
76+
let cardValueSet = Set(cardsValues)
77+
let cardValueCount = cardValueSet.map { value in
78+
cardsValues.filter { $0 == value }.count
79+
}
80+
81+
let sortedCardValueCount = cardValueCount.sorted()
82+
let isFiveOfKind = sortedCardValueCount == [5]
83+
let isFourOfKind = sortedCardValueCount == [1, 4]
84+
let isFullHouse = sortedCardValueCount == [2, 3]
85+
let isThreeOfKind = sortedCardValueCount == [1, 1, 3]
86+
let isTwoPairs = sortedCardValueCount == [1, 2, 2]
87+
let isOnePair = sortedCardValueCount == [1, 1, 1, 2]
88+
let isHighCard = sortedCardValueCount == [1, 1, 1, 1, 1]
89+
if isFiveOfKind {
90+
return .fiveOfKind
91+
} else if isFourOfKind {
92+
return .fourOfKind
93+
} else if isFullHouse {
94+
return .fullHouse
95+
} else if isThreeOfKind {
96+
return .threeOfKind
97+
} else if isTwoPairs {
98+
return .twoPairs
99+
} else if isOnePair {
100+
return .onePair
101+
} else if isHighCard {
102+
return .highCard
103+
}
104+
return .highCard
105+
}
106+
107+
public func run() throws {
108+
guard let filecontent = try? String(contentsOfFile: input, encoding: .utf8)
109+
else {
110+
print("Failed to open file \(input)")
111+
return
112+
}
113+
114+
let lines = filecontent.components(separatedBy: .newlines)
115+
var bids: [Bid] = []
116+
for line in lines {
117+
let cards = line.components(separatedBy: " ")
118+
let hand = Hand(cards: cards[0], type: type(cards[0]))
119+
bids.append(Bid(hand: hand, bid: Int(cards[1])!))
120+
}
121+
bids.sort()
122+
var result = 0
123+
for i in 0..<bids.count {
124+
print(bids[i].hand.cards)
125+
result += bids[i].bid * (i+1)
126+
}
127+
128+
print(result)
129+
}
130+
}

0 commit comments

Comments
 (0)