Skip to content

Commit 628f5f6

Browse files
committed
Solved 2023 day 9 in Swift
1 parent 40fc6ac commit 628f5f6

File tree

8 files changed

+423
-0
lines changed

8 files changed

+423
-0
lines changed

.vscode/launch.json

+60
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,66 @@
511511
"name": "Release day08b (2023\\swift\\08)",
512512
"program": "${workspaceFolder:adventofcode}\\2023\\swift\\08\\.build\\release\\day08b.exe",
513513
"preLaunchTask": "swift: Build Release day08b (2023\\swift\\08)"
514+
},
515+
{
516+
"type": "swift-lldb",
517+
"request": "launch",
518+
"sourceLanguages": [
519+
"swift"
520+
],
521+
"args": [
522+
"--input",
523+
"input.txt"
524+
],
525+
"cwd": "${workspaceFolder:adventofcode}\\2023\\swift\\09",
526+
"name": "Debug day09 (2023\\swift\\09)",
527+
"program": "${workspaceFolder:adventofcode}\\2023\\swift\\09\\.build\\debug\\day09.exe",
528+
"preLaunchTask": "swift: Build Debug day09 (2023\\swift\\09)"
529+
},
530+
{
531+
"type": "swift-lldb",
532+
"request": "launch",
533+
"sourceLanguages": [
534+
"swift"
535+
],
536+
"args": [
537+
"--input",
538+
"input.txt"
539+
],
540+
"cwd": "${workspaceFolder:adventofcode}\\2023\\swift\\09",
541+
"name": "Release day09 (2023\\swift\\09)",
542+
"program": "${workspaceFolder:adventofcode}\\2023\\swift\\09\\.build\\release\\day09.exe",
543+
"preLaunchTask": "swift: Build Release day09 (2023\\swift\\09)"
544+
},
545+
{
546+
"type": "swift-lldb",
547+
"request": "launch",
548+
"sourceLanguages": [
549+
"swift"
550+
],
551+
"args": [
552+
"--input",
553+
"input.txt"
554+
],
555+
"cwd": "${workspaceFolder:adventofcode}\\2023\\swift\\09",
556+
"name": "Debug day09b (2023\\swift\\09)",
557+
"program": "${workspaceFolder:adventofcode}\\2023\\swift\\09\\.build\\debug\\day09b.exe",
558+
"preLaunchTask": "swift: Build Debug day09b (2023\\swift\\09)"
559+
},
560+
{
561+
"type": "swift-lldb",
562+
"request": "launch",
563+
"sourceLanguages": [
564+
"swift"
565+
],
566+
"args": [
567+
"--input",
568+
"input.txt"
569+
],
570+
"cwd": "${workspaceFolder:adventofcode}\\2023\\swift\\09",
571+
"name": "Release day09b (2023\\swift\\09)",
572+
"program": "${workspaceFolder:adventofcode}\\2023\\swift\\09\\.build\\release\\day09b.exe",
573+
"preLaunchTask": "swift: Build Release day09b (2023\\swift\\09)"
514574
}
515575
]
516576
}

2023/swift/09/.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/09/Package.resolved

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"originHash" : "1e67459e4c409e323022abc77f565007d3378175dddb13d8d98b7ae5df31b2b6",
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/09/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: "day09",
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: "day09",
16+
dependencies: [
17+
.product(name: "ArgumentParser", package: "swift-argument-parser")
18+
],
19+
path: "Sources/a"),
20+
.executableTarget(
21+
name: "day09b",
22+
dependencies: [
23+
.product(name: "ArgumentParser", package: "swift-argument-parser"),
24+
],
25+
path: "Sources/b")
26+
]
27+
)

2023/swift/09/Sources/a/main.swift

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import ArgumentParser
2+
import Foundation
3+
4+
@main
5+
struct Day09: ParsableCommand {
6+
@Option(help: "Specify the input")
7+
public var input: String
8+
9+
func numbers(_ fromString: String) -> [Int] {
10+
return fromString.components(separatedBy: " ").compactMap { Int($0) }
11+
}
12+
13+
func diff(_ sequence: [Int]) -> [[Int]] {
14+
var result = [[Int]]()
15+
result.append(sequence)
16+
var j = 0
17+
18+
while !result[j].allSatisfy({ $0 == 0 }) {
19+
j += 1
20+
result.append([])
21+
for i in 1..<result[j-1].count {
22+
result[j].append(result[j-1][i] - result[j-1][i-1])
23+
}
24+
}
25+
return result
26+
}
27+
28+
func next(_ sequences: [[Int]]) -> Int {
29+
var d = 0
30+
for sequence in sequences.reversed() {
31+
d = sequence.last! + d
32+
}
33+
return d
34+
}
35+
36+
37+
public func run() throws {
38+
guard let filecontent = try? String(contentsOfFile: input, encoding: .utf8)
39+
else {
40+
print("Failed to open file \(input)")
41+
return
42+
}
43+
44+
let lines = filecontent.components(separatedBy: .newlines)
45+
let sequences = lines.compactMap { numbers($0) }
46+
var result = 0
47+
for sequence in sequences {
48+
let diffs = diff(sequence)
49+
let n = next(diffs)
50+
print(n)
51+
result += n
52+
}
53+
print("Result is \(result)")
54+
}
55+
}

2023/swift/09/Sources/b/main.swift

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import ArgumentParser
2+
import Foundation
3+
4+
@main
5+
struct Day09b: ParsableCommand {
6+
@Option(help: "Specify the input")
7+
public var input: String
8+
9+
func numbers(_ fromString: String) -> [Int] {
10+
return fromString.components(separatedBy: " ").compactMap { Int($0) }
11+
}
12+
13+
func diff(_ sequence: [Int]) -> [[Int]] {
14+
var result = [[Int]]()
15+
result.append(sequence)
16+
var j = 0
17+
18+
while !result[j].allSatisfy({ $0 == 0 }) {
19+
j += 1
20+
result.append([])
21+
for i in 1..<result[j-1].count {
22+
result[j].append(result[j-1][i] - result[j-1][i-1])
23+
}
24+
}
25+
return result
26+
}
27+
28+
func next(_ sequences: [[Int]]) -> Int {
29+
var d = 0
30+
for sequence in sequences.reversed() {
31+
d = sequence.first! - d
32+
}
33+
return d
34+
}
35+
36+
37+
public func run() throws {
38+
guard let filecontent = try? String(contentsOfFile: input, encoding: .utf8)
39+
else {
40+
print("Failed to open file \(input)")
41+
return
42+
}
43+
44+
let lines = filecontent.components(separatedBy: .newlines)
45+
let sequences = lines.compactMap { numbers($0) }
46+
var result = 0
47+
for sequence in sequences {
48+
let diffs = diff(sequence)
49+
let n = next(diffs)
50+
print(n)
51+
result += n
52+
}
53+
print("Result is \(result)")
54+
}
55+
}

0 commit comments

Comments
 (0)