-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
319 lines (276 loc) · 6.23 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
312
313
314
315
316
317
318
319
package main
import (
"fmt"
"io"
"os"
"strconv"
"strings"
"github.com/devries/advent_of_code_2022/utils"
"github.com/spf13/pflag"
)
const inputfile = "../inputs/day21.txt"
func main() {
pflag.Parse()
f, err := os.Open(inputfile)
utils.Check(err, "error opening input")
defer f.Close()
r := solve(f)
fmt.Println(r)
}
func solve(r io.Reader) int64 {
lines := utils.ReadLines(r)
monkeys := make(map[string]Job)
for _, ln := range lines {
name, j := parseLine(ln)
monkeys[name] = j
}
monkeys["humn"] = Job{"x", "", ""}
equality := monkeys["root"]
listA := rpnize(equality.A, monkeys)
listB := rpnize(equality.B, monkeys)
var listVar []string
var listPlain []string
// Find which list contains the variable
found := false
for _, v := range listA {
if v == "x" {
listVar = listA
listPlain = listB
found = true
break
}
}
if !found {
listVar = listB
listPlain = listA
}
// Calculate the right side of the equality
s := doCalculation(listPlain)
// Split the other side into pre and post variable
pre, post := splitAroundVariable(listVar)
// Calculate what comes before the variable and grab the stack
pcalc := doCalculation(pre)
// Condense the post variable RPN list by doing any possible portions
// of the calculation
condy := condense(post)
complete := undoCalculation(condy, pcalc, s[0])
return complete[0]
}
type Job struct {
A string
B string
Operation string
}
func parseLine(ln string) (string, Job) {
components := strings.Split(ln, ": ")
jobParts := strings.Fields(components[1])
switch len(jobParts) {
case 1:
job := Job{jobParts[0], "", ""}
return components[0], job
default:
job := Job{jobParts[0], jobParts[2], jobParts[1]}
return components[0], job
}
}
type IntStack []int64
func (s *IntStack) Pop() int64 {
idx := len(*s) - 1
r := (*s)[idx]
*s = (*s)[:idx]
return r
}
func (s *IntStack) Push(n int64) {
*s = append(*s, n)
}
func rpnize(root string, monkeys map[string]Job) []string {
rpnList := []string{root}
// Expand out
keepGoing := true
for keepGoing {
// Do this until there are no more monkey names in stack
keepGoing = false
newList := []string{}
// Move from bottom to top
for _, m := range rpnList {
if strings.ContainsAny(m, "0123456789+-/*") || m == "x" {
newList = append(newList, m)
continue
}
// Split the operation op and append this operation to the existing op stack
keepGoing = true
j := monkeys[m]
switch j.Operation {
case "":
newList = append(newList, j.A)
default:
newList = append(newList, j.B, j.A, j.Operation)
}
}
rpnList = newList
}
return rpnList
}
func doCalculation(rpnList []string) IntStack {
nStack := IntStack{}
for _, v := range rpnList {
switch v {
case "+":
a := nStack.Pop()
b := nStack.Pop()
nStack.Push(a + b)
case "-":
a := nStack.Pop()
b := nStack.Pop()
nStack.Push(a - b)
case "/":
a := nStack.Pop()
b := nStack.Pop()
nStack.Push(a / b)
case "*":
a := nStack.Pop()
b := nStack.Pop()
nStack.Push(a * b)
default:
n, err := strconv.ParseInt(v, 10, 64)
utils.Check(err, "unable to convert %s to integer", v)
nStack.Push(n)
}
}
return nStack
}
func undoCalculation(rpnList []string, nStack IntStack, equality int64) IntStack {
// Reverse stack
// Reverse the stack
for i := 0; i < len(nStack)/2; i++ {
nStack[i], nStack[len(nStack)-1-i] = nStack[len(nStack)-1-i], nStack[i]
}
// Append equality to stack
nStack = append(nStack, equality)
// Reverse the condensed list mostly, but keep numbers paired with their operations
// Condensing already swapped the numbers so their operations preceed them
for i := 0; i < len(rpnList)/2; i++ {
rpnList[i], rpnList[len(rpnList)-1-i] = rpnList[len(rpnList)-1-i], rpnList[i]
}
// undo calculations
for i := 0; i < len(rpnList); i++ {
v := rpnList[i]
switch v {
case "+":
a := nStack.Pop()
b := nStack.Pop()
nStack.Push(a - b)
case "-":
a := nStack.Pop()
b := nStack.Pop()
nStack.Push(a + b)
case "*":
a := nStack.Pop()
b := nStack.Pop()
nStack.Push(a / b)
case "/":
a := nStack.Pop()
b := nStack.Pop()
nStack.Push(a * b)
default:
n, err := strconv.ParseInt(v, 10, 64)
utils.Check(err, "unable to convert %s to integer", v)
i++
v = rpnList[i]
l := nStack.Pop()
switch v {
case "+":
nStack.Push(l - n)
case "*":
nStack.Push(l / n)
case "-":
nStack.Push(n - l)
case "/":
nStack.Push(n / l)
}
}
}
return nStack
}
func doRevCalculation(rpnList []string, nStack IntStack) IntStack {
for _, v := range rpnList {
switch v {
case "+":
a := nStack.Pop()
b := nStack.Pop()
nStack.Push(a + b)
case "-":
a := nStack.Pop()
b := nStack.Pop()
nStack.Push(a - b)
case "/":
a := nStack.Pop()
b := nStack.Pop()
nStack.Push(a / b)
case "*":
a := nStack.Pop()
b := nStack.Pop()
nStack.Push(a * b)
default:
n, err := strconv.ParseInt(v, 10, 64)
utils.Check(err, "unable to convert %s to integer", v)
t := nStack.Pop()
nStack.Push(n)
nStack.Push(t)
}
}
return nStack
}
func splitAroundVariable(rpnList []string) ([]string, []string) {
for i, p := range rpnList {
if p == "x" {
return rpnList[:i], rpnList[i+1:]
}
}
empty := []string{}
return rpnList, empty
}
func condense(rpnList []string) []string {
nStack := IntStack{}
remain := []string{}
for _, v := range rpnList {
switch v {
case "+", "-", "/", "*":
switch len(nStack) {
case 0:
remain = append(remain, v)
case 1:
remain = append(remain, v)
remain = append(remain, fmt.Sprintf("%d", nStack.Pop()))
default:
switch v {
case "+":
a := nStack.Pop()
b := nStack.Pop()
nStack.Push(a + b)
case "-":
a := nStack.Pop()
b := nStack.Pop()
nStack.Push(a - b)
case "/":
a := nStack.Pop()
b := nStack.Pop()
nStack.Push(a / b)
case "*":
a := nStack.Pop()
b := nStack.Pop()
nStack.Push(a * b)
default:
n, err := strconv.ParseInt(v, 10, 64)
utils.Check(err, "unable to convert %s to integer", v)
nStack.Push(n)
}
}
default:
n, err := strconv.ParseInt(v, 10, 64)
utils.Check(err, "unable to convert %s to integer", v)
nStack.Push(n)
}
}
return remain
}