Skip to content

Commit 7c68a9e

Browse files
committed
fixup
1 parent f69d348 commit 7c68a9e

File tree

8 files changed

+259
-19
lines changed

8 files changed

+259
-19
lines changed

.vscode/launch.json

+60
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,66 @@
331331
"name": "Release day05 (2023\\swift\\05)",
332332
"program": "${workspaceFolder:adventofcode}\\2023\\swift\\05\\.build\\release\\day05.exe",
333333
"preLaunchTask": "swift: Build Release day05 (2023\\swift\\05)"
334+
},
335+
{
336+
"type": "swift-lldb",
337+
"request": "launch",
338+
"sourceLanguages": [
339+
"swift"
340+
],
341+
"args": [
342+
"--input",
343+
"input.txt"
344+
],
345+
"cwd": "${workspaceFolder:adventofcode}\\2023\\swift\\06",
346+
"name": "Debug day06 (2023\\swift\\06)",
347+
"program": "${workspaceFolder:adventofcode}\\2023\\swift\\06\\.build\\debug\\day06.exe",
348+
"preLaunchTask": "swift: Build Debug day06 (2023\\swift\\06)"
349+
},
350+
{
351+
"type": "swift-lldb",
352+
"request": "launch",
353+
"sourceLanguages": [
354+
"swift"
355+
],
356+
"args": [
357+
"--input",
358+
"input.txt"
359+
],
360+
"cwd": "${workspaceFolder:adventofcode}\\2023\\swift\\06",
361+
"name": "Release day06 (2023\\swift\\06)",
362+
"program": "${workspaceFolder:adventofcode}\\2023\\swift\\06\\.build\\release\\day06.exe",
363+
"preLaunchTask": "swift: Build Release day06 (2023\\swift\\06)"
364+
},
365+
{
366+
"type": "swift-lldb",
367+
"request": "launch",
368+
"sourceLanguages": [
369+
"swift"
370+
],
371+
"args": [
372+
"--input",
373+
"input.txt"
374+
],
375+
"cwd": "${workspaceFolder:adventofcode}\\2023\\swift\\06",
376+
"name": "Debug day06b (2023\\swift\\06)",
377+
"program": "${workspaceFolder:adventofcode}\\2023\\swift\\06\\.build\\debug\\day06b.exe",
378+
"preLaunchTask": "swift: Build Debug day06b (2023\\swift\\06)"
379+
},
380+
{
381+
"type": "swift-lldb",
382+
"request": "launch",
383+
"sourceLanguages": [
384+
"swift"
385+
],
386+
"args": [
387+
"--input",
388+
"input.txt"
389+
],
390+
"cwd": "${workspaceFolder:adventofcode}\\2023\\swift\\06",
391+
"name": "Release day06b (2023\\swift\\06)",
392+
"program": "${workspaceFolder:adventofcode}\\2023\\swift\\06\\.build\\release\\day06b.exe",
393+
"preLaunchTask": "swift: Build Release day06b (2023\\swift\\06)"
334394
}
335395
]
336396
}

2023/swift/05/Sources/b/main.swift

+76-19
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
import ArgumentParser
22
import Foundation
33

