Day 07. part 2

This commit is contained in:
Borna Rajković 2024-12-08 17:00:28 +01:00
parent 830dcade38
commit a16d3c95b8
2 changed files with 55 additions and 17 deletions

View File

@ -1,10 +1,19 @@
package day_07 package day_07
import ( import (
"math"
"strconv" "strconv"
"strings" "strings"
) )
type Operator int
const (
ADD = iota
MULTIPLY = iota
CONCAT = iota
)
type Equation struct { type Equation struct {
Result int Result int
Variables []int Variables []int
@ -16,40 +25,69 @@ func SolveBasic(input string) int {
sum := 0 sum := 0
for _, equation := range equations { for _, equation := range equations {
if Solvable(equation) { if Solvable(equation, 2) {
sum += equation.Result sum += equation.Result
} }
} }
return sum return sum
} }
func Solvable(equation Equation) bool { func Solvable(equation Equation, operators int) bool {
for i := 0; i < 1<<(len(equation.Variables)-1); i++ { for i := 0; i < numberOfSolutions(equation, operators); i++ {
if solvable(equation, i) { if solvable(equation, operators, i) {
return true return true
} }
} }
return false return false
} }
func solvable(equation Equation, options int) bool { func numberOfSolutions(equation Equation, operators int) int {
return int(math.Pow(float64(operators), float64(len(equation.Variables)-1)))
}
func solvable(equation Equation, operators int, options int) bool {
sum := equation.Variables[0] sum := equation.Variables[0]
for i := 0; i < len(equation.Variables)-1; i++ { for i := 0; i < len(equation.Variables)-1; i++ {
if sum > equation.Result { if sum > equation.Result {
return false return false
} }
if options&(1<<i) != 0 { operator := options % operators
options = options / operators
switch operator {
case ADD:
sum += equation.Variables[i+1]
case MULTIPLY:
sum *= equation.Variables[i+1] sum *= equation.Variables[i+1]
} else { case CONCAT:
offset := findLarger(equation.Variables[i+1])
sum *= offset
sum += equation.Variables[i+1] sum += equation.Variables[i+1]
} }
} }
return sum == equation.Result return sum == equation.Result
} }
func findLarger(val int) int {
larger := 10
for val >= larger {
larger *= 10
}
return larger
}
// task:https://adventofcode.com/2024/day/7#part2 // task:https://adventofcode.com/2024/day/7#part2
func SolveComplex(input string) int { func SolveComplex(input string) int {
return 0 equations := loadEquations(input)
sum := 0
for _, equation := range equations {
if Solvable(equation, 3) {
sum += equation.Result
}
}
return sum
} }
func loadEquations(input string) (equations []Equation) { func loadEquations(input string) (equations []Equation) {

View File

@ -18,17 +18,17 @@ func TestBasicSolutionExample(t *testing.T) {
func TestComplexSolutionExample(t *testing.T) { func TestComplexSolutionExample(t *testing.T) {
result := day_07.SolveComplex(ExampleData) result := day_07.SolveComplex(ExampleData)
if result != 6 { if result != 11387 {
t.Fatalf("Expected 6 received %d", result) t.Fatalf("Expected 11387 received %d", result)
} }
} }
func TestSolvableExample(t *testing.T) { func TestSolvable(t *testing.T) {
result := day_07.Solvable(day_07.Equation{ if !day_07.Solvable(day_07.Equation{
Result: 45694842, Result: 27100,
Variables: []int{2, 9, 227, 6, 1, 5, 4, 382, 1, 5, 2}, Variables: []int{3, 9, 100},
}) }, 3) {
if !result { t.Fatalf("Expected true")
t.Fatalf("Expected true received false")
} }
} }