-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDay19.swift
More file actions
175 lines (145 loc) · 3.88 KB
/
Copy pathDay19.swift
File metadata and controls
175 lines (145 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import Algorithms
import Collections
import Foundation
import RegexBuilder
fileprivate extension StringProtocol {
var integers: [Int] {
return self
.split{ "-0123456789".contains($0) == false }
.map { Int($0)! }
}
}
fileprivate enum Rock: String, CaseIterable, Hashable {
case geode, clay, obsidian, ore
}
fileprivate struct RockQuantity: Hashable {
var rock: Rock
var quantity: Int
}
fileprivate struct Blueprint: Hashable {
var index: Int
var costs: [Rock: Set<RockQuantity>] = [:]
}
fileprivate struct Game: Hashable {
var timestamp = 0
var ores: [Rock: Int] = [:]
var robots: [Rock: Int] = [.ore: 1]
func clamped(to requirements: [Rock: Int]) -> Game {
var copy = self
ores.forEach { rock, quantity in
copy.ores[rock] = min(quantity, requirements[rock]! + 1)
}
return copy
}
}
struct Day19: Day {
var input: String
var debugMode = false
init(input: String) {
self.input = input
}
func partOne() -> String {
input
.split(separator: "\n")
.map(parseBlueprint)
.map { $0.index * mostGeodes(from: $0, steps: 24) }
.reduce(0, +)
.description
}
func partTwo() -> String {
input
.split(separator: "\n")
.map(parseBlueprint)
.prefix(3)
.map { mostGeodes(from: $0, steps: 32) }
.reduce(1, *)
.description
}
fileprivate func parseBlueprint(_ line: some StringProtocol) -> Blueprint {
let index = line.integers.first!
var blueprint = Blueprint(index: index)
for term in line.components(separatedBy: "Each ").dropFirst() {
let words = term.split(separator: " ")
let rock = Rock(rawValue: String(words.first!))!
var quantity: Int? = 0
for word in words.dropFirst(3) {
if let number = Int(word) {
quantity = number
} else if let q = quantity {
blueprint.costs[rock, default: []].insert(
RockQuantity(
rock: Rock(rawValue: String(word.trimmingCharacters(in: .punctuationCharacters)))!,
quantity: q
)
)
quantity = nil
}
}
}
return blueprint
}
fileprivate func mostGeodes(from blueprint: Blueprint, steps: Int) -> Int {
if debugMode {
print("Blueprint", blueprint.index)
}
var maxNeeded = blueprint.costs.reduce(into: [Rock: Int]()) { dict, tuple in
for needed in tuple.value {
dict[needed.rock] = max(needed.quantity, dict[needed.rock, default: 0])
}
}
maxNeeded[.geode] = 20000
var maxGeodes = 0 {
didSet {
if debugMode, maxGeodes != oldValue {
print("New max geodes:", maxGeodes)
}
}
}
var queue = Deque([Game()])
var visited: Set<Game> = []
while let game = queue.popFirst() {
if visited.insert(game).inserted == false {
continue
}
let remainingTime = steps - game.timestamp
let maxPotential = game.ores[.geode, default: 0]
+ game.robots[.ore, default: 0] * remainingTime
+ ((remainingTime * remainingTime) / 2)
if maxPotential < maxGeodes {
continue
}
if let geodes = game.ores[.geode] {
maxGeodes = max(maxGeodes, geodes)
}
if game.timestamp == steps {
continue
}
for robotToBuild in Rock.allCases {
let cost = blueprint.costs[robotToBuild]!
let canBuild = cost.allSatisfy { rq in
game.ores[rq.rock, default: 0] >= rq.quantity
}
let isNeeded = game.robots[robotToBuild, default: 0] < maxNeeded[robotToBuild]!
guard canBuild, isNeeded else { continue }
var newGame = game
newGame.timestamp += 1
for rq in cost {
newGame.ores[rq.rock]! -= rq.quantity
}
newGame.robots.forEach { rock, quantity in
newGame.ores[rock, default: 0] += quantity
}
newGame.robots[robotToBuild, default: 0] += 1
queue.append(newGame)
}
var newGame = game
newGame.timestamp += 1
newGame.robots.forEach { rock, quantity in
newGame.ores[rock, default: 0] += quantity
}
newGame = newGame.clamped(to: maxNeeded)
queue.append(newGame)
}
return maxGeodes
}
}