-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay18.go
152 lines (134 loc) · 2.9 KB
/
Day18.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
package main
import (
"fmt"
"strconv"
"strings"
)
func (day *Day) Day18a() {
fmt.Printf("Part 1: %d\n", ComputeDay18a(day.input))
}
func (day *Day) Day18b() {
fmt.Printf("Part 2: %d\n", ComputeDay18b(day.input))
}
func ComputeDay18a(input string) int {
lines := strings.Split(input, "\n")
res := 0
for _, line := range lines {
res = res + ComputeExpressionDay18a(line, false)
}
return res
}
func ComputeDay18b(input string) int {
lines := strings.Split(input, "\n")
res := 0
for _, line := range lines {
res = res + ComputeExpressionDay18a(line, true)
}
return res
}
func ComputeExpressionDay18a(line string, part2 bool) int {
expr := line
leftPar := strings.Index(expr, "(")
for leftPar >= 0 {
rightPar := findFirstMatchingRightPar(expr)
enclosedResult := ComputeExpressionDay18a(expr[leftPar + 1:rightPar], part2)
suffix := ""
if rightPar < len(expr) - 1 {
suffix = expr[rightPar + 1:]
}
expr = expr[:leftPar] + strconv.Itoa(enclosedResult) + suffix
leftPar = strings.Index(expr, "(")
}
if !part2 {
return ComputeParentheseFreeExpressionPart1(expr)
} else {
return ComputeParentheseFreeExpressionPart2(expr)
}
}
func findFirstMatchingRightPar(expr string) int {
nesting := 0
for i, c := range expr {
if c == '(' {
nesting++
} else if c == ')' {
nesting--
if nesting == 0 {
return i
}
}
}
panic("Could not find matching parenthesis " + expr)
}
func ComputeParentheseFreeExpressionPart1(expr string) int {
lhs := -1
rhs := -1
var op int32
var err error
buf := ""
for _, c := range expr {
if c == '*' || c == '+' {
if lhs == - 1 {
lhs, err = strconv.Atoi(buf)
if err != nil {
panic("Could not parse first number "+ buf)
}
buf = ""
} else {
rhs, err = strconv.Atoi(buf)
if err != nil {
panic("Could not parse rhs " + buf)
}
buf = ""
if op == '+' {
lhs = lhs + rhs
} else if op == '*' {
lhs = lhs * rhs
} else {
panic("Unknown operator " + string(op))
}
}
op = c
} else if c == ' ' {
continue
} else {
buf = buf + string(c)
}
}
rhs, err = strconv.Atoi(buf)
if err != nil {
panic("Could not parse rhs " + buf)
}
if op == '+' {
lhs = lhs + rhs
} else if op == '*' {
lhs = lhs * rhs
} else {
return rhs
}
return lhs
}
func ComputeParentheseFreeExpressionPart2(line string) int {
expr := ComputeAdditions(line)
return ComputeParentheseFreeExpressionPart1(expr)
}
func ComputeAdditions(line string) string {
expr := line
multPos := strings.Index(line, "+")
if multPos < 0 {
return expr
}
startPos := 0
for i := multPos - 1; i >= 0; i-- {
if expr[i] == '*' {
startPos = i + 1
break
}
}
endPos := multPos + 1
for ; endPos < len(expr); endPos++ {
if expr[endPos] == '+' || expr[endPos] == '*' {
break
}
}
return ComputeAdditions(expr[:startPos] + strconv.Itoa(ComputeParentheseFreeExpressionPart1(expr[startPos:endPos]))+ expr[endPos:])
}