-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd14.go
140 lines (113 loc) · 2.53 KB
/
d14.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
package main
import (
"fmt"
"io/ioutil"
"math"
"os"
"regexp"
"strconv"
"strings"
)
var rex = regexp.MustCompile("(\\d+) (\\w+)")
type Rec struct {
name string
q int
}
type State struct {
Spent int
Remaining map[string]int
Missing []Rec
}
func calc1(recipies map[string][]Rec) int {
st := State{
0,
make(map[string]int),
recipies["FUEL"][:len(recipies["FUEL"])-1]}
for len(st.Missing) > 0 {
cur := st.Missing[0]
st.Missing = st.Missing[1:]
if st.Remaining[cur.name] >= cur.q {
st.Remaining[cur.name] -= cur.q
continue
}
cur.q -= st.Remaining[cur.name]
st.Remaining[cur.name] = 0
parts := recipies[cur.name]
unitq := parts[len(parts)-1].q
q := int(math.Ceil(float64(cur.q) / float64(unitq)))
for i := 0; i < len(parts)-1; i++ {
sub := parts[i]
if sub.name == "ORE" {
st.Spent += sub.q * q
} else {
st.Missing = append(
st.Missing,
Rec{sub.name, sub.q * q})
}
}
st.Remaining[cur.name] += (q * unitq) - cur.q
}
return st.Spent
}
func calc2(recipies map[string][]Rec, maxSpent int) int {
st := State{
0,
make(map[string]int),
make([]Rec, 0)}
cnt := 0
for {
lastSpent := st.Spent
for i := 0; i < len(recipies["FUEL"])-1; i++ {
st.Missing = append(st.Missing, recipies["FUEL"][i])
}
for len(st.Missing) > 0 {
cur := st.Missing[0]
st.Missing = st.Missing[1:]
if st.Remaining[cur.name] >= cur.q {
st.Remaining[cur.name] -= cur.q
continue
}
cur.q -= st.Remaining[cur.name]
st.Remaining[cur.name] = 0
parts := recipies[cur.name]
unitq := parts[len(parts)-1].q
q := int(math.Ceil(float64(cur.q) / float64(unitq)))
for i := 0; i < len(parts)-1; i++ {
sub := parts[i]
if sub.name == "ORE" {
st.Spent += sub.q * q
} else {
st.Missing = append(
st.Missing,
Rec{sub.name, sub.q * q})
}
}
st.Remaining[cur.name] += (q * unitq) - cur.q
}
if st.Spent > maxSpent {
return cnt
}
cnt += 1
}
}
func parse(lines []string) map[string][]Rec {
recs := make(map[string][]Rec)
for _, line := range lines {
arr := rex.FindAllStringSubmatch(line, -1)
row := make([]Rec, 0)
for i := 0; i < len(arr); i++ {
n, _ := strconv.Atoi(arr[i][1])
rec := Rec{arr[i][2], n}
row = append(row, rec)
}
recs[row[len(row)-1].name] = row
}
return recs
}
func main() {
raw, _ := ioutil.ReadFile(os.Args[1])
lines := strings.Split(strings.Trim(string(raw), "\n "), "\n")
recs := parse(lines)
fmt.Println("Result1:", calc1(recs))
fmt.Println("Result2:", calc2(recs, 1000000000000))
}