:work: Working with part 2 of day 7

This commit is contained in:
Alphyron 2019-12-10 08:15:43 +01:00
parent 0b74b2411b
commit 1594ddd9ab

View File

@ -92,7 +92,7 @@ func RunAmplification(list []int, phases []int) int {
inputs = append(inputs, inputStr)
}
output, err, _ := ParseArray(wip, inputs)
output, err, _, _ := ParseArray(wip, inputs, 0)
if err != nil {
log.Printf("Problem at Amplification: %d", i+1)
@ -109,17 +109,15 @@ func RunRecursiveAmplification(list []int, phases []int) int {
signal := []string{"0"}
amplifier := make([][]int, 0)
startIndex := make([]int, 0)
for i := 0; i < 5; i++ {
log.Println("============")
log.Println(i)
if len(amplifier) <= i {
log.Println(len(amplifier))
log.Println("APPPEND")
wip := make([]int, len(list))
copy(wip, list)
amplifier = append(amplifier, wip)
log.Println(len(amplifier))
startIndex = append(startIndex, 0)
}
inputs := make([]int, 0)
@ -129,7 +127,8 @@ func RunRecursiveAmplification(list []int, phases []int) int {
inputs = append(inputs, inputStr)
}
output, err, cmdSuccess := ParseArray(amplifier[i], inputs)
output, err, cmdSuccess, ind := ParseArray(amplifier[i], inputs, startIndex[i])
startIndex[i] = ind
if err != nil {
log.Printf("Problem at Amplification: %d", i+1)
@ -138,19 +137,20 @@ func RunRecursiveAmplification(list []int, phases []int) int {
signal = output
if cmdSuccess {
break
}
if i == 4 {
i = -1
if cmdSuccess {
break
}
}
}
sigInt, _ := strconv.Atoi(signal[len(signal)-1])
fmt.Println(len(signal))
return sigInt
}
func ParseArray(list []int, inputs []int) ([]string, error, bool) {
func ParseArray(list []int, inputs []int, startIndex int) ([]string, error, bool, int) {
var (
output []string
)
@ -158,14 +158,11 @@ func ParseArray(list []int, inputs []int) ([]string, error, bool) {
inputIndex := 0
output = make([]string, 0)
for i := 0; i < len(list); i += 4 {
//log.Println(strconv.Itoa(i) + " -> " + strconv.Itoa(list[i]))
log.Println("=================")
for i := startIndex; i < len(list); i += 4 {
log.Println(strconv.Itoa(i) + " -> " + strconv.Itoa(list[i]))
cmd := list[i]
if cmd == 99 {
return output, nil, true
}
sum := 0
mode1 := 0
@ -188,6 +185,10 @@ func ParseArray(list []int, inputs []int) ([]string, error, bool) {
}
}
if cmd == 99 {
return output, nil, true, -2
}
if cmd == 1 {
num1 := -1
num2 := -1
@ -226,10 +227,7 @@ func ParseArray(list []int, inputs []int) ([]string, error, bool) {
inputIndex++
list[list[i+1]] = input
} else {
input := output[0]
output = output[1:]
inputInt, _ := strconv.Atoi(input)
list[list[i+1]] = inputInt
return output, nil, false, i
}
i = i - 2
} else if cmd == 4 {
@ -312,16 +310,12 @@ func ParseArray(list []int, inputs []int) ([]string, error, bool) {
list[list[i+3]] = 0
}
} else {
return output, errors.New("Wrong Command (" + strconv.Itoa(list[i]) + ") at position " + strconv.Itoa(i)), false
return output, errors.New("Wrong Command (" + strconv.Itoa(list[i]) + ") at position " + strconv.Itoa(i)), false, -1
}
}
if len(inputs) > inputIndex {
output = append(strings.Fields(fmt.Sprint(inputs[inputIndex:])), output...)
}
log.Println("NONE")
return output, nil, false
return output, nil, false, -1
}
func CountDigits(i int) int {