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
|
package main
import (
"fmt"
"os"
"strconv"
"strings"
)
func FileRead(path string) string {
file, err := os.ReadFile(path)
if err != nil {
fmt.Println("Couldn't Read file: ", err)
}
return string(file)
}
type Op = string
const (
Plus Op = "+"
Mul Op = "*"
Concat Op = "||"
)
var OpsOne = []Op{Plus, Mul}
var OpsTwo = []Op{Plus, Mul, Concat}
type Calibration struct {
target int
vals []int
}
type Expression = []string
func (c Calibration) String() string {
valsStr := strings.Trim(strings.Join(strings.Fields(fmt.Sprint(c.vals)), ", "), "[]")
return fmt.Sprintf("Calibration(target: %d, vals: [%s])", c.target, valsStr)
}
func parseInput(data string) []Calibration {
lines := strings.Split(data, "\n")
var cals []Calibration
for _, line := range lines {
if len(line) > 0 {
cal := strings.Split(line, ": ")
target, _ := strconv.Atoi(cal[0])
var vals []int
for _, num := range strings.Split(cal[1], " ") {
val, _ := strconv.Atoi(num)
vals = append(vals, val)
}
calibration := Calibration{
target: target,
vals: vals,
}
cals = append(cals, calibration)
}
}
return cals
}
func generatePossibleExpressions(opsLeft int, expr *Expression, expressions *[]Expression, Ops []string) {
if opsLeft == 0 {
comb := make([]string, len(*expr))
copy(comb, *expr)
*expressions = append(*expressions, comb)
} else {
for _, op := range Ops {
// choose
*expr = append(*expr, op)
// explore
generatePossibleExpressions(opsLeft-1, expr, expressions, Ops)
// unchoose
*expr = (*expr)[:len(*expr)-1]
}
}
}
func removeElement(s []int, i int) ([]int, error) {
if i >= len(s) || i < 0 {
return nil, fmt.Errorf("Index is out of range. Index is %d with slice length %d", i, len(s))
}
return append(s[:i], s[i+1:]...), nil
}
func removeElementString(s []string, i int) ([]string, error) {
if i >= len(s) || i < 0 {
return nil, fmt.Errorf("Index is out of range. Index is %d with slice length %d", i, len(s))
}
return append(s[:i], s[i+1:]...), nil
}
func evalPartOne(vals []int, expression *Expression) int {
for _, op := range *expression {
left := vals[0]
right := vals[1]
if op == Mul {
vals, _ = removeElement(vals, 0)
vals[0] = left * right
} else if op == Plus {
vals, _ = removeElement(vals, 0)
vals[0] = left + right
}
}
return vals[0]
}
func evalPartTwo(vals []int, expression *Expression) int {
for _, op := range *expression {
left := vals[0]
right := vals[1]
if op == Mul {
vals, _ = removeElement(vals, 0)
vals[0] = left * right
} else if op == Plus {
vals, _ = removeElement(vals, 0)
vals[0] = left + right
} else if op == "||" {
vals, _ = removeElement(vals, 0)
// vals[0], _ = strconv.Atoi(strconv.Itoa(left) + strconv.Itoa(right))
multiplier := 1
for temp := right; temp > 0; temp /= 10 {
multiplier *= 10
}
vals[0] = left*multiplier + right
}
}
// fmt.Printf("expression: %v\n", (*expression))
// fmt.Printf("vals: %v\n", vals)
return vals[0]
}
func solve_part_one(data string) int64 {
calibrations := parseInput(data)
var ans int64 = 0
for _, calibration := range calibrations {
var expressions []Expression
generatePossibleExpressions((len(calibration.vals) - 1), &[]string{}, &expressions, OpsOne)
for _, expr := range expressions {
copy_vals := make([]int, len(calibration.vals))
copy(copy_vals, calibration.vals)
if evalPartOne(copy_vals, &expr) == calibration.target {
// fmt.Printf("expr: %v\n", expr)
ans += int64(calibration.target)
break
}
}
// fmt.Printf("expressions: %v\n", expressions)
expressions = []Expression{}
}
return ans
}
func solve_part_two(data string) int64 {
calibrations := parseInput(data)
var ans int64 = 0
for _, calibration := range calibrations {
var expressions []Expression
generatePossibleExpressions((len(calibration.vals) - 1), &[]string{}, &expressions, OpsTwo)
for _, expr := range expressions {
copy_vals := make([]int, len(calibration.vals))
copy(copy_vals, calibration.vals)
if evalPartTwo(copy_vals, &expr) == calibration.target {
// fmt.Printf("expr: %v\n", expr)
ans += int64(calibration.target)
break
}
}
expressions = []Expression{}
}
return ans
}
func main() {
test := FileRead("../input/day07.test")
prod := FileRead("../input/day07.prod")
fmt.Println("Part_1 test: ", solve_part_one(test))
fmt.Println("Part_1 prod: ", solve_part_one(prod))
fmt.Println("Part_2 test: ", solve_part_two(test))
fmt.Println("Part_2 prod: ", solve_part_two(prod))
}
|