-
Notifications
You must be signed in to change notification settings - Fork 0
/
day17_part2.go
48 lines (41 loc) · 1.04 KB
/
day17_part2.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
package day17_part2
import (
"fmt"
"GoAdventOfCode/util"
)
func getAnswer(lines []string) int {
containers := make([]int, 0)
for _, line := range lines {
volume := util.ConvertStringToInt(line)
containers = append(containers, volume)
}
_ = findCombos(containers, 150)
fmt.Println(total)
return total
}
func findCombos(containers []int, target int) [][]int {
combos := [][]int{}
backtrack(containers, target, []int{}, &combos)
return combos
}
var total int = 0
func backtrack(containers []int, target int, currentCombo []int, combos *[][]int) {
if target == 0 {
// Found a valid combo
*combos = append(*combos, append([]int{}, currentCombo...))
if len(currentCombo) == 4 {
total++
}
return
}
if target < 0 || len(containers) == 0 {
// Invalid combo
return
}
// Explore all possible combinations
for i := 0; i < len(containers); i++ {
container := containers[i]
remainingContainers := append([]int{}, containers[i+1:]...)
backtrack(remainingContainers, target-container, append(currentCombo, container), combos)
}
}