From a0415caa4ace61ab8e8dcd17d1e3416703da41e7 Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Tue, 29 Dec 2020 10:36:17 +0100 Subject: [PATCH] day17 solved --- day17/day.go | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/day17/day.go b/day17/day.go index ed0c308..daf999e 100644 --- a/day17/day.go +++ b/day17/day.go @@ -1,7 +1,6 @@ package day17 import ( - "fmt" "sort" "tools" ) @@ -11,11 +10,14 @@ func getCombinations(containers []int, sum int) (combinations [][]int) { if c > sum { break } else if c == sum { - return [][]int{{c}} + 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:], sum-c) + subCombinations := getCombinations(containers[i+1:], sum-c) for _, v := range subCombinations { subCombination := append([]int{c}, v...) combinations = append(combinations, subCombination) @@ -28,13 +30,27 @@ func Part1(puzzle tools.AoCPuzzle) interface{} { input := puzzle.GetInputArrayInt() sort.Ints(input) - combos25 := getCombinations(input, 25) - fmt.Println(combos25) - //combos150 := getCombinations(input, 150) + combos150 := getCombinations(input, 150) - return len(combos25) + return len(combos150) } func Part2(puzzle tools.AoCPuzzle) interface{} { - return 0 + 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] }