Skip to content

Commit e5cda69

Browse files
committed
[2024] Solution for Day 7
1 parent b5f0a88 commit e5cda69

File tree

3 files changed

+175
-1
lines changed

3 files changed

+175
-1
lines changed

2024/day07/main.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
"strings"
8+
9+
"github.com/kfarnung/advent-of-code/2024/lib"
10+
)
11+
12+
type equation struct {
13+
testValue int64
14+
operands []int64
15+
}
16+
17+
func part1(input string) int64 {
18+
equations, err := parseInput(input)
19+
if err != nil {
20+
log.Fatal(err)
21+
}
22+
23+
sum := int64(0)
24+
for _, equation := range equations {
25+
if isValidEquation(0, equation.operands, equation.testValue, false) {
26+
sum += equation.testValue
27+
}
28+
}
29+
30+
return sum
31+
}
32+
33+
func part2(input string) int64 {
34+
equations, err := parseInput(input)
35+
if err != nil {
36+
log.Fatal(err)
37+
}
38+
39+
sum := int64(0)
40+
for _, equation := range equations {
41+
if isValidEquation(0, equation.operands, equation.testValue, true) {
42+
sum += equation.testValue
43+
}
44+
}
45+
46+
return sum
47+
}
48+
49+
func isValidEquation(current int64, operands []int64, target int64, allowConcat bool) bool {
50+
if len(operands) == 0 {
51+
return current == target
52+
}
53+
54+
operand := operands[0]
55+
operands = operands[1:]
56+
57+
if isValidEquation(current+operand, operands, target, allowConcat) {
58+
return true
59+
}
60+
61+
if allowConcat {
62+
if isValidEquation(current*getMultiplier(operand)+operand, operands, target, allowConcat) {
63+
return true
64+
}
65+
}
66+
67+
if current == 0 {
68+
current = 1
69+
}
70+
71+
if isValidEquation(current*operand, operands, target, allowConcat) {
72+
return true
73+
}
74+
75+
return false
76+
}
77+
78+
func getMultiplier(num int64) int64 {
79+
if num == 0 {
80+
return 1
81+
}
82+
83+
multiplier := int64(1)
84+
for num > 0 {
85+
num /= 10
86+
multiplier *= 10
87+
}
88+
89+
return multiplier
90+
}
91+
92+
func parseInput(input string) ([]equation, error) {
93+
var equations []equation
94+
for _, line := range lib.SplitLines(input) {
95+
before, after, found := strings.Cut(line, ": ")
96+
if !found {
97+
continue
98+
}
99+
100+
testValue, err := lib.ParseInt[int64](before)
101+
if err != nil {
102+
return nil, err
103+
}
104+
105+
var operands []int64
106+
for _, operandValue := range strings.Split(after, " ") {
107+
operand, err := lib.ParseInt[int64](operandValue)
108+
if err != nil {
109+
return nil, err
110+
}
111+
112+
operands = append(operands, operand)
113+
}
114+
115+
equations = append(equations, equation{testValue: testValue, operands: operands})
116+
}
117+
118+
return equations, nil
119+
}
120+
121+
func main() {
122+
name := os.Args[1]
123+
content, err := lib.LoadFileContent(name)
124+
if err != nil {
125+
log.Fatal(err)
126+
}
127+
128+
fmt.Printf("Part 1: %d\n", part1(content))
129+
fmt.Printf("Part 2: %d\n", part2(content))
130+
}

2024/day07/main_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package main
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/kfarnung/advent-of-code/2024/lib"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
var input = strings.Join([]string{
12+
"190: 10 19",
13+
"3267: 81 40 27",
14+
"83: 17 5",
15+
"156: 15 6",
16+
"7290: 6 8 6 15",
17+
"161011: 16 10 13",
18+
"192: 17 8 14",
19+
"21037: 9 7 18 13",
20+
"292: 11 6 16 20",
21+
"",
22+
}, "\n")
23+
24+
func TestPart1(t *testing.T) {
25+
assert.Equal(t, int64(3749), part1(input))
26+
27+
inputContent, err := lib.GetInputContent()
28+
if err != nil {
29+
t.Fatal(err)
30+
}
31+
32+
assert.Equal(t, int64(3598800864292), part1(inputContent))
33+
}
34+
35+
func TestPart2(t *testing.T) {
36+
assert.Equal(t, int64(11387), part2(input))
37+
38+
inputContent, err := lib.GetInputContent()
39+
if err != nil {
40+
t.Fatal(err)
41+
}
42+
43+
assert.Equal(t, int64(340362529351427), part2(inputContent))
44+
}

private

0 commit comments

Comments
 (0)