Skip to content

Commit 4be7cd4

Browse files
committed
Solved 2023 day 11 part 2 in Swift
1 parent f36e47e commit 4be7cd4

File tree

8 files changed

+378
-0
lines changed

8 files changed

+378
-0
lines changed

.vscode/launch.json

+60
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,66 @@
631631
"name": "Release day10b (2023\\swift\\10)",
632632
"program": "${workspaceFolder:adventofcode}\\2023\\swift\\10\\.build\\release\\day10b.exe",
633633
"preLaunchTask": "swift: Build Release day10b (2023\\swift\\10)"
634+
},
635+
{
636+
"type": "swift-lldb",
637+
"request": "launch",
638+
"sourceLanguages": [
639+
"swift"
640+
],
641+
"args": [
642+
"--input",
643+
"input.txt"
644+
],
645+
"cwd": "${workspaceFolder:adventofcode}\\2023\\swift\\11",
646+
"name": "Debug day11 (2023\\swift\\11)",
647+
"program": "${workspaceFolder:adventofcode}\\2023\\swift\\11\\.build\\debug\\day11.exe",
648+
"preLaunchTask": "swift: Build Debug day11 (2023\\swift\\11)"
649+
},
650+
{
651+
"type": "swift-lldb",
652+
"request": "launch",
653+
"sourceLanguages": [
654+
"swift"
655+
],
656+
"args": [
657+
"--input",
658+
"input.txt"
659+
],
660+
"cwd": "${workspaceFolder:adventofcode}\\2023\\swift\\11",
661+
"name": "Release day11 (2023\\swift\\11)",
662+
"program": "${workspaceFolder:adventofcode}\\2023\\swift\\11\\.build\\release\\day11.exe",
663+
"preLaunchTask": "swift: Build Release day11 (2023\\swift\\11)"
664+
},
665+
{
666+
"type": "swift-lldb",
667+
"request": "launch",
668+
"sourceLanguages": [
669+
"swift"
670+
],
671+
"args": [
672+
"--input",
673+
"input.txt"
674+
],
675+
"cwd": "${workspaceFolder:adventofcode}\\2023\\swift\\11",
676+
"name": "Debug day11b (2023\\swift\\11)",
677+
"program": "${workspaceFolder:adventofcode}\\2023\\swift\\11\\.build\\debug\\day11b.exe",
678+
"preLaunchTask": "swift: Build Debug day11b (2023\\swift\\11)"
679+
},
680+
{
681+
"type": "swift-lldb",
682+
"request": "launch",
683+
"sourceLanguages": [
684+
"swift"
685+
],
686+
"args": [
687+
"--input",
688+
"input.txt"
689+
],
690+
"cwd": "${workspaceFolder:adventofcode}\\2023\\swift\\11",
691+
"name": "Release day11b (2023\\swift\\11)",
692+
"program": "${workspaceFolder:adventofcode}\\2023\\swift\\11\\.build\\release\\day11b.exe",
693+
"preLaunchTask": "swift: Build Release day11b (2023\\swift\\11)"
634694
}
635695
]
636696
}

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

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

2023/swift/11/Sources/a/main.swift

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import ArgumentParser
2+
import Foundation
3+
4+
@main
5+
struct Day11: ParsableCommand {
6+
@Option(help: "Specify the input")
7+
public var input: String
8+
9+
public func run() throws {
10+
guard let filecontent = try? String(contentsOfFile: input, encoding: .utf8)
11+
else {
12+
print("Failed to open file \(input)")
13+
return
14+
}
15+
16+
let lines = filecontent.components(separatedBy: .newlines)
17+
var stars = [(Int, Int)]()
18+
for (y, line) in lines.enumerated() {
19+
for (x, element) in line.enumerated() {
20+
if element == "#" {
21+
stars.append((x, y))
22+
}
23+
}
24+
}
25+
var horizontal = Set<Int>()
26+
Set<Int>(0..<lines.count).symmetricDifference(Set<Int>(stars.map { $0.1 })).forEach { horizontal.insert($0) }
27+
28+
var vertical = Set<Int>()
29+
Set<Int>(0..<lines[0].count)
30+
.symmetricDifference(Set<Int>(stars.map { $0.0 })).forEach { vertical.insert($0) }
31+
32+
var result = 0
33+
for i in 0..<stars.count - 1 {
34+
for j in i+1..<stars.count {
35+
var result2 = 0
36+
let star1 = stars[i]
37+
let star2 = stars[j]
38+
for x in min(star1.0, star2.0)..<max(star1.0, star2.0) {
39+
result2 += 1
40+
if vertical.contains(x) {
41+
result2 += 1
42+
}
43+
}
44+
45+
for y in min(star1.1, star2.1)..<max(star1.1, star2.1) {
46+
result2 += 1
47+
if horizontal.contains(y) {
48+
result2 += 1
49+
}
50+
}
51+
52+
result += result2
53+
print("\(i) \(j) \(result2)")
54+
}
55+
}
56+
57+
print(result)
58+
}
59+
}

2023/swift/11/Sources/b/main.swift

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import ArgumentParser
2+
import Foundation
3+
4+
@main
5+
struct Day11: ParsableCommand {
6+
@Option(help: "Specify the input")
7+
public var input: String
8+
9+
public func run() throws {
10+
guard let filecontent = try? String(contentsOfFile: input, encoding: .utf8)
11+
else {
12+
print("Failed to open file \(input)")
13+
return
14+
}
15+
16+
let lines = filecontent.components(separatedBy: .newlines)
17+
var stars = [(Int, Int)]()
18+
for (y, line) in lines.enumerated() {
19+
for (x, element) in line.enumerated() {
20+
if element == "#" {
21+
stars.append((x, y))
22+
}
23+
}
24+
}
25+
var horizontal = Set<Int>()
26+
Set<Int>(0..<lines.count).symmetricDifference(Set<Int>(stars.map { $0.1 })).forEach { horizontal.insert($0) }
27+
28+
var vertical = Set<Int>()
29+
Set<Int>(0..<lines[0].count)
30+
.symmetricDifference(Set<Int>(stars.map { $0.0 })).forEach { vertical.insert($0) }
31+
32+
var result = 0
33+
for i in 0..<stars.count - 1 {
34+
for j in i+1..<stars.count {
35+
var result2 = 0
36+
let star1 = stars[i]
37+
let star2 = stars[j]
38+
for x in min(star1.0, star2.0)..<max(star1.0, star2.0) {
39+
result2 += 1
40+
if vertical.contains(x) {
41+
result2 += 999999
42+
}
43+
}
44+
45+
for y in min(star1.1, star2.1)..<max(star1.1, star2.1) {
46+
result2 += 1
47+
if horizontal.contains(y) {
48+
result2 += 999999
49+
}
50+
}
51+
52+
result += result2
53+
// print("\(i) \(j) \(result2)")
54+
}
55+
}
56+
57+
print(result)
58+
}
59+
}

0 commit comments

Comments
 (0)