/
main.go
228 lines (185 loc) · 3.69 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
package main
import (
"bufio"
"fmt"
"io"
"os"
"path"
"strconv"
"strings"
)
func input() *os.File {
input, err := os.Open(path.Join("2020", "18", "input.txt"))
if err != nil {
panic(err)
}
return input
}
type expression interface {
evaluate() int
}
type constant struct {
value int
}
func (c constant) evaluate() int {
return c.value
}
type binary struct {
left expression
operation string
right expression
}
func (b binary) evaluate() int {
switch b.operation {
case "*":
return b.left.evaluate() * b.right.evaluate()
case "+":
return b.left.evaluate() + b.right.evaluate()
default:
panic(b.operation)
}
}
type grouping struct {
inner expression
}
func (g grouping) evaluate() int {
return g.inner.evaluate()
}
const (
tokenConstant = iota
tokenPlus
tokenMul
tokenLeftParenthesis
tokenRightParenthesis
)
type token struct {
tokenType int
value int
}
func tokenize(raw string) []token {
var tokens []token
for i := 0; i < len(raw); i++ {
curr := raw[i]
switch curr {
case ' ':
continue
case '+':
tokens = append(tokens, token{
tokenType: tokenPlus,
})
continue
case '*':
tokens = append(tokens, token{
tokenType: tokenMul,
})
continue
case '(':
tokens = append(tokens, token{
tokenType: tokenLeftParenthesis,
})
continue
case ')':
tokens = append(tokens, token{
tokenType: tokenRightParenthesis,
})
continue
}
if curr >= '0' && curr <= '9' {
end := i + 1
for end < len(raw) && raw[end] >= '0' && raw[end] <= '9' {
end++
}
intVal, err := strconv.Atoi(raw[i:end])
if err != nil {
panic(err)
}
tokens = append(tokens, token{
tokenType: tokenConstant,
value: intVal,
})
continue
}
panic(curr)
}
return tokens
}
func parse(tokens []token) expression {
if len(tokens) == 1 {
if tokens[0].tokenType != tokenConstant {
panic(tokens)
}
return constant{value: tokens[0].value}
}
var firstExpr expression
secondTokenIndex := 1
if tokens[0].tokenType == tokenRightParenthesis {
end := 1
nestingLevel := 1
for nestingLevel != 0 {
switch tokens[end].tokenType {
case tokenRightParenthesis:
nestingLevel += 1
case tokenLeftParenthesis:
nestingLevel -= 1
}
end += 1
if end >= len(tokens) && nestingLevel != 0 {
panic("unbalanced parens")
}
}
firstExpr = grouping{
inner: parse(tokens[1 : end-1]),
}
secondTokenIndex = end
if end == len(tokens) {
return firstExpr
}
//fmt.Println("Parsed group", firstExpr)
} else {
firstExpr = parse(tokens[:1])
}
//fmt.Println("Parsed first expr as", firstExpr)
switch tokens[secondTokenIndex].tokenType {
case tokenPlus:
return binary{
operation: "+",
left: firstExpr,
right: parse(tokens[secondTokenIndex+1:]),
}
case tokenMul:
return binary{
operation: "*",
left: firstExpr,
right: parse(tokens[secondTokenIndex+1:]),
}
default:
fmt.Println("don't know what to do")
panic(tokens[1])
}
}
func solve(r io.Reader) {
scanner := bufio.NewScanner(r)
sum := 0
for scanner.Scan() {
row := scanner.Text()
tokens := tokenize(row)
newTokens := make([]token, len(tokens))
for i := 0; i < len(tokens); i++ {
newTokens[i] = tokens[len(tokens)-i-1]
}
expr := parse(newTokens)
fmt.Println(expr, expr.evaluate())
sum += expr.evaluate()
}
fmt.Println("SUM", sum)
if scanner.Err() != nil {
panic(scanner.Err())
}
}
func main() {
solve(strings.NewReader("2 * 3 + (4 * 5)"))
solve(strings.NewReader("5 + (8 * 3 + 9 + 3 * 4 * 3)"))
solve(strings.NewReader("5 * 9 * (7 * 3 * 3 + 9 * 3 + (8 + 6 * 4))"))
solve(strings.NewReader("((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2 "))
solve(input())
}