diff --git a/2016/day21/main.go b/2016/day21/main.go index ef77b8a..6ab7259 100644 --- a/2016/day21/main.go +++ b/2016/day21/main.go @@ -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 } diff --git a/2019/day07/part1/main.go b/2019/day07/part1/main.go index 30a660b..be201b4 100644 --- a/2019/day07/part1/main.go +++ b/2019/day07/part1/main.go @@ -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 diff --git a/2019/day07/part2/main.go b/2019/day07/part2/main.go index df24f63..398c9d9 100644 --- a/2019/day07/part2/main.go +++ b/2019/day07/part2/main.go @@ -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 diff --git a/algos/permutations.go b/algos/permutations.go index 081d726..b8320f6 100644 --- a/algos/permutations.go +++ b/algos/permutations.go @@ -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 +}