-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
225 lines (208 loc) · 4.48 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
package main
import (
"fmt"
"sort"
"strconv"
"strings"
)
func main() {
part1()
part2()
}
func part1() {
type chem struct {
count int
name string
}
type reac struct {
in []chem
out chem
}
parsec := func(s string) chem {
ps := strings.Split(s, " ")
v, _ := strconv.ParseInt(ps[0], 10, 64)
return chem{
count: int(v),
name: ps[1],
}
}
lines := strings.Split(input, "\n")
rs := map[string]reac{}
for _, line := range lines {
ps := strings.Split(line, " => ")
ins := strings.Split(ps[0], ", ")
in := make([]chem, len(ins))
for i, s := range ins {
in[i] = parsec(s)
}
out := parsec(ps[1])
rs[out.name] = reac{
in: in,
out: out,
}
}
depths := map[string]int{}
var search func(string, int)
search = func(name string, depth int) {
v, ok := depths[name]
if !ok || depth > v {
depths[name] = depth
}
if name == "ORE" {
return
}
r := rs[name]
for _, in := range r.in {
search(in.name, depth+1)
}
}
search("FUEL", 0)
var names []string
for k := range rs {
names = append(names, k)
}
sort.Slice(names, func(i, j int) bool {
return depths[names[i]] < depths[names[j]]
})
counts := map[string]int{"FUEL": 1}
for _, name := range names {
r := rs[name]
rcount := (counts[name] + r.out.count - 1) / r.out.count
for _, in := range r.in {
counts[in.name] += in.count * rcount
}
}
fmt.Println(counts["ORE"])
}
func part2() {
type chem struct {
count int
name string
}
type reac struct {
in []chem
out chem
}
parsec := func(s string) chem {
ps := strings.Split(s, " ")
v, _ := strconv.ParseInt(ps[0], 10, 64)
return chem{
count: int(v),
name: ps[1],
}
}
lines := strings.Split(input, "\n")
rs := map[string]reac{}
for _, line := range lines {
ps := strings.Split(line, " => ")
ins := strings.Split(ps[0], ", ")
in := make([]chem, len(ins))
for i, s := range ins {
in[i] = parsec(s)
}
out := parsec(ps[1])
rs[out.name] = reac{
in: in,
out: out,
}
}
depths := map[string]int{}
var search func(string, int)
search = func(name string, depth int) {
v, ok := depths[name]
if !ok || depth > v {
depths[name] = depth
}
if name == "ORE" {
return
}
r := rs[name]
for _, in := range r.in {
search(in.name, depth+1)
}
}
search("FUEL", 0)
var names []string
for k := range rs {
names = append(names, k)
}
sort.Slice(names, func(i, j int) bool {
return depths[names[i]] < depths[names[j]]
})
find := func(fuel int) int {
counts := map[string]int{"FUEL": fuel}
for _, name := range names {
r := rs[name]
rcount := (counts[name] + r.out.count - 1) / r.out.count
for _, in := range r.in {
counts[in.name] += in.count * rcount
}
}
return counts["ORE"]
}
max := sort.Search(1000000000000, func(i int) bool {
return find(i) >= 1000000000000
})
if find(max) > 1000000000000 {
max--
}
fmt.Println(max, find(max))
}
const input = `4 JWXL => 8 SNBF
23 MPZQF, 10 TXVW, 8 JWXL => 6 DXLB
1 SNZDR, 5 XMWHC, 1 NJSC => 7 MHSB
2 TDHD, 11 TXVW => 4 RFNZ
2 VRCD, 1 FGZG, 3 JWXL, 1 HQTL, 2 MPZQF, 1 GTPJ, 5 HQNMK, 10 CQZQ => 9 QMTZB
3 SRDB, 2 ZMVLP => 3 DHFD
1 DFQGF => 1 CVXJR
193 ORE => 3 TRWXF
23 MFJMS, 4 HJXJH => 1 WVDF
5 TRWXF =>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