mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-05-18 19:13:27 +02:00
2015-day24 code pulled into algos and mathy packages
This commit is contained in:
+12
-31
@@ -7,9 +7,10 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alexchao26/advent-of-code-go/mathy"
|
// first solution to use all the packages?!
|
||||||
|
"github.com/alexchao26/advent-of-code-go/algos"
|
||||||
"github.com/alexchao26/advent-of-code-go/cast"
|
"github.com/alexchao26/advent-of-code-go/cast"
|
||||||
|
"github.com/alexchao26/advent-of-code-go/mathy"
|
||||||
"github.com/alexchao26/advent-of-code-go/util"
|
"github.com/alexchao26/advent-of-code-go/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -36,11 +37,12 @@ func balancingPackages(input string, part int) int {
|
|||||||
target = sum / 4
|
target = sum / 4
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// make the gross assumption that if a group is found, and adds up to the
|
||||||
|
// target, the remaining elements will be able to be split into two equal
|
||||||
|
// groups. This is not always true, but the inputs are nicely generated
|
||||||
var individualGroups [][]int
|
var individualGroups [][]int
|
||||||
for groupLen := 2; len(individualGroups) == 0; groupLen++ {
|
for groupLen := 2; len(individualGroups) == 0; groupLen++ {
|
||||||
for i := 0; i < len(nums); i++ {
|
individualGroups = algos.CombinationsInts(nums, groupLen)
|
||||||
individualGroups = append(individualGroups, combinations(nums, groupLen, []int{}, i)...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// validate that a combination adds up to the target sum
|
// validate that a combination adds up to the target sum
|
||||||
var validGroups [][]int
|
var validGroups [][]int
|
||||||
@@ -59,33 +61,12 @@ func balancingPackages(input string, part int) int {
|
|||||||
return quantumEntanglement(individualGroups[0])
|
return quantumEntanglement(individualGroups[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
func combinations(nums []int, length int, combo []int, index int) [][]int {
|
|
||||||
if len(combo) == length {
|
|
||||||
return [][]int{append([]int{}, combo...)}
|
|
||||||
}
|
|
||||||
var combos [][]int
|
|
||||||
for i := index; i < len(nums); i++ {
|
|
||||||
combo = append(combo, nums[i])
|
|
||||||
with := combinations(nums, length, combo, i+1)
|
|
||||||
combos = append(combos, with...)
|
|
||||||
// backtrack
|
|
||||||
combo = combo[:len(combo)-1]
|
|
||||||
}
|
|
||||||
|
|
||||||
return combos
|
|
||||||
}
|
|
||||||
|
|
||||||
func sortGroups(groups [][]int) [][]int {
|
func sortGroups(groups [][]int) [][]int {
|
||||||
sort.Slice(groups, func(i, j int) bool {
|
clone := append([][]int{}, groups...)
|
||||||
return quantumEntanglement(groups[i]) < quantumEntanglement(groups[j])
|
sort.Slice(clone, func(i, j int) bool {
|
||||||
|
return quantumEntanglement(clone[i]) < quantumEntanglement(clone[j])
|
||||||
})
|
})
|
||||||
return groups
|
return clone
|
||||||
}
|
}
|
||||||
|
|
||||||
func quantumEntanglement(nums []int) int {
|
var quantumEntanglement = mathy.MultiplyIntSlice
|
||||||
prod := 1
|
|
||||||
for _, n := range nums {
|
|
||||||
prod *= n
|
|
||||||
}
|
|
||||||
return prod
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package algos
|
||||||
|
|
||||||
|
// CombinationsInts returns all combinations of an input slice of a given length
|
||||||
|
func CombinationsInts(nums []int, targetLength int) [][]int {
|
||||||
|
if targetLength > len(nums) {
|
||||||
|
panic("target length is greated than length of input slice")
|
||||||
|
}
|
||||||
|
|
||||||
|
var combos [][]int
|
||||||
|
// loop over starting points in the nums slice
|
||||||
|
for i := 0; i < len(nums); i++ {
|
||||||
|
combos = append(combos, helperCombinationsInts(nums[i:], targetLength, []int{})...)
|
||||||
|
}
|
||||||
|
|
||||||
|
return combos
|
||||||
|
}
|
||||||
|
|
||||||
|
func helperCombinationsInts(nums []int, length int, current []int) [][]int {
|
||||||
|
if len(current) == length {
|
||||||
|
return [][]int{append([]int{}, current...)}
|
||||||
|
}
|
||||||
|
var combos [][]int
|
||||||
|
for i := range nums {
|
||||||
|
// add value onto the current combo
|
||||||
|
current = append(current, nums[i])
|
||||||
|
|
||||||
|
// recurse with only the remaining numbers, then append any valid combos
|
||||||
|
// that were found
|
||||||
|
recurseResult := helperCombinationsInts(nums[i+1:], length, current)
|
||||||
|
combos = append(combos, recurseResult...)
|
||||||
|
|
||||||
|
// backtrack
|
||||||
|
current = current[:len(current)-1]
|
||||||
|
}
|
||||||
|
|
||||||
|
return combos
|
||||||
|
}
|
||||||
@@ -33,3 +33,11 @@ func SumIntSlice(nums []int) int {
|
|||||||
}
|
}
|
||||||
return sum
|
return sum
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func MultiplyIntSlice(nums []int) int {
|
||||||
|
product := 1
|
||||||
|
for _, n := range nums {
|
||||||
|
product *= n
|
||||||
|
}
|
||||||
|
return product
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user