mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-06-07 20:53:30 +02:00
update perms generating algo
This commit is contained in:
@@ -1,8 +1,3 @@
|
|||||||
/*
|
|
||||||
Intcode struct is defined within this file
|
|
||||||
MakePermutations is in the util package as that will likely be reused
|
|
||||||
*/
|
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -27,12 +22,12 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Make perms via a util function
|
// Make perms via a util function
|
||||||
perms := algos.MakePermutations([]int{0, 1, 2, 3, 4})
|
perms := algos.MakeIntPermutations([]int{0, 1, 2, 3, 4})
|
||||||
|
|
||||||
// iterate over all perms and run through a single pass of the Amps
|
// 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
|
// if the final output (from Amp E) is higher, update the highestOutput variable
|
||||||
highestOutput := 0
|
highestOutput := 0
|
||||||
for _, perm := range *perms {
|
for _, perm := range perms {
|
||||||
// initialize 5 computers
|
// initialize 5 computers
|
||||||
ampA := MakeComputer(inputNumbers, perm[0])
|
ampA := MakeComputer(inputNumbers, perm[0])
|
||||||
ampB := MakeComputer(inputNumbers, perm[1])
|
ampB := MakeComputer(inputNumbers, perm[1])
|
||||||
|
|||||||
@@ -1,8 +1,3 @@
|
|||||||
/*
|
|
||||||
Intcode struct is defined within this file
|
|
||||||
MakePermutations is in the util package as that will likely be reused
|
|
||||||
*/
|
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -27,12 +22,12 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Make perms via a util function
|
// Make perms via a util function
|
||||||
perms := algos.MakePermutations([]int{5, 6, 7, 8, 9})
|
perms := algos.MakeIntPermutations([]int{5, 6, 7, 8, 9})
|
||||||
|
|
||||||
// iterate over all perms and run through a single pass of the Amps
|
// 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
|
// if the final output (from Amp E) is higher, update the highestOutput variable
|
||||||
highestOutput := 0
|
highestOutput := 0
|
||||||
for _, perm := range *perms {
|
for _, perm := range perms {
|
||||||
// initialize 5 computers
|
// initialize 5 computers
|
||||||
ampA := MakeComputer(inputNumbers, perm[0])
|
ampA := MakeComputer(inputNumbers, perm[0])
|
||||||
ampB := MakeComputer(inputNumbers, perm[1])
|
ampB := MakeComputer(inputNumbers, perm[1])
|
||||||
|
|||||||
@@ -1,16 +1,12 @@
|
|||||||
/*
|
|
||||||
Intcode struct is defined within this file
|
|
||||||
MakePermutations is in the util package as that will likely be reused
|
|
||||||
*/
|
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/alexchao26/advent-of-code-go/util"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/alexchao26/advent-of-code-go/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|||||||
@@ -1,16 +1,12 @@
|
|||||||
/*
|
|
||||||
Intcode struct is defined within this file
|
|
||||||
MakePermutations is in the util package as that will likely be reused
|
|
||||||
*/
|
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/alexchao26/advent-of-code-go/util"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/alexchao26/advent-of-code-go/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|||||||
@@ -1,16 +1,12 @@
|
|||||||
/*
|
|
||||||
Intcode struct is defined within this file
|
|
||||||
MakePermutations is in the util package as that will likely be reused
|
|
||||||
*/
|
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/alexchao26/advent-of-code-go/util"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/alexchao26/advent-of-code-go/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|||||||
+13
-24
@@ -2,45 +2,34 @@ package algos
|
|||||||
|
|
||||||
import "strings"
|
import "strings"
|
||||||
|
|
||||||
// MakePermutations will make all permutations of the numbers input
|
// MakeIntPermutations will make all permutations of the numbers input
|
||||||
// returns a pointer to avoid copying a large number of permutations
|
func MakeIntPermutations(numbers []int) [][]int {
|
||||||
func MakePermutations(numbers []int) *[][]int {
|
return recurseInts(numbers, 0)
|
||||||
result := make([][]int, 0)
|
|
||||||
|
|
||||||
swapRecurseBacktrack(numbers, 0, &result)
|
|
||||||
|
|
||||||
return &result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// helper function to generate permutations
|
// helper function to generate permutations
|
||||||
func swapRecurseBacktrack(numbers []int, startIndex int, results *[][]int) {
|
func recurseInts(numbers []int, startIndex int) [][]int {
|
||||||
if startIndex == len(numbers) {
|
if startIndex == len(numbers) {
|
||||||
// make a copy of the perm
|
// makes a copy using append
|
||||||
perm := make([]int, len(numbers))
|
return [][]int{append([]int{}, numbers...)}
|
||||||
copy(perm, numbers)
|
|
||||||
|
|
||||||
// assign the value at the pointer results to the appended slice (dereferenced) results w/ perm
|
|
||||||
*results = append(*results, perm)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var perms [][]int
|
||||||
for i := startIndex; i < len(numbers); i++ {
|
for i := startIndex; i < len(numbers); i++ {
|
||||||
// swap numbers
|
// swap, append perms, backtrack
|
||||||
numbers[startIndex], numbers[i] = numbers[i], numbers[startIndex]
|
numbers[startIndex], numbers[i] = numbers[i], numbers[startIndex]
|
||||||
|
perms = append(perms, recurseInts(numbers, startIndex+1)...)
|
||||||
// recurse with startIndex incremented
|
|
||||||
swapRecurseBacktrack(numbers, startIndex+1, results)
|
|
||||||
|
|
||||||
// backtrack
|
|
||||||
numbers[startIndex], numbers[i] = numbers[i], numbers[startIndex]
|
numbers[startIndex], numbers[i] = numbers[i], numbers[startIndex]
|
||||||
}
|
}
|
||||||
|
return perms
|
||||||
}
|
}
|
||||||
|
|
||||||
// MakeStringPermutations generates all permutations for a given string
|
// MakeStringPermutations generates all permutations for a given string
|
||||||
func MakeStringPermutations(str string) []string {
|
func MakeStringPermutations(str string) []string {
|
||||||
return recurse(strings.Split(str, ""), 0)
|
return recurseStrings(strings.Split(str, ""), 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func recurse(sli []string, index int) []string {
|
func recurseStrings(sli []string, index int) []string {
|
||||||
if index == len(sli) {
|
if index == len(sli) {
|
||||||
return []string{strings.Join(sli, "")}
|
return []string{strings.Join(sli, "")}
|
||||||
}
|
}
|
||||||
@@ -48,7 +37,7 @@ func recurse(sli []string, index int) []string {
|
|||||||
var perms []string
|
var perms []string
|
||||||
for i := index; i < len(sli); i++ {
|
for i := index; i < len(sli); i++ {
|
||||||
sli[i], sli[index] = sli[index], sli[i]
|
sli[i], sli[index] = sli[index], sli[i]
|
||||||
perms = append(perms, recurse(sli, index+1)...)
|
perms = append(perms, recurseStrings(sli, index+1)...)
|
||||||
sli[i], sli[index] = sli[index], sli[i]
|
sli[i], sli[index] = sli[index], sli[i]
|
||||||
}
|
}
|
||||||
return perms
|
return perms
|
||||||
|
|||||||
Reference in New Issue
Block a user