Zwischenstand

This commit is contained in:
Karl Spickermann 2022-12-17 18:02:58 +01:00
parent 8da77203f0
commit 4ad506dfd2
4 changed files with 207 additions and 3 deletions

139
day16/day16.go Normal file
View File

@ -0,0 +1,139 @@
package main
import (
"AOC2022/helper"
"fmt"
"strconv"
"strings"
)
type Valve struct {
id string
flowRate int
routes map[string]int
cost int
before string
}
func main() {
//args := os.Args[1:]
lines := helper.ReadTextFile("day16/testinput")
valves := getValves(lines)
startValve := "AA"
valvesWithFlowRate := getValvesWithFlowRate(valves)
valvesWithFlowRate = append(valvesWithFlowRate, "AA")
fmt.Println(startValve)
fmt.Println(valvesWithFlowRate)
valvesCopy := deepCopyMap(valves)
for i, activeValve := range valvesWithFlowRate {
routes := make(map[string]int)
for j, valve := range valvesWithFlowRate {
if i != j {
tmpValves := deepCopyMap(valvesCopy)
routeLenght := getRouteLength(activeValve, valve, tmpValves)
routes[valve] = routeLenght
}
}
entry := valves[activeValve]
entry.routes = routes
valves[activeValve] = entry
}
currentValve := "AA"
remainingValves := valvesWithFlowRate[:len(valvesWithFlowRate)-1]
remainingTime := 30
sumReleasedPressure := 0
getNextStep(&remainingValves, valves, &currentValve, &remainingTime, &sumReleasedPressure)
fmt.Println(currentValve)
}
func getNextStep(remainingValves *[]string, valves map[string]Valve, currentValve *string, remainingTime *int, sumReleasedPressure *int) {
bestValve := -1
bestValue := 0
for i, remainingValve := range *remainingValves {
movingTimeCost := valves[*currentValve].routes[remainingValve]
openingTimeCost := 1
flowrate := valves[remainingValve].flowRate
possibleGainedPressureReduction := (*remainingTime - openingTimeCost - movingTimeCost) * flowrate
if possibleGainedPressureReduction > bestValue {
bestValue = possibleGainedPressureReduction
bestValve = i
}
}
*sumReleasedPressure += bestValue
*remainingTime -= (valves[*currentValve].routes[(*remainingValves)[bestValve]] + 1)
*currentValve = (*remainingValves)[bestValve]
helper.Remove(remainingValves, bestValve)
}
func deepCopyMap(valves map[string]Valve) map[string]Valve {
tmpValves := make(map[string]Valve)
for k, v := range valves {
tmpValves[k] = v
}
return tmpValves
}
func getValves(lines []string) map[string]Valve {
valves := make(map[string]Valve)
for _, line := range lines {
id := line[6:8]
flowrate := helper.RemoveError(strconv.Atoi(strings.Split(line[23:], ";")[0]))
routesStrings := strings.Split(strings.Split(line[23:], ";")[1][24:], ", ")
routes := make(map[string]int)
for _, routeString := range routesStrings {
routes[routeString] = 1
}
valves[id] = Valve{id, flowrate, routes, 0, ""}
}
return valves
}
func getValvesWithFlowRate(valves map[string]Valve) []string {
valvesWithFlowRate := []string{}
for key, val := range valves {
if val.flowRate > 0 {
valvesWithFlowRate = append(valvesWithFlowRate, key)
}
}
return valvesWithFlowRate
}
func getRouteLength(valve1, valve2 string, valves map[string]Valve) int {
activeValves := make(map[string]struct{})
activeValves[valve1] = struct{}{}
return getBestRouteLength(activeValves, valves, valve2)
}
func getBestRouteLength(activeValves map[string]struct{}, valves map[string]Valve, endValve string) int {
for len(activeValves) > 0 {
step(&valves, &activeValves)
}
return valves[endValve].cost
}
func step(valves *map[string]Valve, activeValves *map[string]struct{}) {
valve := get_some_key(*activeValves)
delete(*activeValves, valve)
currentValve := (*valves)[valve]
currentFieldSumCost := currentValve.cost
directions := currentValve.routes
for nextValve, _ := range directions {
cost := 1 + currentFieldSumCost
if nextValve != currentValve.before {
if (*valves)[nextValve].cost > cost || (*valves)[nextValve].cost == 0 {
entry := (*valves)[nextValve]
entry.cost = cost
entry.before = valve
(*valves)[nextValve] = entry
(*activeValves)[nextValve] = struct{}{}
}
}
}
}
func get_some_key(m map[string]struct{}) string {
for k := range m {
return k
}
return ""
}

55
day16/input Normal file
View File

