-
Notifications
You must be signed in to change notification settings - Fork 0
/
day18.go
167 lines (157 loc) · 3.61 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"fmt"
"io/ioutil"
"os"
"regexp"
"strconv"
"strings"
)
var formulaCheck *regexp.Regexp
func main() {
i, err := ioutil.ReadFile("input.txt")
if err != nil {
os.Exit(1)
}
input := string(i)
fmt.Printf("Part 1: %v\n", SolveDay18Part1(stringListToSlice(input)))
fmt.Printf("Part 2: %v\n", SolveDay18Part2(stringListToSlice(input)))
}
func init() {
formulaCheck, _ = regexp.Compile("^([0-9]|\\s|\\(|\\)|\\+|\\-|\\*)*$")
}
//SolveDay18Part1 calc each line (from left to right, parentheses first) and return the sum of all lines
func SolveDay18Part1(input []string) (s int) {
for _, line := range input {
s += calcPart1(line)
}
return
}
//SolveDay18Part2 calc each line (first add and subtract then multiply, parentheses first) and return the sum of all lines
func SolveDay18Part2(input []string) (s int) {
for _, line := range input {
s += calcPart2(line)
}
return
}
//calcPart1 calculate the given formula from left to right (parentheses first)
func calcPart1(formula string) (sum int) {
if !formulaCheck.MatchString(formula) {
return 0
}
operator := "+"
var parenthesesCount, parenthesesPosition int
for i, char := range formula {
if parenthesesCount != 0 && char != '(' && char != ')' {
continue
}
switch char {
case '(':
parenthesesCount++
if parenthesesCount == 1 {
parenthesesPosition = i + 1
}
case ')':
parenthesesCount--
if parenthesesCount == 0 {
parenthesesSolve := calcPart1(formula[parenthesesPosition:i])
if operator == "+" {
sum += parenthesesSolve
} else if operator == "-" {
sum -= parenthesesSolve
} else if operator == "*" {
sum *= parenthesesSolve
}
}
case ' ':
continue
case '+':
operator = "+"
case '-':
operator = "-"
case '*':
operator = "*"
default:
if operator == "+" {
num, _ := strconv.Atoi(string(char))
sum += num
} else if operator == "-" {
num, _ := strconv.Atoi(string(char))
sum -= num
} else if operator == "*" {
num, _ := strconv.Atoi(string(char))
sum *= num
}
}
}
if parenthesesCount != 0 {
return 0
}
return
}
//calcPart2 calculate the given formula first add and subtract then multiply (parentheses first)
func calcPart2(formula string) int {
if !formulaCheck.MatchString(formula) {
return 0
}
var multiply []int
var parenthesesCount, parenthesesPosition, tempSum int
operator := "+"
for i, char := range formula {
if parenthesesCount != 0 && char != '(' && char != ')' {
continue
}
switch char {
case '(':
parenthesesCount++
if parenthesesCount == 1 {
parenthesesPosition = i + 1
}
case ')':
parenthesesCount--
if parenthesesCount == 0 {
parenthesesSolve := calcPart2(formula[parenthesesPosition:i])
if operator == "+" {
tempSum += parenthesesSolve
} else if operator == "-" {
tempSum -= parenthesesSolve
}
}
case ' ':
continue
case '+':
operator = "+"
case '-':
operator = "-"
case '*':
operator = "+"
multiply = append(multiply, tempSum)
tempSum = 0
default:
if operator == "+" {
num, _ := strconv.Atoi(string(char))
tempSum += num
} else if operator == "-" {
num, _ := strconv.Atoi(string(char))
tempSum -= num
}
}
}
if parenthesesCount != 0 {
return 0
}
multiply = append(multiply, tempSum)
sum := 1
for _, num := range multiply {
sum *= num
}
return sum
}
//Helper functions
//stringListToSlice converts the list of strings (each string one row) to a slice
func stringListToSlice(list string) (s []string) {
for _, line := range strings.Split(strings.TrimSuffix(list, "\n"), "\n") {
s = append(s, line)
}
return
}