Skip to content

Commit 82b8008

Browse files
committed
day7: solution for both parts
1 parent b5f6c48 commit 82b8008

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

day7/example.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
190: 10 19
2+
3267: 81 40 27
3+
83: 17 5
4+
156: 15 6
5+
7290: 6 8 6 15
6+
161011: 16 10 13
7+
192: 17 8 14
8+
21037: 9 7 18 13
9+
292: 11 6 16 20

day7/main.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"os"
7+
"strings"
8+
9+
"github.com/fxnn/adventofcode2024/util"
10+
)
11+
12+
const (
13+
ADD = '+'
14+
MUL = '*'
15+
CON = '|'
16+
)
17+
18+
var allOperators = []byte{ADD, MUL, CON}
19+
20+
func calculate(operands []int, operators []byte) int {
21+
var result = operands[0]
22+
for i := 0; i < len(operators); i++ {
23+
if operators[i] == ADD {
24+
result += operands[i+1]
25+
} else if operators[i] == MUL {
26+
result *= operands[i+1]
27+
} else {
28+
result = util.Atoi(util.Itoa(result) + util.Itoa(operands[i+1]))
29+
}
30+
}
31+
return result
32+
}
33+
34+
func formula(operands []int, operators []byte) string {
35+
var s = util.Itoa(operands[0])
36+
for i := 0; i < len(operators); i++ {
37+
s += fmt.Sprintf(" %s %d", string(operators[i]), operands[i+1])
38+
}
39+
40+
return s
41+
}
42+
43+
func modifyOperators(operators []byte, index int, newOperator byte) []byte {
44+
// we need to copy, because slices are, although passed by value, only shallow copies
45+
// referring to the same backing array
46+
var result = make([]byte, len(operators))
47+
copy(result[:], operators[:])
48+
result[index] = newOperator
49+
return result
50+
}
51+
52+
func isOperatorsExistStep(testResult int, operands []int, operators []byte, fixedOperators int) bool {
53+
if fixedOperators >= len(operators) {
54+
var match = calculate(operands, operators) == testResult
55+
if match {
56+
fmt.Printf("%d: %s\n", testResult, formula(operands, operators))
57+
}
58+
return match
59+
}
60+
61+
for _, o := range allOperators {
62+
var newOps = modifyOperators(operators, fixedOperators, o)
63+
if isOperatorsExistStep(testResult, operands, newOps, fixedOperators+1) {
64+
return true
65+
}
66+
}
67+
return false
68+
}
69+
70+
func makeOperators(count int) []byte {
71+
var operators = make([]byte, count)
72+
for i := 0; i < count; i++ {
73+
operators[i] = ADD
74+
}
75+
return operators
76+
}
77+
78+
func isOperatorsExist(testResult int, operands []int) bool {
79+
var operators = makeOperators(len(operands) - 1)
80+
var fixedOperators = 0
81+
return isOperatorsExistStep(testResult, operands, operators, fixedOperators)
82+
}
83+
84+
func main() {
85+
scanner := bufio.NewScanner(os.Stdin)
86+
scanner.Split(bufio.ScanLines)
87+
88+
var total = 0
89+
for scanner.Scan() {
90+
var line = scanner.Text()
91+
var parts = strings.SplitN(line, ": ", 2)
92+
var testResult = util.Atoi(parts[0])
93+
var operands = util.AtoiList(strings.Split(parts[1], " "))
94+
if isOperatorsExist(testResult, operands) {
95+
total += testResult
96+
}
97+
}
98+
99+
fmt.Printf("total calibration result: %d\n", total)
100+
}

util/util.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import (
88

99
type Void struct{}
1010

11+
// Itoa converts integer to string
12+
func Itoa(i int) string {
13+
return strconv.Itoa(i)
14+
}
15+
1116
// Atoi converts string to integer, and exist immediately upon error
1217
func Atoi(s string) int {
1318
if i, err := strconv.Atoi(s); err != nil {
@@ -19,6 +24,16 @@ func Atoi(s string) int {
1924
}
2025
}
2126

27+
// AtoiList converts a list of strings into a list of integers,
28+
// whereas the result list has the same size as the given one.
29+
func AtoiList(l []string) []int {
30+
var result = make([]int, len(l))
31+
for i, s := range l {
32+
result[i] = Atoi(s)
33+
}
34+
return result
35+
}
36+
2237
// Abs calculates the absolute value of an integer
2338
func Abs(x int) int {
2439
if x < 0 {

0 commit comments

Comments
 (0)