mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-06-07 12:45:10 +02:00
part 1 complete but i didn't make permutations myself
This commit is contained in:
+15
-2
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"./intcode"
|
||||
"./permutations"
|
||||
)
|
||||
|
||||
@@ -11,7 +12,7 @@ func main() {
|
||||
// output of each amp is input of next amp
|
||||
// final output is from amp #5 / E to thrusters
|
||||
|
||||
// input := []int{3, 8, 1001, 8, 10, 8, 105, 1, 0, 0, 21, 42, 55, 64, 77, 94, 175, 256, 337, 418, 99999, 3, 9, 102, 4, 9, 9, 1001, 9, 5, 9, 102, 2, 9, 9, 101, 3, 9, 9, 4, 9, 99, 3, 9, 102, 2, 9, 9, 101, 5, 9, 9, 4, 9, 99, 3, 9, 1002, 9, 4, 9, 4, 9, 99, 3, 9, 102, 4, 9, 9, 101, 5, 9, 9, 4, 9, 99, 3, 9, 102, 5, 9, 9, 1001, 9, 3, 9, 1002, 9, 5, 9, 4, 9, 99, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 101, 1, 9, 9, 4, 9, 3, 9, 1001, 9, 1, 9, 4, 9, 3, 9, 101, 1, 9, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 101, 1, 9, 9, 4, 9, 3, 9, 101, 2, 9, 9, 4, 9, 3, 9, 1001, 9, 2, 9, 4, 9, 3, 9, 101, 1, 9, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 99, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 1001, 9, 2, 9, 4, 9, 3, 9, 1001, 9, 1, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 101, 2, 9, 9, 4, 9, 3, 9, 1001, 9, 1, 9, 4, 9, 3, 9, 1001, 9, 1, 9, 4, 9, 99, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 101, 2, 9, 9, 4, 9, 3, 9, 101, 1, 9, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 1001, 9, 2, 9, 4, 9, 3, 9, 101, 1, 9, 9, 4, 9, 3, 9, 101, 1, 9, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 99, 3, 9, 101, 2, 9, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 1001, 9, 2, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 1001, 9, 2, 9, 4, 9, 3, 9, 101, 2, 9, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 101, 1, 9, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 99, 3, 9, 1001, 9, 2, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 101, 1, 9, 9, 4, 9, 3, 9, 101, 1, 9, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 1001, 9, 1, 9, 4, 9, 99}
|
||||
input := []int{3, 8, 1001, 8, 10, 8, 105, 1, 0, 0, 21, 42, 55, 64, 77, 94, 175, 256, 337, 418, 99999, 3, 9, 102, 4, 9, 9, 1001, 9, 5, 9, 102, 2, 9, 9, 101, 3, 9, 9, 4, 9, 99, 3, 9, 102, 2, 9, 9, 101, 5, 9, 9, 4, 9, 99, 3, 9, 1002, 9, 4, 9, 4, 9, 99, 3, 9, 102, 4, 9, 9, 101, 5, 9, 9, 4, 9, 99, 3, 9, 102, 5, 9, 9, 1001, 9, 3,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, 1001, 9, 2, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 101, 1, 9, 9, 4, 9, 3, 9, 101, 1, 9, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 1001, 9, 1, 9, 4, 9, 99}
|
||||
|
||||
// input := []int{3, 15, 3, 16, 1002, 16, 10, 16, 1, 16, 15, 15, 4, 15, 99, 0, 0}
|
||||
// void return value
|
||||
@@ -23,6 +24,18 @@ func main() {
|
||||
// fmt.Println("output of A amp run", intcode.RunDiagnostics(input, 0, 4321)) // outputs 43210
|
||||
|
||||
// create all permutations of 0, 1, 2, 3, 4
|
||||
perms := permutations.CreatePermutations(5)
|
||||
perms := permutations.CreatePermutations()
|
||||
fmt.Println(perms)
|
||||
|
||||
highestReturn := 0
|
||||
for _, onePerm := range perms {
|
||||
lastOutput := 0
|
||||
for _, firstInput := range onePerm {
|
||||
lastOutput = intcode.RunDiagnostics(input, firstInput, lastOutput)
|
||||
}
|
||||
if lastOutput > highestReturn {
|
||||
highestReturn = lastOutput
|
||||
}
|
||||
}
|
||||
fmt.Println("highestReturn is", highestReturn)
|
||||
}
|
||||
|
||||
@@ -1,26 +1,59 @@
|
||||
package permutations
|
||||
|
||||
// CreatePermutations will return a 2D slice containing permutations from zero to the inputted int
|
||||
func CreatePermutations(size int) []string {
|
||||
result := make([]string, 0)
|
||||
// func CreatePermutations(size int) [][]int {
|
||||
// result := make([][]int, 0)
|
||||
|
||||
pointerToResult := &result
|
||||
onePerm(pointerToResult, "01234")
|
||||
// helper := func(digits []int, builder []int) {
|
||||
|
||||
// newDigits := make([]int, len(digits))
|
||||
// copy(newDigits, digits)
|
||||
|
||||
// if len(digits) == 0 {
|
||||
// // use the reference point in memory
|
||||
// result = append(result, builder)
|
||||
// } else {
|
||||
// for index, digit := range newDigits {
|
||||
// fmt.Println("append", append(newDigits[:index], newDigits[index+1:]...))
|
||||
// fmt.Println("builder", append(builder, digit))
|
||||
// helper(append(newDigits[:index], newDigits[index+1:]...), append(builder, digit))
|
||||
// // fmt.Println("looping")
|
||||
// }
|
||||
// }
|
||||
|
||||
// }
|
||||
|
||||
// helper([]int{0, 1, 2, 3, 4}, make([]int, 0))
|
||||
|
||||
// return result
|
||||
// }
|
||||
|
||||
func nextPerm(p []int) {
|
||||
for i := len(p) - 1; i >= 0; i-- {
|
||||
if i == 0 || p[i] < len(p)-i-1 {
|
||||
p[i]++
|
||||
return
|
||||
}
|
||||
p[i] = 0
|
||||
}
|
||||
}
|
||||
|
||||
func getPerm(orig, p []int) []int {
|
||||
result := append([]int{}, orig...)
|
||||
for i, v := range p {
|
||||
result[i], result[i+v] = result[i+v], result[i]
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func onePerm(resultSlice *[]string, substring string) {
|
||||
// if len(*resultSlice) == 0 {
|
||||
// for i := 0; i < len(substring); i++ {
|
||||
// *resultSlice = append(*resultSlice, string(substring[i]))
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
// for i := 0; i < len(*resultSlice); i++ {
|
||||
// newSubstring := *resultSlice[i]
|
||||
// }
|
||||
// modify the actual data @ the location in memory
|
||||
// fmt.Println(resultSlice, *resultSlice)
|
||||
// CreatePermutations docz
|
||||
func CreatePermutations() [][]int {
|
||||
orig := []int{0, 1, 2, 3, 4}
|
||||
results := make([][]int, 0)
|
||||
|
||||
for p := make([]int, len(orig)); p[0] < len(p); nextPerm(p) {
|
||||
// fmt.Println(getPerm(orig, p))
|
||||
results = append(results, getPerm(orig, p))
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user