-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday07_2.go
57 lines (46 loc) · 1.46 KB
/
day07_2.go
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
package day07
import (
"strconv"
"strings"
"github.com/blfuentes/AdventOfCode_2024_Go/utilities"
)
func calculate2(expected int64, eqparams []int64, index int) bool {
if index < 0 {
return false
}
lastparam := eqparams[index]
if index == 0 {
return expected == lastparam
}
if expected%lastparam == 0 && calculate2(expected/lastparam, eqparams, index-1) {
return true
}
if expected > lastparam && calculate2(expected-lastparam, eqparams, index-1) {
return true
}
expectedAsString := strconv.FormatInt(expected, 10)
lastparamAsString := strconv.FormatInt(lastparam, 10)
if len(expectedAsString) > len(lastparamAsString) &&
strings.HasSuffix(expectedAsString, lastparamAsString) {
newTarget := utilities.StringToInt64(expectedAsString[:len(expectedAsString)-len(lastparamAsString)])
return calculate2(newTarget, eqparams, index-1)
}
return false
}
func Executepart2() int64 {
var result int64 = 0
var fileName string = "./day07/day07.txt"
if fileContent, err := utilities.ReadFileAsLines(fileName); err == nil {
for index := 0; index < len(fileContent); index++ {
expected := utilities.StringToInt64(strings.Split(fileContent[index], ":")[0])
values := make([]int64, 0)
for _, val := range strings.Split(strings.TrimSpace(strings.Split(fileContent[index], ":")[1]), " ") {
values = append(values, utilities.StringToInt64(val))
}
if calculate2(expected, values, len(values)-1) {
result += expected
}
}
}
return result
}