|
@@ -44,36 +44,33 @@ func parseInput(file string) recipeBook {
|
44
|
44
|
return recipes
|
45
|
45
|
}
|
46
|
46
|
|
47
|
|
-func (rb recipeBook) produce(target *amount, used map[chemical]int, spare map[chemical]int) {
|
48
|
|
- recipe := rb[target.chemical]
|
49
|
|
- needed := target.quantity
|
|
47
|
+func (rb recipeBook) produce(target chemical, quantity int, used map[chemical]int, spare map[chemical]int) {
|
|
48
|
+ recipe := rb[target]
|
|
49
|
+ needed := quantity
|
50
|
50
|
|
51
|
51
|
if recipe == nil {
|
52
|
|
- used[target.chemical] += needed
|
|
52
|
+ used[target] += needed
|
53
|
53
|
return
|
54
|
54
|
}
|
55
|
55
|
|
56
|
|
- free := spare[target.chemical]
|
|
56
|
+ free := spare[target]
|
57
|
57
|
if free >= needed {
|
58
|
|
- used[target.chemical] += needed
|
59
|
|
- spare[target.chemical] -= needed
|
|
58
|
+ used[target] += needed
|
|
59
|
+ spare[target] -= needed
|
60
|
60
|
return
|
61
|
61
|
} else {
|
62
|
|
- used[target.chemical] += free
|
63
|
|
- spare[target.chemical] = 0
|
|
62
|
+ used[target] += free
|
|
63
|
+ spare[target] = 0
|
64
|
64
|
needed -= free
|
65
|
65
|
}
|
66
|
66
|
|
67
|
67
|
runs := int(math.Ceil(float64(needed) / float64(recipe.produces.quantity)))
|
68
|
68
|
for _, i := range recipe.requires {
|
69
|
|
- rb.produce(&amount{
|
70
|
|
- chemical: i.chemical,
|
71
|
|
- quantity: i.quantity * runs,
|
72
|
|
- }, used, spare)
|
|
69
|
+ rb.produce(i.chemical, i.quantity*runs, used, spare)
|
73
|
70
|
}
|
74
|
71
|
|
75
|
|
- used[target.chemical] += needed
|
76
|
|
- spare[target.chemical] += (recipe.produces.quantity * runs) - needed
|
|
72
|
+ used[target] += needed
|
|
73
|
+ spare[target] += (recipe.produces.quantity * runs) - needed
|
77
|
74
|
}
|
78
|
75
|
|
79
|
76
|
func main() {
|
|
@@ -81,10 +78,7 @@ func main() {
|
81
|
78
|
used := make(map[chemical]int, len(recipes))
|
82
|
79
|
spare := make(map[chemical]int, len(recipes))
|
83
|
80
|
|
84
|
|
- recipes.produce(&amount{
|
85
|
|
- chemical: "FUEL",
|
86
|
|
- quantity: 1,
|
87
|
|
- }, used, spare)
|
|
81
|
+ recipes.produce("FUEL", 1, used, spare)
|
88
|
82
|
|
89
|
83
|
orePerFuel := used["ORE"]
|
90
|
84
|
println(used["ORE"])
|
|
@@ -92,10 +86,7 @@ func main() {
|
92
|
86
|
last := 0
|
93
|
87
|
for used["ORE"] < 1000000000000 {
|
94
|
88
|
last = used["FUEL"]
|
95
|
|
- recipes.produce(&amount{
|
96
|
|
- chemical: "FUEL",
|
97
|
|
- quantity: common.Max(1, (1000000000000-used["ORE"])/orePerFuel),
|
98
|
|
- }, used, spare)
|
|
89
|
+ recipes.produce("FUEL", common.Max(1, (1000000000000-used["ORE"])/orePerFuel), used, spare)
|
99
|
90
|
}
|
100
|
91
|
|
101
|
92
|
println(last)
|