Day21 Part1

This commit is contained in:
Karl Spickermann 2022-12-21 18:37:32 +01:00
parent aec2b2c848
commit 97d1fecaf6
3 changed files with 2834 additions and 0 deletions

86
day21/day21.go Normal file

@ -0,0 +1,86 @@
package main
import (
"AOC2022/helper"
"fmt"
"strconv"
"strings"
)
type Operation struct {
value int
apes [2]string
operator string
hasValue bool
}
func main() {
//args := os.Args[1:]
lines := helper.ReadTextFile("day21/input")
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)
}
}
fmt.Println(apesWithValue)
fmt.Println(apesWithOutValue)
for len(apesWithOutValue) > 0 {
apesWithOutValue = step(apesWithOutValue, &apes)
}
fmt.Print(apes["root"])
}
func step(apesWithOutValue []string, apes *map[string]Operation) []string {
newApesWithoutValue := []string{}
for _, id := range apesWithOutValue {
ape := (*apes)[id]
ape1 := (*apes)[ape.apes[0]]
ape2 := (*apes)[ape.apes[1]]
if ape1.hasValue && ape2.hasValue {
ape.value = calcOperation(ape1.value, ape2.value, ape.operator)
ape.hasValue = true
(*apes)[id] = ape
} else {
newApesWithoutValue = append(newApesWithoutValue, id)
}
}
return newApesWithoutValue
}
func getApe(line string) (id string, operation Operation) {
splitLine := strings.Split(line, ": ")
id = splitLine[0]
if strings.ContainsAny(splitLine[1], " ") {
operationLine := strings.Split(splitLine[1], " ")
operation.apes[0] = operationLine[0]
operation.apes[1] = operationLine[2]
operation.operator = string(operationLine[1][0])
} else {
operation.hasValue = true
operation.value = helper.RemoveError(strconv.Atoi(splitLine[1]))
}
return
}
func calcOperation(val1, val2 int, op string) int {
switch op {
case "*":
return val1 * val2
case "/":
return val1 / val2
case "+":
return val1 + val2
case "-":
return val1 - val2
default:
return -99999999999999
}
}

2733
day21/input Normal file

File diff suppressed because it is too large Load Diff

15
day21/testinput Normal file

@ -0,0 +1,15 @@
root: pppw + sjmn
dbpl: 5
cczh: sllz + lgvd
zczc: 2
ptdq: humn - dvpt
dvpt: 3
lfqf: 4
humn: 5
ljgn: 2
sjmn: drzm * dbpl
sllz: 4
pppw: cczh / lfqf
lgvd: ljgn * ptdq
drzm: hmdt - zczc
hmdt: 32