Day21
This commit is contained in:
parent
97d1fecaf6
commit
6cbca15c3d
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"AOC2022/helper"
|
"AOC2022/helper"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
@ -17,6 +18,97 @@ type Operation struct {
|
||||||
func main() {
|
func main() {
|
||||||
//args := os.Args[1:]
|
//args := os.Args[1:]
|
||||||
lines := helper.ReadTextFile("day21/input")
|
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)
|
apes := make(map[string]Operation)
|
||||||
apesWithValue := []string{}
|
apesWithValue := []string{}
|
||||||
apesWithOutValue := []string{}
|
apesWithOutValue := []string{}
|
||||||
|
@ -35,7 +127,6 @@ func main() {
|
||||||
apesWithOutValue = step(apesWithOutValue, &apes)
|
apesWithOutValue = step(apesWithOutValue, &apes)
|
||||||
}
|
}
|
||||||
fmt.Print(apes["root"])
|
fmt.Print(apes["root"])
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func step(apesWithOutValue []string, apes *map[string]Operation) []string {
|
func step(apesWithOutValue []string, apes *map[string]Operation) []string {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user