-
Notifications
You must be signed in to change notification settings - Fork 0
/
day19.go
285 lines (248 loc) · 9.42 KB
/
day19.go
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package main
import (
"fmt"
"math"
"strconv"
"strings"
"sync"
)
func main() {
fmt.Println(part1(input1)) // 33
fmt.Println(part1(input2)) // 1306
fmt.Println(part2(input1)) // 2852
fmt.Println(part2(input2)) // 37604
}
func part1(input string) int {
return solver(input, 1)
}
func part2(input string) int {
return solver(input, 2)
}
func solver(input string, part int) int {
iterations := [2]int{24, 32}
var wg sync.WaitGroup
var mu sync.Mutex
var total int
blueprints := parse(input)
if part == 2 {
total = 1
if len(blueprints) > 3 {
blueprints = blueprints[:3]
}
}
wg.Add(len(blueprints))
rateLimit := make(chan bool, 10)
for i, recipes := range blueprints {
rateLimit <- true
go func(i int, recipes []recipe) {
defer func() {
<-rateLimit
}()
defer wg.Done()
maxOr := maxOre(recipes)
q := []state{newState()}
c := make(map[string]bool)
var bestScore int = math.MinInt
for t := 0; t < iterations[part-1]; t++ {
j := len(q)
fmt.Println(i, t, j, bestScore, q[len(q)-1])
for _, s := range q[:j] {
if s.minerals["geode"] > bestScore {
bestScore = s.minerals["geode"]
}
if s.minerals["geode"] < bestScore-1 {
continue
}
ns := s.next(recipes)
alreadyMax := false
for _, n := range ns {
if c[n.String()] {
continue
}
c[n.String()] = true
if n.minerals["ore"] >= maxOr {
alreadyMax = true
}
q = append(q, n)
}
if !alreadyMax {
if c[s.produce().String()] {
continue
}
c[s.produce().String()] = true
q = append(q, s.produce())
}
}
q = q[j:]
}
for _, s := range q {
if s.minerals["geode"] > bestScore {
bestScore = s.minerals["geode"]
}
}
if part == 1 {
qualityLevel := bestScore * (i + 1)
fmt.Println("quality level for", i+1, bestScore, qualityLevel)
mu.Lock()
total += qualityLevel
mu.Unlock()
} else {
fmt.Println("best score for", i+1, bestScore)
total *= bestScore
}
}(i, recipes)
}
wg.Wait()
return total
}
type state struct {
minerals map[string]int
robots map[string]int
}
func (s state) produce() state {
sc := s.clone()
for r, c := range sc.robots {
sc.minerals[r] += c
}
return sc
}
func (s state) clone() state {
return state{
minerals: copyMap(s.minerals),
robots: copyMap(s.robots),
}
}
func (s state) String() string {
return fmt.Sprintf("%v:%v", s.minerals, s.robots)
}
func newState() state {
minerals := make(map[string]int)
robots := make(map[string]int)
robots["ore"] = 1
return state{
minerals: minerals,
robots: robots,
}
}
func (s state) next(recipes []recipe) []state {
states := []state{}
for _, r := range recipes {
minerals, ok := r.build(s.minerals)
if ok {
sc := s.clone()
sc.minerals = minerals
sc = sc.produce()
sc.robots[r.output]++
states = append(states, sc)
}
}
return states
}
type ingredient struct {
robot string
cost int
}
type recipe struct {
input []ingredient
output string
}
func maxOre(recipes []recipe) int {
cost := math.MinInt
for _, r := range recipes {
for _, i := range r.input {
if i.robot == "ore" && i.cost > cost {
cost = i.cost
}
}
}
return cost
}
func (r recipe) build(m map[string]int) (map[string]int, bool) {
cm := copyMap(m)
for _, in := range r.input {
cm[in.robot] -= in.cost
if cm[in.robot] < 0 {
return m, false
}
}
return cm, true
}
func parse(input string) [][]recipe {
var res [][]recipe
rows := strings.Split(input, "\n")
for _, row := range rows {
parts := strings.Split(row, ":")
recipes := strings.Split(strings.TrimSpace(parts[1]), ".")
var out []recipe
for _, r := range recipes {
if r == "" {
continue
}
p := strings.Fields(strings.TrimSpace(r))
switch len(p) {
case 6:
rec := recipe{
output: p[1],
input: []ingredient{{p[5], toInt(p[4])}},
}
out = append(out, rec)
case 9:
rec := recipe{
output: p[1],
input: []ingredient{{p[5], toInt(p[4])}, {p[8], toInt(p[7])}},
}
out = append(out, rec)
default:
panic(r)
}
}
res = append(res, out)
}
return res
}
func toInt(s string) int {
n, err := strconv.Atoi(s)
if err != nil {
panic(err)
}
return n
}
func copyMap[K comparable, V any](m map[K]V) map[K]V {
res := make(map[K]V)
for k, v := range m {
res[k] = v
}
return res
}
var input1 = `Blueprint 1: Each ore robot costs 4 ore. Each clay robot costs 2 ore. Each obsidian robot costs 3 ore and 14 clay. Each geode robot costs 2 ore and 7 obsidian.
Blueprint 2: Each ore robot costs 2 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 8 clay. Each geode robot costs 3 ore and 12 obsidian.`
var input2 = `Blueprint 1: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 12 clay. Each geode robot costs 3 ore and 8 obsidian.
Blueprint 2: Each ore robot costs 2 ore. Each clay robot costs 2 ore. Each obsidian robot costs 2 ore and 15 clay. Each geode robot costs 2 ore and 7 obsidian.
Blueprint 3: Each ore robot costs 4 ore. Each clay robot costs 3 ore. Each obsidian robot costs 4 ore and 18 clay. Each geode robot costs 4 ore and 11 obsidian.
Blueprint 4: Each ore robot costs 2 ore. Each clay robot costs 2 ore. Each obsidian robot costs 2 ore and 10 clay. Each geode robot costs 2 ore and 11 obsidian.
Blueprint 5: Each ore robot costs 3 ore. Each clay robot costs 3 ore. Each obsidian robot costs 2 ore and 9 clay. Each geode robot costs 2 ore and 9 obsidian.
Blueprint 6: Each ore robot costs 3 ore. Each clay robot costs 3 ore. Each obsidian robot costs 2 ore and 12 clay. Each geode robot costs 2 ore and 10 obsidian.
Blueprint 7: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 10 clay. Each geode robot costs 2 ore and 7 obsidian.
Blueprint 8: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 2 ore and 10 clay. Each geode robot costs 3 ore and 14 obsidian.
Blueprint 9: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 17 clay. Each geode robot costs 3 ore and 8 obsidian.
Blueprint 10: Each ore robot costs 3 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 11 clay. Each geode robot costs 2 ore and 8 obsidian.
Blueprint 11: Each ore robot costs 4 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 20 clay. Each geode robot costs 2 ore and 19 obsidian.
Blueprint 12: Each ore robot costs 3 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 20 clay. Each geode robot costs 2 ore and 12 obsidian.
Blueprint 13: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 6 clay. Each geode robot costs 2 ore and 20 obsidian.
Blueprint 14: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 5 clay. Each geode robot costs 3 ore and 18 obsidian.
Blueprint 15: Each ore robot costs 3 ore. Each clay robot costs 3 ore. Each obsidian robot costs 4 ore and 19 clay. Each geode robot costs 4 ore and 7 obsidian.
Blueprint 16: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 19 clay. Each geode robot costs 4 ore and 11 obsidian.
Blueprint 17: Each ore robot costs 2 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 20 clay. Each geode robot costs 2 ore and 16 obsidian.
Blueprint 18: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 18 clay. Each geode robot costs 3 ore and 8 obsidian.
Blueprint 19: Each ore robot costs 3 ore. Each clay robot costs 3 ore. Each obsidian robot costs 2 ore and 14 clay. Each geode robot costs 3 ore and 17 obsidian.
Blueprint 20: Each ore robot costs 2 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 11 clay. Each geode robot costs 3 ore and 14 obsidian.
Blueprint 21: Each ore robot costs 3 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 6 clay. Each geode robot costs 2 ore and 16 obsidian.
Blueprint 22: Each ore robot costs 2 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 20 clay. Each geode robot costs 3 ore and 14 obsidian.
Blueprint 23: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 10 clay. Each geode robot costs 2 ore and 14 obsidian.
Blueprint 24: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 2 ore and 7 clay. Each geode robot costs 4 ore and 13 obsidian.
Blueprint 25: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 18 clay. Each geode robot costs 4 ore and 12 obsidian.
Blueprint 26: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 11 clay. Each geode robot costs 4 ore and 12 obsidian.
Blueprint 27: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 9 clay. Each geode robot costs 4 ore and 16 obsidian.
Blueprint 28: Each ore robot costs 4 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 7 clay. Each geode robot costs 2 ore and 7 obsidian.
Blueprint 29: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 2 ore and 14 clay. Each geode robot costs 4 ore and 19 obsidian.
Blueprint 30: Each ore robot costs 4 ore. Each clay robot costs 3 ore. Each obsidian robot costs 4 ore and 20 clay. Each geode robot costs 2 ore and 15 obsidian.`