-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
311 lines (266 loc) · 6.35 KB
/
main.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package main
import (
"bufio"
"fmt"
"io"
"strconv"
"strings"
"time"
)
func fs1(input io.Reader, minute int) (int, error) {
scanner := bufio.NewScanner(input)
sum := 0
for scanner.Scan() {
line := scanner.Text()
blueprint, err := toBlueprints(line)
if err != nil {
return 0, err
}
cache = make(map[int]map[State]int, minute)
d := duration()
v := best(&blueprint, &State{nbOreRobot: 1}, minute)
fmt.Println(blueprint.id, v, d())
sum += blueprint.id * v
}
return sum, nil
}
func duration() func() time.Duration {
start := time.Now()
return func() time.Duration {
return time.Since(start)
}
}
var cache map[int]map[State]int
func getCache(left int, state *State) (int, bool) {
v, exists := cache[left]
if !exists {
return 0, false
}
v2, exists := v[*state]
return v2, exists
}
func addCache(left int, state *State, best int) {
v, exists := cache[left]
if !exists {
v = make(map[State]int)
cache[left] = v
}
v[*state] = best
}
func best(blueprint *Blueprint, state *State, left int) int {
if left == 0 {
return state.nbGeode
}
if v, exists := getCache(left, state); exists {
return v
}
v := 0
newState, canBuild := newGeodeRobot(blueprint, state)
if canBuild {
s := newState.combineGeode()
v = best(blueprint, &s, left-1)
addCache(left, state, v)
// If we can build a geode robot now, we should build it
return v
}
newState, canBuild = newObsidianRobot(blueprint, state)
if canBuild {
s := newState.combineObsidian()
v = max(v, best(blueprint, &s, left-1))
if left >= 5 {
addCache(left, state, v)
return v
}
}
newState, canBuild = newClayRobot(blueprint, state)
if canBuild {
s := newState.combineClay()
v = max(v, best(blueprint, &s, left-1))
}
newState, canBuild = newOreRobot(blueprint, state)
if canBuild {
s := newState.combineOre()
v = max(v, best(blueprint, &s, left-1))
}
s := state.combine()
v = max(v, best(blueprint, &s, left-1))
addCache(left, state, v)
return v
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func newOreRobot(blueprint *Blueprint, state *State) (State, bool) {
if blueprint.oreOreCost <= state.nbOre {
s := *state
s.nbOre -= blueprint.oreOreCost
s.nbOreRobot++
return s, true
}
return State{}, false
}
func newClayRobot(blueprint *Blueprint, state *State) (State, bool) {
if blueprint.clayOreCost <= state.nbOre {
s := *state
s.nbOre -= blueprint.clayOreCost
s.nbClayRobot++
return s, true
}
return State{}, false
}
func newObsidianRobot(blueprint *Blueprint, state *State) (State, bool) {
if blueprint.obsidianOreCost <= state.nbOre &&
blueprint.obsidianClayCost <= state.nbClay {
s := *state
s.nbOre -= blueprint.obsidianOreCost
s.nbClay -= blueprint.obsidianClayCost
s.nbObsidianRobot++
return s, true
}
return State{}, false
}
func newGeodeRobot(blueprint *Blueprint, state *State) (State, bool) {
if blueprint.geodeOreCost <= state.nbOre &&
blueprint.geodeObsidianCost <= state.nbObsidian {
s := *state
s.nbOre -= blueprint.geodeOreCost
s.nbObsidian -= blueprint.geodeObsidianCost
s.nbGeodeRobot++
return s, true
}
return State{}, false
}
func collect(state State) State {
state.nbOre = state.nbOreRobot
state.nbClay = state.nbClayRobot
state.nbObsidian = state.nbObsidianRobot
state.nbGeode = state.nbGeodeRobot
return state
}
type State struct {
nbOre int
nbClay int
nbObsidian int
nbGeode int
nbOreRobot int
nbClayRobot int
nbObsidianRobot int
nbGeodeRobot int
}
func (s State) combine() State {
s.nbOre += s.nbOreRobot
s.nbClay += s.nbClayRobot
s.nbObsidian += s.nbObsidianRobot
s.nbGeode += s.nbGeodeRobot
return s
}
func (s State) combineOre() State {
s.nbOre += s.nbOreRobot - 1
s.nbClay += s.nbClayRobot
s.nbObsidian += s.nbObsidianRobot
s.nbGeode += s.nbGeodeRobot
return s
}
func (s State) combineClay() State {
s.nbOre += s.nbOreRobot
s.nbClay += s.nbClayRobot - 1
s.nbObsidian += s.nbObsidianRobot
s.nbGeode += s.nbGeodeRobot
return s
}
func (s State) combineObsidian() State {
s.nbOre += s.nbOreRobot
s.nbClay += s.nbClayRobot
s.nbObsidian += s.nbObsidianRobot - 1
s.nbGeode += s.nbGeodeRobot
return s
}
func (s State) combineGeode() State {
s.nbOre += s.nbOreRobot
s.nbClay += s.nbClayRobot
s.nbObsidian += s.nbObsidianRobot
s.nbGeode += s.nbGeodeRobot - 1
return s
}
type Blueprint struct {
id int
oreOreCost int
clayOreCost int
obsidianOreCost int
obsidianClayCost int
geodeOreCost int
geodeObsidianCost int
}
func toBlueprints(s string) (Blueprint, error) {
start := 10
end := strings.Index(s, ":")
id, err := strconv.Atoi(s[start:end])
if err != nil {
return Blueprint{}, err
}
oreOreCost, err := getCost(s, "Each ore robot costs ")
if err != nil {
return Blueprint{}, err
}
clayOreCost, err := getCost(s, "Each clay robot costs ")
if err != nil {
return Blueprint{}, err
}
obsidianOreCost, err := getCost(s, "Each obsidian robot costs ")
if err != nil {
return Blueprint{}, err
}
obsidianClayCost, err := getCost(s, " ore and ")
if err != nil {
return Blueprint{}, err
}
geodeOreCost, err := getCost(s, "Each geode robot costs ")
if err != nil {
return Blueprint{}, err
}
start = strings.Index(s, "Each geode robot costs ")
start = strings.Index(s[start:], "ore and ") + start + len("ore and ")
end = strings.Index(s[start:], " ")
geodeObsidianCost, err := strconv.Atoi(s[start : start+end])
if err != nil {
return Blueprint{}, err
}
return Blueprint{
id: id,
oreOreCost: oreOreCost,
clayOreCost: clayOreCost,
obsidianOreCost: obsidianOreCost,
obsidianClayCost: obsidianClayCost,
geodeOreCost: geodeOreCost,
geodeObsidianCost: geodeObsidianCost,
}, nil
}
func getCost(s string, sep string) (int, error) {
start := strings.Index(s, sep)
end := strings.Index(s[start+len(sep):], " ")
return strconv.Atoi(s[start+len(sep) : end+start+len(sep)])
}
func fs2(input io.Reader, minute int, remaining int) (int, error) {
scanner := bufio.NewScanner(input)
sum := 1
for scanner.Scan() {
if remaining == 0 {
return sum, nil
}
remaining--
line := scanner.Text()
blueprint, err := toBlueprints(line)
if err != nil {
return 0, err
}
cache = make(map[int]map[State]int, minute)
d := duration()
v := best(&blueprint, &State{nbOreRobot: 1}, minute)
fmt.Println(blueprint.id, v, d())
sum *= v
}
return sum, nil
}