@ -0,0 +1,55 @@
Valve SW has flow rate=0; tunnels lead to valves LX, LD
Valve VS has flow rate=0; tunnels lead to valves JO, OO
Valve OO has flow rate=10; tunnels lead to valves KK, HD, VS, KI
Valve DZ has flow rate=8; tunnels lead to valves KV, GX, WQ, BA, PK
Valve GX has flow rate=0; tunnels lead to valves AA, DZ
Valve IF has flow rate=0; tunnels lead to valves OI, DW
Valve BO has flow rate=0; tunnels lead to valves UJ, ZT
Valve KI has flow rate=0; tunnels lead to valves OO, KU
Valve JT has flow rate=3; tunnels lead to valves FC, AM, KV, XP, XZ
Valve TQ has flow rate=0; tunnels lead to valves AA, DW
Valve KK has flow rate=0; tunnels lead to valves QW, OO
Valve NR has flow rate=0; tunnels lead to valves UG, XM
Valve VO has flow rate=0; tunnels lead to valves YR, AA
Valve MS has flow rate=17; tunnels lead to valves LT, LX
Valve JO has flow rate=0; tunnels lead to valves YR, VS
Valve ZB has flow rate=0; tunnels lead to valves UJ, LT
Valve ZT has flow rate=0; tunnels lead to valves XM, BO
Valve YR has flow rate=9; tunnels lead to valves VO, FY, WB, JO
Valve QS has flow rate=0; tunnels lead to valves QW, FY
Valve UD has flow rate=0; tunnels lead to valves CA, JB
Valve AP has flow rate=0; tunnels lead to valves CA, DW
Valve KV has flow rate=0; tunnels lead to valves JT, DZ
Valve JH has flow rate=0; tunnels lead to valves IK, UJ
Valve LD has flow rate=15; tunnels lead to valves IK, SW
Valve XK has flow rate=0; tunnels lead to valves XZ, BH
Valve XM has flow rate=11; tunnels lead to valves XP, CJ, ZT, NR
Valve FY has flow rate=0; tunnels lead to valves YR, QS
Valve GI has flow rate=22; tunnel leads to valve TI
Valve JB has flow rate=14; tunnels lead to valves WB, UD, WQ, HD
Valve DW has flow rate=6; tunnels lead to valves AP, TQ, NQ, IF, PK
Valve UJ has flow rate=13; tunnels lead to valves JH, ZB, BO
Valve KU has flow rate=0; tunnels lead to valves CA, KI
Valve WQ has flow rate=0; tunnels lead to valves JB, DZ
Valve BA has flow rate=0; tunnels lead to valves BH, DZ
Valve AA has flow rate=0; tunnels lead to valves YX, TQ, VO, GX, QP
Valve TI has flow rate=0; tunnels lead to valves GI, UG
Valve FC has flow rate=0; tunnels lead to valves QP, JT
Valve CA has flow rate=18; tunnels lead to valves KU, UD, AP
Valve QW has flow rate=25; tunnels lead to valves QS, KK
Valve XZ has flow rate=0; tunnels lead to valves JT, XK
Valve YX has flow rate=0; tunnels lead to valves AA, CJ
Valve OI has flow rate=0; tunnels lead to valves IF, BH
Valve NQ has flow rate=0; tunnels lead to valves AM, DW
Valve QP has flow rate=0; tunnels lead to valves AA, FC
Valve AM has flow rate=0; tunnels lead to valves NQ, JT
Valve XP has flow rate=0; tunnels lead to valves XM, JT
Valve BH has flow rate=12; tunnels lead to valves BA, XK, OI
Valve HD has flow rate=0; tunnels lead to valves OO, JB
Valve LT has flow rate=0; tunnels lead to valves MS, ZB
Valve LX has flow rate=0; tunnels lead to valves MS, SW
Valve CJ has flow rate=0; tunnels lead to valves XM, YX
Valve PK has flow rate=0; tunnels lead to valves DW, DZ
Valve IK has flow rate=0; tunnels lead to valves LD, JH
Valve WB has flow rate=0; tunnels lead to valves YR, JB
Valve UG has flow rate=21; tunnels lead to valves TI, NR

10
day16/testinput Normal file
View File

@ -0,0 +1,10 @@
Valve AA has flow rate=0; tunnels lead to valves DD, II, BB
Valve BB has flow rate=13; tunnels lead to valves CC, AA
Valve CC has flow rate=2; tunnels lead to valves DD, BB
Valve DD has flow rate=20; tunnels lead to valves CC, AA, EE
Valve EE has flow rate=3; tunnels lead to valves FF, DD
Valve FF has flow rate=0; tunnels lead to valves EE, GG
Valve GG has flow rate=0; tunnels lead to valves FF, HH
Valve HH has flow rate=22; tunnel leads to valve GG
Valve II has flow rate=0; tunnels lead to valves AA, JJ
Valve JJ has flow rate=21; tunnel leads to valve II

View File

@ -98,9 +98,9 @@ func Contains[T constraints.Ordered](elems []T, v T) bool {
return false
}
func Remove[T constraints.Ordered](s []T, i int) []T {
s[i] = s[len(s)-1]
return s[:len(s)-1]
func Remove[T any](s *[]T, i int) {
(*s)[i] = (*s)[len(*s)-1]
*s = (*s)[:len(*s)-1]
}
func GetValueOf2DMap[T any](location [2]int, map2D *[][]T) (T, error) {