-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday07_1.go
46 lines (38 loc) · 1.08 KB
/
day07_1.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
package day07
import (
"strings"
"github.com/blfuentes/AdventOfCode_2024_Go/utilities"
)
func calculate1(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 && calculate1(expected/lastparam, eqparams, index-1) {
return true
}
if expected > lastparam && calculate1(expected-lastparam, eqparams, index-1) {
return true
}
return false
}
func Executepart1() 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 calculate1(expected, values, len(values)-1) {
result += expected
}
}
}
return result
}