mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-05-18 19:13:27 +02:00
algos permutation method names changed to algos.PermuteTYPE
This commit is contained in:
+1
-1
@@ -39,7 +39,7 @@ func part1(input string, starting string, target string) string {
|
||||
return runSteps(starting)
|
||||
}
|
||||
|
||||
for _, p := range algos.MakeStringPermutations(starting) {
|
||||
for _, p := range algos.PermuteString(starting) {
|
||||
if runSteps(p) == target {
|
||||
return p
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ func main() {
|
||||
}
|
||||
|
||||
// Make perms via a util function
|
||||
perms := algos.MakeIntPermutations([]int{0, 1, 2, 3, 4})
|
||||
perms := algos.PermuteIntSlice([]int{0, 1, 2, 3, 4})
|
||||
|
||||
// iterate over all perms and run through a single pass of the Amps
|
||||
// if the final output (from Amp E) is higher, update the highestOutput variable
|
||||
|
||||
@@ -22,7 +22,7 @@ func main() {
|
||||
}
|
||||
|
||||
// Make perms via a util function
|
||||
perms := algos.MakeIntPermutations([]int{5, 6, 7, 8, 9})
|
||||
perms := algos.PermuteIntSlice([]int{5, 6, 7, 8, 9})
|
||||
|
||||
// iterate over all perms and run through a single pass of the Amps
|
||||
// if the final output (from Amp E) is higher, update the highestOutput variable
|
||||
|
||||
+32
-10
@@ -2,13 +2,13 @@ package algos
|
||||
|
||||
import "strings"
|
||||
|
||||
// MakeIntPermutations will make all permutations of the numbers input
|
||||
func MakeIntPermutations(numbers []int) [][]int {
|
||||
return recurseInts(numbers, 0)
|
||||
// PermuteIntSlice will make all permutations of the numbers input
|
||||
func PermuteIntSlice(numbers []int) [][]int {
|
||||
return recurseIntSlice(numbers, 0)
|
||||
}
|
||||
|
||||
// helper function to generate permutations
|
||||
func recurseInts(numbers []int, startIndex int) [][]int {
|
||||
func recurseIntSlice(numbers []int, startIndex int) [][]int {
|
||||
if startIndex == len(numbers) {
|
||||
// makes a copy using append
|
||||
return [][]int{append([]int{}, numbers...)}
|
||||
@@ -18,18 +18,18 @@ func recurseInts(numbers []int, startIndex int) [][]int {
|
||||
for i := startIndex; i < len(numbers); i++ {
|
||||
// swap, append perms, backtrack
|
||||
numbers[startIndex], numbers[i] = numbers[i], numbers[startIndex]
|
||||
perms = append(perms, recurseInts(numbers, startIndex+1)...)
|
||||
perms = append(perms, recurseIntSlice(numbers, startIndex+1)...)
|
||||
numbers[startIndex], numbers[i] = numbers[i], numbers[startIndex]
|
||||
}
|
||||
return perms
|
||||
}
|
||||
|
||||
// MakeStringPermutations generates all permutations for a given string
|
||||
func MakeStringPermutations(str string) []string {
|
||||
return recurseStrings(strings.Split(str, ""), 0)
|
||||
// PermuteString generates all permutations for a given string
|
||||
func PermuteString(str string) []string {
|
||||
return recurseString(strings.Split(str, ""), 0)
|
||||
}
|
||||
|
||||
func recurseStrings(sli []string, index int) []string {
|
||||
func recurseString(sli []string, index int) []string {
|
||||
if index == len(sli) {
|
||||
return []string{strings.Join(sli, "")}
|
||||
}
|
||||
@@ -37,8 +37,30 @@ func recurseStrings(sli []string, index int) []string {
|
||||
var perms []string
|
||||
for i := index; i < len(sli); i++ {
|
||||
sli[i], sli[index] = sli[index], sli[i]
|
||||
perms = append(perms, recurseStrings(sli, index+1)...)
|
||||
perms = append(perms, recurseString(sli, index+1)...)
|
||||
sli[i], sli[index] = sli[index], sli[i]
|
||||
}
|
||||
return perms
|
||||
}
|
||||
|
||||
// PermuteStringSlice will make all permutations of a string slice
|
||||
func PermuteStringSlice(in []string) [][]string {
|
||||
return recurseStringsSlice(in, 0)
|
||||
}
|
||||
|
||||
// helper function to generate permutations
|
||||
func recurseStringsSlice(in []string, startIndex int) [][]string {
|
||||
if startIndex == len(in) {
|
||||
// makes a copy using append
|
||||
return [][]string{append([]string{}, in...)}
|
||||
}
|
||||
|
||||
var perms [][]string
|
||||
for i := startIndex; i < len(in); i++ {
|
||||
// swap, append perms, backtrack
|
||||
in[startIndex], in[i] = in[i], in[startIndex]
|
||||
perms = append(perms, recurseStringsSlice(in, startIndex+1)...)
|
||||
in[startIndex], in[i] = in[i], in[startIndex]
|
||||
}
|
||||
return perms
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user