From 6cbca15c3d87afba4b9c3d57fe4ca9707dc33770 Mon Sep 17 00:00:00 2001 From: Karl Spickermann Date: Thu, 22 Dec 2022 03:14:11 +0100 Subject: [PATCH] Day21 --- day21/day21.go | 93 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 1 deletion(-) diff --git a/day21/day21.go b/day21/day21.go index 4693ef3..da0fdb2 100644 --- a/day21/day21.go +++ b/day21/day21.go @@ -3,6 +3,7 @@ package main import ( "AOC2022/helper" "fmt" + "math" "strconv" "strings" ) @@ -17,6 +18,97 @@ type Operation struct { func main() { //args := os.Args[1:] lines := helper.ReadTextFile("day21/input") + //part1(lines) + fmt.Println(part2(lines)) + fmt.Println(testNumber(lines, 3403989691757)) + +} + +func part2(lines []string) int { + currentBestDifference := 99999999999999 + currentI := 0 + for currentBestDifference > 0 { + startingDifference := testNumber(lines, currentI) + fmt.Println(startingDifference) + increaseDifference := testNumber(lines, currentI+1) + reduceDifference := testNumber(lines, currentI-1) + if increaseDifference < reduceDifference { + fmt.Println("Up we go") + currentI, currentBestDifference = checkForward(lines, currentBestDifference, currentI) + } else { + fmt.Println("Down we go") + currentI, currentBestDifference = checkBackward(lines, currentBestDifference, currentI) + } + } + fmt.Println(currentI) + return 0 +} + +func checkForward(lines []string, increaseDifference int, startI int) (int, int) { + i := startI + increaser := 1000000000 + for increaser > 0 { + differenceDoesntDecreaseAnymore := false + for !differenceDoesntDecreaseAnymore { + i += increaser + tmpDifference := testNumber(lines, i) + if tmpDifference < increaseDifference { + increaseDifference = tmpDifference + } else { + i -= increaser + differenceDoesntDecreaseAnymore = true + } + } + increaser /= 10 + } + return i, increaseDifference +} + +func checkBackward(lines []string, increaseDifference int, startI int) (int, int) { + i := startI + increaser := 1000000000 + for increaser > 0 { + differenceDoesntDecreaseAnymore := false + for !differenceDoesntDecreaseAnymore { + i -= increaser + tmpDifference := testNumber(lines, i) + if tmpDifference < increaseDifference { + increaseDifference = tmpDifference + } else { + i += increaser + differenceDoesntDecreaseAnymore = true + } + } + increaser /= 10 + } + return i, increaseDifference +} + +func testNumber(lines []string, testnumber int) int { + apes := make(map[string]Operation) + apesWithValue := []string{} + apesWithOutValue := []string{} + for _, line := range lines { + id, operation := getApe(line) + apes[id] = operation + if len(operation.operator) > 0 { + apesWithOutValue = append(apesWithOutValue, id) + } else { + apesWithValue = append(apesWithValue, id) + } + } + humnApe := apes["humn"] + humnApe.value = testnumber + apes["humn"] = humnApe + for len(apesWithOutValue) > 1 { + apesWithOutValue = step(apesWithOutValue, &apes) + } + rootApe1Val := apes[apes["root"].apes[0]].value + rootApe2Val := apes[apes["root"].apes[1]].value + return int(math.Abs(float64(rootApe1Val - rootApe2Val))) +} + +func part1(lines []string) { apes := make(map[string]Operation) apesWithValue := []string{} apesWithOutValue := []string{} @@ -35,7 +127,6 @@ func main() { apesWithOutValue = step(apesWithOutValue, &apes) } fmt.Print(apes["root"]) - } func step(apesWithOutValue []string, apes *map[string]Operation) []string {