:work: Part 2 of day 7 still not working
This commit is contained in:
parent
194981f938
commit
da97805203
165
day7/main.go
165
day7/main.go
@ -3,8 +3,8 @@ package main
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"math"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
@ -34,21 +34,123 @@ func main() {
|
||||
t2 = append(t2, j)
|
||||
}
|
||||
wip := make([]int, len(t2))
|
||||
wip2 := make([]int, len(t2))
|
||||
|
||||
// ========= Finish now, we can solve the task ======
|
||||
copy(wip, t2)
|
||||
err = ParseArray(wip)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
SolvePuzzle1(wip)
|
||||
copy(wip2, t2)
|
||||
SolvePuzzle2(wip2)
|
||||
}
|
||||
|
||||
func ParseArray(list []int) error {
|
||||
func SolvePuzzle1(list []int) {
|
||||
|
||||
rawr := []int{0, 1, 2, 3, 4}
|
||||
combinations := permutations(rawr)
|
||||
highestOutput := math.MinInt32
|
||||
//highestInput := ""
|
||||
|
||||
for _, combi := range combinations {
|
||||
output := RunAmplification(list, []int{int(combi[0]), int(combi[1]), int(combi[2]), int(combi[3]), int(combi[4])})
|
||||
if output > highestOutput {
|
||||
//highestInput = combi
|
||||
highestOutput = output
|
||||
}
|
||||
}
|
||||
log.Println("Part1: " + strconv.Itoa(highestOutput))
|
||||
}
|
||||
|
||||
func SolvePuzzle2(list []int) {
|
||||
|
||||
rawr := []int{9, 8, 7, 6, 5}
|
||||
combinations := permutations(rawr)
|
||||
highestOutput := math.MinInt32
|
||||
//highestInput := ""
|
||||
|
||||
for _, combi := range combinations {
|
||||
output := RunRecursiveAmplification(list, []int{combi[0], combi[1], combi[2], combi[3], combi[4]})
|
||||
if output > highestOutput {
|
||||
//highestInput = combi
|
||||
highestOutput = output
|
||||
}
|
||||
}
|
||||
log.Println("Part2: " + strconv.Itoa(highestOutput))
|
||||
}
|
||||
|
||||
func RunAmplification(list []int, phases []int) int {
|
||||
signal := []string{"0"}
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
wip := make([]int, len(list))
|
||||
copy(wip, list)
|
||||
|
||||
inputs := make([]int, 0)
|
||||
inputs = append(inputs, phases[i])
|
||||
for j := 0; j < len(signal); j++ {
|
||||
inputStr, _ := strconv.Atoi(signal[j])
|
||||
inputs = append(inputs, inputStr)
|
||||
}
|
||||
|
||||
output, err, _ := ParseArray(wip, inputs)
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Problem at Amplification: %d", i+1)
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
signal = output
|
||||
}
|
||||
sigInt, _ := strconv.Atoi(signal[0])
|
||||
return sigInt
|
||||
}
|
||||
|
||||
func RunRecursiveAmplification(list []int, phases []int) int {
|
||||
signal := []string{"0"}
|
||||
|
||||
i := 0
|
||||
for true {
|
||||
wip := make([]int, len(list))
|
||||
copy(wip, list)
|
||||
|
||||
inputs := make([]int, 0)
|
||||
inputs = append(inputs, phases[i])
|
||||
for j := 0; j < len(signal); j++ {
|
||||
inputStr, _ := strconv.Atoi(signal[j])
|
||||
inputs = append(inputs, inputStr)
|
||||
}
|
||||
|
||||
output, err, cmdSuccess := ParseArray(wip, inputs)
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Problem at Amplification: %d", i+1)
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
signal = output
|
||||
|
||||
if cmdSuccess && i == 4 {
|
||||
break
|
||||
}
|
||||
|
||||
if i == 4 {
|
||||
i = -1
|
||||
}
|
||||
i++
|
||||
}
|
||||
sigInt, _ := strconv.Atoi(signal[len(signal)-1])
|
||||
return sigInt
|
||||
}
|
||||
|
||||
func ParseArray(list []int, inputs []int) (output []string, err error, cmdEnd bool) {
|
||||
inputIndex := 0
|
||||
output = make([]string, 0)
|
||||
|
||||
for i := 0; i < len(list); i += 4 {
|
||||
//log.Println(strconv.Itoa(i) + " -> " + strconv.Itoa(list[i]))
|
||||
cmd := list[i]
|
||||
|
||||
if cmd == 99 {
|
||||
return nil
|
||||
return output, nil, true
|
||||
}
|
||||
sum := 0
|
||||
|
||||
@ -105,15 +207,24 @@ func ParseArray(list []int) error {
|
||||
sum = num1 * num2
|
||||
list[list[i+3]] = sum
|
||||
} else if cmd == 3 {
|
||||
var input int
|
||||
fmt.Scanf("%d", &input)
|
||||
//log.Println(i)
|
||||
|
||||
if len(inputs) > inputIndex {
|
||||
input := inputs[inputIndex]
|
||||
inputIndex++
|
||||
list[list[i+1]] = input
|
||||
} else {
|
||||
input := output[0]
|
||||
output = output[1:]
|
||||
inputInt, _ := strconv.Atoi(input)
|
||||
list[list[i+1]] = inputInt
|
||||
}
|
||||
i = i - 2
|
||||
} else if cmd == 4 {
|
||||
if mode1 == 1 {
|
||||
fmt.Print(list[i+1])
|
||||
output = append(output, strconv.Itoa(list[i+1]))
|
||||
} else {
|
||||
fmt.Print(list[list[i+1]])
|
||||
output = append(output, strconv.Itoa(list[list[i+1]]))
|
||||
}
|
||||
i = i - 2
|
||||
} else if cmd == 5 {
|
||||
@ -189,10 +300,10 @@ func ParseArray(list []int) error {
|
||||
list[list[i+3]] = 0
|
||||
}
|
||||
} else {
|
||||
return errors.New("Wrong Command (" + strconv.Itoa(list[i]) + ") at position " + strconv.Itoa(i))
|
||||
return output, errors.New("Wrong Command (" + strconv.Itoa(list[i]) + ") at position " + strconv.Itoa(i)), false
|
||||
}
|
||||
}
|
||||
return nil
|
||||
return output, nil, false
|
||||
}
|
||||
|
||||
func CountDigits(i int) int {
|
||||
@ -203,3 +314,31 @@ func CountDigits(i int) int {
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
func permutations(arr []int) [][]int {
|
||||
var helper func([]int, int)
|
||||
res := [][]int{}
|
||||
|
||||
helper = func(arr []int, n int) {
|
||||
if n == 1 {
|
||||
tmp := make([]int, len(arr))
|
||||
copy(tmp, arr)
|
||||
res = append(res, tmp)
|
||||
} else {
|
||||
for i := 0; i < n; i++ {
|
||||
helper(arr, n-1)
|
||||
if n%2 == 1 {
|
||||
tmp := arr[i]
|
||||
arr[i] = arr[n-1]
|
||||
arr[n-1] = tmp
|
||||
} else {
|
||||
tmp := arr[0]
|
||||
arr[0] = arr[n-1]
|
||||
arr[n-1] = tmp
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
helper(arr, len(arr))
|
||||
return res
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user