-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
133 lines (122 loc) · 2.98 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
package main
import (
_ "embed"
"fmt"
"regexp"
"strings"
. "github.com/beanz/advent/lib-go"
)
//go:embed input.txt
var input []byte
var (
finishedMatch = regexp.MustCompile(`^(\d+)$`)
tetheredPlus = regexp.MustCompile(`^(\d+) \+ (\d+)`)
tetheredMult = regexp.MustCompile(`^(\d+) \* (\d+)`)
untetheredPlus = regexp.MustCompile(`(\d+) \+ (\d+)`)
bracketMatch = regexp.MustCompile(`\(([^()]+)\)`)
)
func ReadNInt64s(s string, expected int, msg string) []int64 {
nums := SimpleReadInt64s(s)
if len(nums) != expected {
panic(msg)
}
return nums
}
func Part1Math(s string) int64 {
if DEBUG() {
fmt.Printf("P1: %s\n", s)
}
if m := finishedMatch.FindStringSubmatch(s); m != nil {
if DEBUG() {
fmt.Printf("Just number: %s\n", s)
}
nums := ReadNInt64s(m[0], 1, "invalid answer"+s)
return nums[0]
}
if m := bracketMatch.FindStringSubmatch(s); m != nil {
if DEBUG() {
fmt.Printf("(): %s => %s\n", s, m[1])
}
return Part1Math(strings.Replace(s,
m[0], fmt.Sprintf("%d", Part1Math(m[1])), 1))
}
if m := tetheredPlus.FindStringSubmatch(s); m != nil {
if DEBUG() {
fmt.Printf("+: %s => %s\n", s, m[0])
}
nums := ReadNInt64s(m[0], 2, "invalid + operands in "+m[0])
return Part1Math(strings.Replace(s,
m[0], fmt.Sprintf("%d", nums[0]+nums[1]), 1))
}
if m := tetheredMult.FindStringSubmatch(s); m != nil {
if DEBUG() {
fmt.Printf("*: %s => %s\n", s, m[0])
}
nums := ReadNInt64s(m[0], 2, "invalid * operands in "+m[0])
return Part1Math(strings.Replace(s,
m[0], fmt.Sprintf("%d", nums[0]*nums[1]), 1))
}
return 1
}
func Part1(lines []string) int64 {
var s int64
for _, sum := range lines {
s += Part1Math(sum)
}
return s
}
func Part2Math(s string) int64 {
if DEBUG() {
fmt.Printf("P1: %s\n", s)
}
if m := finishedMatch.FindStringSubmatch(s); m != nil {
if DEBUG() {
fmt.Printf("Just number: %s\n", s)
}
nums := ReadNInt64s(m[0], 1, "invalid answer"+s)
return nums[0]
}
if m := bracketMatch.FindStringSubmatch(s); m != nil {
if DEBUG() {
fmt.Printf("(): %s => %s\n", s, m[1])
}
return Part2Math(strings.Replace(s,
m[0], fmt.Sprintf("%d", Part2Math(m[1])), 1))
}
if m := untetheredPlus.FindStringSubmatch(s); m != nil {
if DEBUG() {
fmt.Printf("+: %s => %s\n", s, m[0])
}
nums := ReadNInt64s(m[0], 2, "invalid + operands in "+m[0])
return Part2Math(strings.Replace(s,
m[0], fmt.Sprintf("%d", nums[0]+nums[1]), 1))
}
if m := tetheredMult.FindStringSubmatch(s); m != nil {
if DEBUG() {
fmt.Printf("*: %s => %s\n", s, m[0])
}
nums := ReadNInt64s(m[0], 2, "invalid * operands in "+m[0])
return Part2Math(strings.Replace(s,
m[0], fmt.Sprintf("%d", nums[0]*nums[1]), 1))
}
return 1
}
func Part2(lines []string) int64 {
var s int64
for _, sum := range lines {
s += Part2Math(sum)
}
return s
}
func main() {
lines := InputLines(input)
p1 := Part1(lines)
if !benchmark {
fmt.Printf("Part 1: %d\n", p1)
}
p2 := Part2(lines)
if !benchmark {
fmt.Printf("Part 2: %d\n", p2)
}
}
var benchmark = false