-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
226 lines (194 loc) · 3.77 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
package main
import (
"bufio"
"fmt"
"io"
"strings"
lib "github.com/teivah/advent-of-code"
)
func fs1(input io.Reader) int {
scanner := bufio.NewScanner(input)
transformations := make(map[string]Transformation)
for scanner.Scan() {
t := toTransformation(scanner.Text())
transformations[t.to.name] = t
}
v := min("FUEL", 1, transformations)
return v
}
func min(name string, count int, transformations map[string]Transformation) int {
inDegree := make(map[string]int)
for k, t := range transformations {
inDegree[k] = 0
for _, v := range t.from {
inDegree[v.name] = 0
}
}
for _, t := range transformations {
for _, v := range t.from {
inDegree[v.name]++
}
}
var q []string
for k, v := range inDegree {
if v == 0 {
q = append(q, k)
}
}
level := 0
levels := make(map[string]int)
for len(q) != 0 {
v := len(q)
for i := 0; i < v; i++ {
s := q[0]
q = q[1:]
levels[s] = level
for _, unit := range transformations[s].from {
inDegree[unit.name]--
if inDegree[unit.name] == 0 {
q = append(q, unit.name)
}
}
}
level++
}
m := needed(name, count, transformations)
level = 1
for {
m2 := make(map[string]int)
for name, count := range m {
if levels[name] != level {
m2[name] += count
continue
}
m3 := needed(name, count, transformations)
for k, v := range m3 {
m2[k] += v
}
}
m = m2
if len(m) == 1 {
if v, exists := m["ORE"]; exists {
return v
}
}
level++
}
}
func needed(name string, count int, transformations map[string]Transformation) map[string]int {
t := transformations[name]
n := 1
for ; ; n++ {
if n*t.to.count >= count {
break
}
}
sub := make(map[string]int)
for _, unit := range t.from {
sub[unit.name] += unit.count * n
}
return sub
}
func cpy(remaining map[string]Unit) map[string]Unit {
res := make(map[string]Unit, len(remaining))
for k, v := range remaining {
res[k] = v
}
return res
}
func add(remaining map[string]Unit, count int, name string) {
unit, exists := remaining[name]
if !exists {
remaining[name] = Unit{
name: name,
count: count,
}
return
}
unit.count += count
remaining[name] = unit
}
func del(remaining map[string]Unit, count int, name string) {
unit := remaining[name]
if unit.count == count {
delete(remaining, name)
return
}
unit.count -= count
remaining[name] = unit
}
func toTransformation(s string) Transformation {
s = strings.TrimSpace(s)
start := 0
var from []Unit
for {
if s[start] == '=' {
break
}
i := start + 1
for ; ; i++ {
r := rune(s[i])
if !isDigit(r) {
break
}
}
count := lib.StringToInt(s[start:i])
start = i + 1
i = start + 1
for ; ; i++ {
r := rune(s[i])
if !isLetter(r) {
break
}
}
name := s[start:i]
from = append(from, Unit{
name: name,
count: count,
})
start = i
if s[start] == ',' {
start = i + 2
} else {
start = i + 4
break
}
}
del := lib.NewDelimiter(s[start:], " ")
return Transformation{
from: from,
to: Unit{
count: del.GetInt(0),
name: del.GetString(1),
},
}
}
func isDigit(r rune) bool {
return r >= '0' && r <= '9'
}
func isLetter(r rune) bool {
return r >= 'A' && r <= 'Z'
}
type Transformation struct {
from []Unit
to Unit
}
type Unit struct {
name string
count int
}
func (u Unit) String() string {
return fmt.Sprintf("%s:%v", u.name, u.count)
}
func fs2(input io.Reader) int {
scanner := bufio.NewScanner(input)
transformations := make(map[string]Transformation)
for scanner.Scan() {
t := toTransformation(scanner.Text())
transformations[t.to.name] = t
}
const v = 1_000_000_000_000
one := min("FUEL", 1, transformations)
sec := min("FUEL", v/one, transformations)
return int((float64(1000000000000) / float64(one)) * (float64(1000000000000) / float64(sec)))
}