140 lines
3.9 KiB
Go
140 lines
3.9 KiB
Go
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, ¤tValve, &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 ""
|
|
}
|