Skip to content

Commit 9d99fa9

Browse files
committed
feat: Solving day 7
1 parent 0816e8e commit 9d99fa9

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed

docs/day7.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
url: "https://adventofcode.com/2024/day/7"
3+
---
4+
5+
# Day 7: Bridge Repair
6+
7+
The Historians take you to a familiar rope bridge over a river in the middle of a jungle. The Chief isn't on this side of the bridge, though; maybe he's on the other side?
8+
9+
When you go to cross the bridge, you notice a group of engineers trying to repair it. (Apparently, it breaks pretty frequently.) You won't be able to cross until it's fixed.
10+
11+
You ask how long it'll take; the engineers tell you that it only needs final calibrations, but some young elephants were playing nearby and stole all the operators from their calibration equations! They could finish the calibrations if only someone could determine which test values could possibly be produced by placing any combination of operators into their calibration equations (your puzzle input).
12+
13+
For example:
14+
15+
```txt
16+
190: 10 19
17+
3267: 81 40 27
18+
83: 17 5
19+
156: 15 6
20+
7290: 6 8 6 15
21+
161011: 16 10 13
22+
192: 17 8 14
23+
21037: 9 7 18 13
24+
292: 11 6 16 20
25+
```
26+
27+
Each line represents a single equation. The test value appears before the colon on each line; it is your job to determine whether the remaining numbers can be combined with operators to produce the test value.
28+
29+
Operators are always evaluated left-to-right, not according to precedence rules. Furthermore, numbers in the equations cannot be rearranged. Glancing into the jungle, you can see elephants holding two different types of operators: add (`+`) and multiply (`*`).
30+
31+
Only three of the above equations can be made true by inserting operators:
32+
33+
* `190: 10 19` has only one position that accepts an operator: between `10` and `19`. Choosing `+` would give `29`, but choosing `*` would give the test value (`10 * 19 = 190`).
34+
* `3267: 81 40 27` has two positions for operators. Of the four possible configurations of the operators, two cause the right side to match the test value: `81 + 40 * 27` and `81 * 40 + 27` both equal `3267` (when evaluated left-to-right)!
35+
* `292: 11 6 16 20` can be solved in exactly one way: `11 + 6 * 16 + 20`.
36+
37+
The engineers just need the total calibration result, which is the sum of the test values from just the equations that could possibly be true. In the above example, the sum of the test values for the three equations listed above is `3749`.
38+
39+
Determine which equations could possibly be true. What is their total calibration result?

src/day7/day7.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package day7
2+
3+
import (
4+
"slices"
5+
"strconv"
6+
"strings"
7+
)
8+
9+
type equation struct {
10+
result uint
11+
coefficients []uint
12+
}
13+
14+
func Solve(input string) uint {
15+
equations := parseInput(input)
16+
return solve(equations)
17+
}
18+
19+
func parseInput(input string) []equation {
20+
result := make([]equation, 0)
21+
for _, line := range strings.Split(input, "\n") {
22+
var eqn equation
23+
pieces := strings.Split(line, ": ")
24+
if len(pieces) != 2 {
25+
continue
26+
}
27+
28+
x, err := strconv.Atoi(pieces[0])
29+
if err != nil {
30+
continue
31+
}
32+
eqn.result = uint(x)
33+
34+
for _, coef := range strings.Split(pieces[1], " ") {
35+
y, err := strconv.Atoi(coef)
36+
if err != nil {
37+
continue
38+
}
39+
eqn.coefficients = slices.Insert(eqn.coefficients, len(eqn.coefficients), uint(y))
40+
}
41+
42+
result = slices.Insert(result, len(result), eqn)
43+
}
44+
return result
45+
}
46+
47+
func solve(eqns []equation) uint {
48+
var result uint = 0
49+
for _, eqn := range eqns {
50+
if trueEquation(eqn) {
51+
result += eqn.result
52+
}
53+
}
54+
return result
55+
}
56+
57+
func trueEquation(eqn equation) bool {
58+
possibilities := []uint{eqn.coefficients[0]}
59+
for i, coef := range eqn.coefficients {
60+
if i == 0 {
61+
continue
62+
}
63+
64+
updatedPossibilities := []uint{}
65+
for _, priorPartial := range possibilities {
66+
sum := priorPartial + coef
67+
if sum <= eqn.result {
68+
updatedPossibilities = slices.Insert(updatedPossibilities, len(updatedPossibilities), sum)
69+
}
70+
mult := priorPartial * coef
71+
if mult <= eqn.result {
72+
updatedPossibilities = slices.Insert(updatedPossibilities, len(updatedPossibilities), mult)
73+
}
74+
}
75+
possibilities = updatedPossibilities
76+
}
77+
78+
for _, poss := range possibilities {
79+
if poss == eqn.result {
80+
return true
81+
}
82+
}
83+
return false
84+
}

src/day7/day7_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package day7
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestSample(t *testing.T) {
8+
input := `190: 10 19
9+
3267: 81 40 27
10+
83: 17 5
11+
156: 15 6
12+
7290: 6 8 6 15
13+
161011: 16 10 13
14+
192: 17 8 14
15+
21037: 9 7 18 13
16+
292: 11 6 16 20`
17+
result := Solve(input)
18+
if result != 3749 {
19+
t.Errorf("Calculated solution was not expected")
20+
}
21+
}

0 commit comments

Comments
 (0)