4-
struct Card {
5-
var count = 1
6-
var score = 0
4+
struct MapKey: Hashable {
5+
let first: String
6+
let second: String
7+
}
8+
9+
struct Seen : Hashable {
10+
let type: String
11+
let coord: Int
12+
}
13+
14+
struct MapEntry {
15+
let input: Range<Int>
16+
let output: Range<Int>
717
}
818

919
@main
10-
struct Day05b: ParsableCommand {
20+
struct Day05a: ParsableCommand {
1121
@Option(help: "Specify the input")
1222
public var input: String
1323

@@ -20,23 +30,70 @@ struct Day05b: ParsableCommand {
2030
print("Failed to open file \(input)")
2131
return
2232
}
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)
33+
34+
let lines = filecontent.components(separatedBy: .newlines)
35+
let s = numbers(fromString: lines[0].substring(from: 7))
36+
var seeds = [Range<Int>]()
37+
for i in 0 ..< s.count / 2 {
38+
seeds.append(s[i*2]..<s[i*2]+s[i*2+1])
3139
}
3240

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
41+
var mappings = [String: String]()
42+
var almanac = [MapKey: [MapEntry]]()
43+
44+
var key: MapKey?
45+
for idx in 2..<lines.count {
46+
if lines[idx] == "" {
47+
key = nil
48+
continue
49+
}
50+
51+
if key == nil {
52+
let split = lines[idx].components(separatedBy: ["-", " "])
53+
key = MapKey(first: split[0], second: split[2])
54+
mappings[split[0]] = split[2]
55+
almanac[key!] = []
56+
} else {
57+
let entry = numbers(fromString: lines[idx])
58+
almanac[key!]!.append(MapEntry(input: entry[1]..<entry[1]+entry[2], output: entry[0]..<entry[0]+entry[2]))
59+
}
60+
}
61+
62+
// print(seeds)
63+
// print(mappings)
64+
// print(almanac)
65+
66+
var result = Int.max
67+
var seen = [String: Set<Int>]()
68+
mappings.values.forEach({ seen[$0] = Set<Int>() })
69+
for seed in seeds {
70+
for i in seed {
71+
var what = i
72+
var first = "seed"
73+
var skip = false
74+
while let second = mappings[first] {
75+
// if !seen[second]!.insert(what).inserted {
76+
// skip = true
77+
// break
78+
// }
79+
if let entries = almanac[MapKey(first: first, second: second)] {
80+
for entry in entries {
81+
if entry.input.contains(what) {
82+
what = entry.output.lowerBound + (what - entry.input.lowerBound)
83+
break
84+
}
85+
}
86+
} else {
87+
assert(false)
88+
}
89+
first = second
90+
}
91+
// if (skip) {
92+
// continue
93+
// }
94+
if what < result {
95+
print("New best: \(what) from \(i)")
96+
result = what
4097
}
4198
}
4299
}

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

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

2023/swift/06/Sources/a/main.swift

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import ArgumentParser
2+
import Foundation
3+
4+
@main
5+
struct Day06a: 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+
public func run() throws {
14+
guard let filecontent = try? String(contentsOfFile: input, encoding: .utf8)
15+
else {
16+
print("Failed to open file \(input)")
17+
return
18+
}
19+
20+
let lines = filecontent.components(separatedBy: .newlines)
21+
let times = numbers(lines[0].components(separatedBy: ":")[1])
22+
let distances = numbers(lines[1].components(separatedBy: ":")[1])
23+
24+
var result = 1
25+
for i in 0..<times.count {
26+
let distance = distances[i]
27+
let time = times[i]
28+
// (t - x) * x > d
29+
// tx - x^2 > d
30+
// -x^2 + tx - d > 0
31+
32+
// delta = (t)^2 - 4d
33+
let delta = time * time - 4 * distance
34+
let x1 = Int(floor((Double(time) + sqrt(Double(delta))) / 2 - 0.01))
35+
let x2 = Int(ceil((Double(time) - sqrt(Double(delta))) / 2 + 0.01))
36+
print (x1, x2, x1-x2)
37+
result *= x1 - x2 + 1
38+
}
39+
print(result)
40+
}
41+
}

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

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import ArgumentParser
2+
import Foundation
3+
4+
@main
5+
struct Day06b: 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+
public func run() throws {
14+
guard let filecontent = try? String(contentsOfFile: input, encoding: .utf8)
15+
else {
16+
print("Failed to open file \(input)")
17+
return
18+
}
19+
20+
let lines = filecontent.components(separatedBy: .newlines)
21+
let time = Int(lines[0].components(separatedBy: ":")[1].replacingOccurrences(of: " ", with: ""))!
22+
let distance = Int(lines[1].components(separatedBy: ":")[1].replacingOccurrences(of: " ", with: ""))!
23+
24+
let delta = time * time - 4 * distance
25+
let x1 = Int(floor((Double(time) + sqrt(Double(delta))) / 2 - 0.01))
26+
let x2 = Int(ceil((Double(time) - sqrt(Double(delta))) / 2 + 0.01))
27+
print (x1, x2, x1-x2)
28+
print(x1 - x2 + 1)
29+
}
30+
}

2023/swift/06/input.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Time: 44 70 70 80
2+
Distance: 283 1134 1134 1491

0 commit comments

Comments
 (0)