Zwischenstand

This commit is contained in:
Karl Spickermann 2022-12-20 02:06:55 +01:00
parent 4b081961e5
commit 34155ab4f3
2 changed files with 49 additions and 25 deletions

@ -26,23 +26,24 @@ func main() {
highestGeode := make([]int, len(lines)) highestGeode := make([]int, len(lines))
for i, line := range lines { for i, line := range lines {
blueprint := getBluePrint(line) blueprint := getBluePrint(line)
fastestTime := getFastestTimeToElementN(blueprint, map[[9]int]State{[9]int{0, 0, 0, 0, 1, 0, 0, 0, 0}: State{[4]int{0, 0, 0, 0}, [4]int{1, 0, 0, 0}, 0}}, 2) //fastestTime := getFastestTimeToElementN(blueprint, map[[4]int]State{[4]int{0, 0, 0, 0, 1, 0, 0, 0}: State{[4]int{0, 0, 0, 0}, [4]int{1, 0, 0, 0}, 0}}, 3)
startStates := getAllPossibleCombinationsWithFastestTime(blueprint, map[[9]int]State{[9]int{0, 0, 0, 0, 1, 0, 0, 0, 0}: State{[4]int{0, 0, 0, 0}, [4]int{1, 0, 0, 0}, 0}}, 2, fastestTime) //startStates := getAllPossibleCombinationsWithFastestTime(blueprint, map[[4]int]State{[4]int{0, 0, 0, 0, 1, 0, 0, 0}: State{[4]int{0, 0, 0, 0}, [4]int{1, 0, 0, 0}, 0}}, 3, fastestTime)
tmpStates := getAllPossibleCombinationsWithFastestTime(blueprint, map[[9]int]State{[9]int{0, 0, 0, 0, 1, 0, 0, 0, 0}: State{[4]int{0, 0, 0, 0}, [4]int{1, 0, 0, 0}, 0}}, 2, fastestTime) //tmpStates := getAllPossibleCombinationsWithFastestTime(blueprint, map[[4]int]State{[4]int{0, 0, 0, 0, 1, 0, 0, 0, 0}: State{[4]int{0, 0, 0, 0}, [4]int{1, 0, 0, 0}, 0}}, 2, fastestTime)
fastestTime = getFastestTimeToElementN(blueprint, tmpStates, 3) //fastestTime = getFastestTimeToElementN(blueprint, tmpStates, 3)
startStates = getAllPossibleCombinationsWithFastestTime(blueprint, startStates, 3, fastestTime) //startStates = getAllPossibleCombinationsWithFastestTime(blueprint, startStates, 3, fastestTime)
highestGeode[i] = getHighestGeode(blueprint, startStates) highestGeode[i] = getHighestGeode(blueprint, map[[4]int]State{[4]int{0, 0, 0, 0}: State{[4]int{0, 0, 0, 0}, [4]int{1, 0, 0, 0}, 0}})
fmt.Println(i) fmt.Println(highestGeode[i])
} }
sum := 0 sum := 0
for i, score := range highestGeode { for i, score := range highestGeode {
sum += (i + 1) * score sum += (i + 1) * score
} }
fmt.Println(highestGeode)
fmt.Println(sum) fmt.Println(sum)
} }
func getHighestGeode(blueprint Blueprint, startStates map[[9]int]State) int { func getHighestGeode(blueprint Blueprint, startStates map[[4]int]State) int {
activeStates := startStates activeStates := startStates
endStates := []State{} endStates := []State{}
for len(activeStates) > 0 { for len(activeStates) > 0 {
@ -50,14 +51,14 @@ func getHighestGeode(blueprint Blueprint, startStates map[[9]int]State) int {
} }
highestgeode := 0 highestgeode := 0
for _, state := range endStates { for _, state := range endStates {
if state.currentRessources[3] > highestgeode && state.runtime >= 25 { if state.currentRessources[3] > highestgeode {
highestgeode = state.currentRessources[3] highestgeode = state.currentRessources[3]
} }
} }
return highestgeode return highestgeode
} }
func getFastestTimeToElementN(blueprint Blueprint, startStates map[[9]int]State, untilElementN int) int { func getFastestTimeToElementN(blueprint Blueprint, startStates map[[4]int]State, untilElementN int) int {
activeStates := startStates activeStates := startStates
fastestTimeToObsidianState := State{[4]int{0, 0, 0, 0}, [4]int{1, 0, 0, 0}, 26} fastestTimeToObsidianState := State{[4]int{0, 0, 0, 0}, [4]int{1, 0, 0, 0}, 26}
for len(activeStates) > 0 { for len(activeStates) > 0 {
@ -66,32 +67,41 @@ func getFastestTimeToElementN(blueprint Blueprint, startStates map[[9]int]State,
return fastestTimeToObsidianState.runtime return fastestTimeToObsidianState.runtime
} }
func getAllPossibleCombinationsWithFastestTime(blueprint Blueprint, startStates map[[9]int]State, untilElementN int, fastestTime int) map[[9]int]State { func getAllPossibleCombinationsWithFastestTime(blueprint Blueprint, startStates map[[4]int]State, untilElementN int, fastestTime int) map[[4]int]State {
activeStates := startStates activeStates := startStates
fastestTimeToObsidianState := map[[9]int]State{} fastestTimeToObsidianState := map[[4]int]State{}
for len(activeStates) > 0 { for len(activeStates) > 0 {
stepFindAllFastestTime(&activeStates, &fastestTimeToObsidianState, untilElementN, fastestTime, &blueprint) stepFindAllFastestTime(&activeStates, &fastestTimeToObsidianState, untilElementN, fastestTime, &blueprint)
} }
return fastestTimeToObsidianState return fastestTimeToObsidianState
} }
func step(activeStates *map[[9]int]State, fastestTImeToObsidian *State, untilElementN int, blueprint *Blueprint) { func step(activeStates *map[[4]int]State, fastestTImeToObsidian *State, untilElementN int, blueprint *Blueprint) {
newTmpStates := generatePossibleTmpStates(activeStates, blueprint) newTmpStates := generatePossibleTmpStates(activeStates, blueprint)
for _, tmpState := range newTmpStates { for _, tmpState := range newTmpStates {
if tmpState.currentProduction[untilElementN] > 0 && (*fastestTImeToObsidian).runtime > tmpState.runtime { if tmpState.currentProduction[untilElementN] > 0 && (*fastestTImeToObsidian).runtime > tmpState.runtime {
*fastestTImeToObsidian = tmpState *fastestTImeToObsidian = tmpState
} }
if tmpState.currentProduction[untilElementN] == 0 && tmpState.runtime < (*fastestTImeToObsidian).runtime { if tmpState.currentProduction[untilElementN] == 0 && tmpState.runtime < (*fastestTImeToObsidian).runtime {
(*activeStates)[getIdentifier(tmpState)] = tmpState identifier := getIdentifier(tmpState)
elem, ok := (*activeStates)[identifier]
if !ok || elem.runtime > tmpState.runtime {
(*activeStates)[identifier] = tmpState
}
} }
} }
} }
func stepHighestGeode(activeStates *map[[9]int]State, endStates *[]State, blueprint *Blueprint) { func stepHighestGeode(activeStates *map[[4]int]State, endStates *[]State, blueprint *Blueprint) {
newTmpStates := generatePossibleTmpStates(activeStates, blueprint) newTmpStates := generatePossibleTmpStates(activeStates, blueprint)
for _, tmpState := range newTmpStates { for _, tmpState := range newTmpStates {
if tmpState.runtime < 24 { if tmpState.runtime < 24 && !checkOptimal(tmpState, blueprint) {
(*activeStates)[getIdentifier(tmpState)] = tmpState identifier := getIdentifier(tmpState)
elem, ok := (*activeStates)[identifier]
if !ok || elem.runtime > tmpState.runtime || (elem.runtime == tmpState.runtime && checkBetterRessources(tmpState, elem)) {
(*activeStates)[identifier] = tmpState
}
} else if tmpState.runtime > 24 { } else if tmpState.runtime > 24 {
fmt.Println("WTF") fmt.Println("WTF")
} else { } else {
@ -100,7 +110,17 @@ func stepHighestGeode(activeStates *map[[9]int]State, endStates *[]State, bluepr
} }
} }
func stepFindAllFastestTime(activeStates *map[[9]int]State, fastestTImeStates *map[[9]int]State, untilElementN int, fastestTime int, blueprint *Blueprint) { func checkOptimal(state1 State, blueprint *Blueprint) bool {
return state1.currentProduction[0] >= blueprint.geodeRoboterCost[0] && state1.currentProduction[2] >= blueprint.geodeRoboterCost[1]
}
func checkBetterRessources(state1, state2 State) bool {
ressources1 := state1.currentRessources
ressources2 := state2.currentRessources
return ressources1[0] > ressources1[0] && ressources1[1] > ressources2[1] && ressources1[2] > ressources2[2] && ressources1[3] > ressources2[3]
}
func stepFindAllFastestTime(activeStates *map[[4]int]State, fastestTImeStates *map[[4]int]State, untilElementN int, fastestTime int, blueprint *Blueprint) {
newTmpStates := generatePossibleTmpStates(activeStates, blueprint) newTmpStates := generatePossibleTmpStates(activeStates, blueprint)
for _, tmpState := range newTmpStates { for _, tmpState := range newTmpStates {
if tmpState.currentProduction[untilElementN] > 0 && fastestTime >= tmpState.runtime && tmpState.runtime < 24 { if tmpState.currentProduction[untilElementN] > 0 && fastestTime >= tmpState.runtime && tmpState.runtime < 24 {
@ -113,18 +133,18 @@ func stepFindAllFastestTime(activeStates *map[[9]int]State, fastestTImeStates *m
} }
} }
func getIdentifier(tmpState State) [9]int { func getIdentifier(tmpState State) [4]int {
identifier := [9]int{} identifier := [4]int{}
copy(identifier[:], append(append(tmpState.currentRessources[:], tmpState.currentProduction[:]...), tmpState.runtime)[:9]) copy(identifier[:], tmpState.currentProduction[:4])
return identifier return identifier
} }
func generatePossibleTmpStates(activeStates *map[[9]int]State, blueprint *Blueprint) map[[9]int]State { func generatePossibleTmpStates(activeStates *map[[4]int]State, blueprint *Blueprint) map[[4]int]State {
key := get_some_key(*activeStates) key := get_some_key(*activeStates)
activeState := (*activeStates)[key] activeState := (*activeStates)[key]
delete(*activeStates, key) delete(*activeStates, key)
possibleProductions := activeState.getPossibleProductions(blueprint) possibleProductions := activeState.getPossibleProductions(blueprint)
newTmpStates := make(map[[9]int]State) newTmpStates := make(map[[4]int]State)
for i := -1; i < len(possibleProductions); i++ { for i := -1; i < len(possibleProductions); i++ {
if i == -1 || possibleProductions[i] == 1 { if i == -1 || possibleProductions[i] == 1 {
tmpState := activeState tmpState := activeState
@ -194,9 +214,9 @@ func getBluePrint(line string) Blueprint {
return Blueprint{oreCost, claycost, [2]int{obsidianCost[0], obsidianCost[1]}, [2]int{geodeCost[0], geodeCost[1]}} return Blueprint{oreCost, claycost, [2]int{obsidianCost[0], obsidianCost[1]}, [2]int{geodeCost[0], geodeCost[1]}}
} }
func get_some_key(m map[[9]int]State) [9]int { func get_some_key(m map[[4]int]State) [4]int {
for k := range m { for k := range m {
return k return k
} }
return [9]int{} return [4]int{}
} }

4
day19/inputMax Normal file

@ -0,0 +1,4 @@
Blueprint 22: Each ore robot costs 3 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 6 clay. Each geode robot costs 2 ore and 16 obsidian.
Blueprint 23: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 6 clay. Each geode robot costs 4 ore and 11 obsidian.
Blueprint 24: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 5 clay. Each geode robot costs 2 ore and 10 obsidian.
Blueprint 25: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 2 ore and 14 clay. Each geode robot costs 3 ore and 14 obsidian.