:work: Working with part 2 of day 7
This commit is contained in:
		
							parent
							
								
									0fbbe517c6
								
							
						
					
					
						commit
						0b74b2411b
					
				
							
								
								
									
										42
									
								
								day7/main.go
									
									
									
									
									
								
							
							
						
						
									
										42
									
								
								day7/main.go
									
									
									
									
									
								
							@ -3,6 +3,7 @@ package main
 | 
			
		||||
import (
 | 
			
		||||
	"bufio"
 | 
			
		||||
	"errors"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"log"
 | 
			
		||||
	"math"
 | 
			
		||||
	"os"
 | 
			
		||||
@ -62,7 +63,7 @@ func SolvePuzzle1(list []int) {
 | 
			
		||||
 | 
			
		||||
func SolvePuzzle2(list []int) {
 | 
			
		||||
 | 
			
		||||
	rawr := []int{9, 8, 7, 6, 5}
 | 
			
		||||
	rawr := []int{5, 6, 7, 8, 9}
 | 
			
		||||
	combinations := permutations(rawr)
 | 
			
		||||
	highestOutput := math.MinInt32
 | 
			
		||||
	//highestInput := ""
 | 
			
		||||
@ -107,10 +108,19 @@ func RunAmplification(list []int, phases []int) int {
 | 
			
		||||
func RunRecursiveAmplification(list []int, phases []int) int {
 | 
			
		||||
	signal := []string{"0"}
 | 
			
		||||
 | 
			
		||||
	i := 0
 | 
			
		||||
	for true {
 | 
			
		||||
		wip := make([]int, len(list))
 | 
			
		||||
		copy(wip, list)
 | 
			
		||||
	amplifier := make([][]int, 0)
 | 
			
		||||
 | 
			
		||||
	for i := 0; i < 5; i++ {
 | 
			
		||||
		log.Println("============")
 | 
			
		||||
		log.Println(i)
 | 
			
		||||
		if len(amplifier) <= i {
 | 
			
		||||
			log.Println(len(amplifier))
 | 
			
		||||
			wip := make([]int, len(list))
 | 
			
		||||
			copy(wip, list)
 | 
			
		||||
			amplifier = append(amplifier, wip)
 | 
			
		||||
			log.Println(len(amplifier))
 | 
			
		||||
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		inputs := make([]int, 0)
 | 
			
		||||
		inputs = append(inputs, phases[i])
 | 
			
		||||
@ -119,7 +129,7 @@ func RunRecursiveAmplification(list []int, phases []int) int {
 | 
			
		||||
			inputs = append(inputs, inputStr)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		output, err, cmdSuccess := ParseArray(wip, inputs)
 | 
			
		||||
		output, err, cmdSuccess := ParseArray(amplifier[i], inputs)
 | 
			
		||||
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Printf("Problem at Amplification: %d", i+1)
 | 
			
		||||
@ -128,20 +138,23 @@ func RunRecursiveAmplification(list []int, phases []int) int {
 | 
			
		||||
 | 
			
		||||
		signal = output
 | 
			
		||||
 | 
			
		||||
		if cmdSuccess && i == 4 {
 | 
			
		||||
		if cmdSuccess {
 | 
			
		||||
			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) {
 | 
			
		||||
func ParseArray(list []int, inputs []int) ([]string, error, bool) {
 | 
			
		||||
	var (
 | 
			
		||||
		output []string
 | 
			
		||||
	)
 | 
			
		||||
 | 
			
		||||
	inputIndex := 0
 | 
			
		||||
	output = make([]string, 0)
 | 
			
		||||
 | 
			
		||||
@ -152,6 +165,7 @@ func ParseArray(list []int, inputs []int) (output []string, err error, cmdEnd bo
 | 
			
		||||
		if cmd == 99 {
 | 
			
		||||
			return output, nil, true
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		sum := 0
 | 
			
		||||
 | 
			
		||||
		mode1 := 0
 | 
			
		||||
@ -207,8 +221,6 @@ func ParseArray(list []int, inputs []int) (output []string, err error, cmdEnd bo
 | 
			
		||||
			sum = num1 * num2
 | 
			
		||||
			list[list[i+3]] = sum
 | 
			
		||||
		} else if cmd == 3 {
 | 
			
		||||
			//log.Println(i)
 | 
			
		||||
 | 
			
		||||
			if len(inputs) > inputIndex {
 | 
			
		||||
				input := inputs[inputIndex]
 | 
			
		||||
				inputIndex++
 | 
			
		||||
@ -226,7 +238,7 @@ func ParseArray(list []int, inputs []int) (output []string, err error, cmdEnd bo
 | 
			
		||||
			} else {
 | 
			
		||||
				output = append(output, strconv.Itoa(list[list[i+1]]))
 | 
			
		||||
			}
 | 
			
		||||
			i = i - 2
 | 
			
		||||
			i -= 2
 | 
			
		||||
		} else if cmd == 5 {
 | 
			
		||||
			num1 := -1
 | 
			
		||||
			if mode1 == 1 {
 | 
			
		||||
@ -303,6 +315,12 @@ func ParseArray(list []int, inputs []int) (output []string, err error, cmdEnd bo
 | 
			
		||||
			return output, errors.New("Wrong Command (" + strconv.Itoa(list[i]) + ") at position " + strconv.Itoa(i)), false
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if len(inputs) > inputIndex {
 | 
			
		||||
		output = append(strings.Fields(fmt.Sprint(inputs[inputIndex:])), output...)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	log.Println("NONE")
 | 
			
		||||
	return output, nil, false
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										45
									
								
								day7/main_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								day7/main_test.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,45 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"testing"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestExampleRecursive1(t *testing.T) {
 | 
			
		||||
	text := "3,26,1001,26,-4,26,3,27,1002,27,2,27,1,27,26,27,4,27,1001,28,-1,28,1005,28,6,99,0,0,5"
 | 
			
		||||
	stringarr := strings.Split(text, ",")
 | 
			
		||||
	var t2 = []int{}
 | 
			
		||||
 | 
			
		||||
	for _, i := range stringarr {
 | 
			
		||||
		j, err := strconv.Atoi(i)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			panic(err)
 | 
			
		||||
		}
 | 
			
		||||
		t2 = append(t2, j)
 | 
			
		||||
	}
 | 
			
		||||
	goal := RunRecursiveAmplification(t2, []int{9, 8, 7, 6, 5})
 | 
			
		||||
	success := 139629729
 | 
			
		||||
	if goal != success {
 | 
			
		||||
		t.Errorf("Example 1 was incorrect, got: {%d}, want: {%d}.", goal, success)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestExampleRecursive2(t *testing.T) {
 | 
			
		||||
	text := "3,52,1001,52,-5,52,3,53,1,52,56,54,1007,54,5,55,1005,55,26,1001,54,-5,54,1105,1,12,1,53,54,53,1008,54,0,55,1001,55,1,55,2,53,55,53,4,53,1001,56,-1,56,1005,56,6,99,0,0,0,0,10"
 | 
			
		||||
	stringarr := strings.Split(text, ",")
 | 
			
		||||
	var t2 = []int{}
 | 
			
		||||
 | 
			
		||||
	for _, i := range stringarr {
 | 
			
		||||
		j, err := strconv.Atoi(i)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			panic(err)
 | 
			
		||||
		}
 | 
			
		||||
		t2 = append(t2, j)
 | 
			
		||||
	}
 | 
			
		||||
	goal := RunRecursiveAmplification(t2, []int{9, 7, 8, 5, 6})
 | 
			
		||||
	success := 18216
 | 
			
		||||
	if goal != success {
 | 
			
		||||
		t.Errorf("Example 2 was incorrect, got: {%d}, want: {%d}.", goal, success)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user