57 lines
1.3 KiB
Go
57 lines
1.3 KiB
Go
package day17
|
|
|
|
import (
|
|
"sort"
|
|
"tools"
|
|
)
|
|
|
|
func getCombinations(containers []int, sum int) (combinations [][]int) {
|
|
for i, c := range containers {
|
|
if c > sum {
|
|
break
|
|
} else if c == sum {
|
|
combinations = append(combinations, []int{c})
|
|
if i == len(containers)-1 || containers[i+1] != sum {
|
|
return combinations
|
|
}
|
|
} else if i == len(containers)-1 {
|
|
break
|
|
}
|
|
subCombinations := getCombinations(containers[i+1:], sum-c)
|
|
for _, v := range subCombinations {
|
|
subCombination := append([]int{c}, v...)
|
|
combinations = append(combinations, subCombination)
|
|
}
|
|
}
|
|
return combinations
|
|
}
|
|
|
|
func Part1(puzzle tools.AoCPuzzle) interface{} {
|
|
input := puzzle.GetInputArrayInt()
|
|
sort.Ints(input)
|
|
|
|
combos150 := getCombinations(input, 150)
|
|
|
|
return len(combos150)
|
|
}
|
|
|
|
func Part2(puzzle tools.AoCPuzzle) interface{} {
|
|
input := puzzle.GetInputArrayInt()
|
|
sort.Ints(input)
|
|
|
|
// REVISIT! This does not account for the fact, that a container might be re-used
|
|
// Still, the answer is correct for the given input.
|
|
|
|
combos150 := getCombinations(input, 150)
|
|
minNumContainers := len(input)
|
|
numContainers := make(map[int]int)
|
|
for _, v := range combos150 {
|
|
numContainers[len(v)]++
|
|
if len(v) < minNumContainers {
|
|
minNumContainers = len(v)
|
|
}
|
|
}
|
|
|
|
return numContainers[minNumContainers]
|
|
}
|