-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
197 lines (184 loc) · 4.61 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
package main
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
)
func main() {
fmt.Println("step1 ==>", step1(load("puzzle_input.txt")))
fmt.Println("step2 ==>", step2(load("puzzle_input.txt")))
}
func computeArray2(input []string) int {
multOnlyInput := []string{}
ops := ""
result := 0
for tokenIndex := 0; tokenIndex < len(input); tokenIndex++ {
token := string(input[tokenIndex])
fmt.Println("token is ", token, " result is ", result, "ops is ", ops, "multiops", multOnlyInput)
switch token {
case "+":
fmt.Println("tracking +")
ops = "+"
case "*":
ops = ""
multOnlyInput = append(multOnlyInput, itoa(result))
multOnlyInput = append(multOnlyInput, "*")
result = 0
fmt.Println("found a * adding everything we got to string", multOnlyInput)
case "(":
fmt.Println("chasing )")
indexClosing := tokenIndex + 1
groundZero := 0
for input[indexClosing] != ")" || groundZero != 0 {
fmt.Println("chasing ) found", input[indexClosing], "level", groundZero)
if input[indexClosing] == "(" {
groundZero++
fmt.Println("since we found ( level is now", groundZero)
}
if input[indexClosing] == ")" {
groundZero--
fmt.Println("since we found ) level is now", groundZero)
}
indexClosing++
}
fmt.Println("parent expression is ", input[tokenIndex+1:indexClosing])
intermediaryResult := computeArray2(input[tokenIndex+1 : indexClosing])
tokenIndex = indexClosing
if ops == "" {
result = intermediaryResult
} else {
fmt.Println("found a + ", intermediaryResult)
result += intermediaryResult
fmt.Println("result is now", result)
ops = ""
}
default:
if ops == "" {
fmt.Println("putting", input[tokenIndex], "into result")
result = atoi(input[tokenIndex])
} else {
fmt.Println("found a + ", input[tokenIndex])
result += atoi(input[tokenIndex])
fmt.Println("result is now", result)
ops = ""
}
}
}
if result > 0 {
multOnlyInput = append(multOnlyInput, itoa(result))
}
fmt.Println("passing", multOnlyInput)
return computeArray(multOnlyInput)
}
func computeArray(input []string) int {
if len(input) == 1 {
return atoi(input[0])
}
ops := ""
result := 0
for tokenIndex := 0; tokenIndex < len(input); tokenIndex++ {
token := string(input[tokenIndex])
fmt.Println("token is ", token, " result is ", result, "ops is ", ops)
switch token {
case "(":
fmt.Println("chasing )")
indexClosing := tokenIndex + 1
groundZero := 0
for input[indexClosing] != ")" || groundZero != 0 {
fmt.Println("chasing ) found", input[indexClosing], "level", groundZero)
if input[indexClosing] == "(" {
groundZero++
fmt.Println("since we found ( level is now", groundZero)
}
if input[indexClosing] == ")" {
groundZero--
fmt.Println("since we found ) level is now", groundZero)
}
indexClosing++
}
fmt.Println("parent expression is ", input[tokenIndex+1:indexClosing])
parResult := computeArray(input[tokenIndex+1 : indexClosing])
tokenIndex = indexClosing
if ops == "" {
result = parResult
} else {
if ops == "+" {
result += parResult
} else {
result *= parResult
}
ops = ""
}
case "+":
ops = "+"
case "*":
ops = "*"
default:
if ops == "" {
result = atoi(input[tokenIndex])
} else {
if ops == "+" {
fmt.Println("+", input[tokenIndex])
result += atoi(input[tokenIndex])
} else {
result *= atoi(input[tokenIndex])
}
ops = ""
}
}
}
fmt.Println("returning", result)
return result
}
func compute(input string) int {
fmt.Println("------")
newInput := strings.Fields(strings.NewReplacer("(", " ( ", ")", " ) ").Replace(input))
return computeArray(newInput)
}
func compute2(input string) int {
fmt.Println("------")
newInput := strings.Fields(strings.NewReplacer("(", " ( ", ")", " ) ").Replace(input))
return computeArray2(newInput)
}
func step1(dataAsString string) int {
sum := 0
for _, line := range strings.Split(dataAsString, "\n") {
sum += compute(line)
}
return sum
}
func step2(dataAsString string) int {
sum := 0
for _, line := range strings.Split(dataAsString, "\n") {
sum += compute2(line)
}
return sum
}
func find(name []string, target string) bool {
for i := 0; i < len(name); i++ {
if name[i] == target {
return true
}
}
return false
}
func load(filename string) string {
text, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Print(err)
}
return string(text)
}
func atoi(line string) int {
number, err := strconv.Atoi(line)
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
return number
}
func itoa(line int) string {
return strconv.Itoa(line)
}