mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-09 14:20:48 +00:00
use already calculated one.LowestCost
This commit is contained in:
parent
17f2bc8223
commit
f1cac7da7b
@ -4,6 +4,8 @@ import (
|
||||
"bytes"
|
||||
)
|
||||
|
||||
var LowestCost int
|
||||
|
||||
func Solve(buf []byte) (int, error) {
|
||||
r := bytes.NewReader(buf)
|
||||
graph, err := parseLines(r)
|
||||
@ -11,10 +13,10 @@ func Solve(buf []byte) (int, error) {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
lowestCost, err := graph.dijkstra()
|
||||
LowestCost, err = graph.dijkstra()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return lowestCost, nil
|
||||
return LowestCost, nil
|
||||
}
|
||||
|
@ -2,10 +2,10 @@ package two
|
||||
|
||||
import (
|
||||
hp "container/heap"
|
||||
"math"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/onyx-and-iris/aoc2024/day-16/internal/one"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
@ -32,20 +32,18 @@ func (g *graph) dijkstra() int {
|
||||
hp.Push(heap, move{g.start, 0, []node{g.start}})
|
||||
visited := make(map[node]int)
|
||||
const turnCost int = 1000
|
||||
lowestCost := math.MaxInt
|
||||
|
||||
bestPaths := [][]node{}
|
||||
lowestCostPaths := [][]node{}
|
||||
for heap.Len() > 0 {
|
||||
current := hp.Pop(heap).(move)
|
||||
|
||||
// we're on a path that's already exceeded the lowest cost
|
||||
if current.cost > lowestCost {
|
||||
if current.cost > one.LowestCost {
|
||||
continue
|
||||
}
|
||||
|
||||
if g.valueAt(current.node) == 'E' {
|
||||
bestPaths = append(bestPaths, current.path)
|
||||
lowestCost = current.cost
|
||||
lowestCostPaths = append(lowestCostPaths, current.path)
|
||||
continue
|
||||
}
|
||||
|
||||
@ -71,7 +69,7 @@ func (g *graph) dijkstra() int {
|
||||
}
|
||||
|
||||
possibleSafe := make(map[coords]struct{})
|
||||
for _, path := range bestPaths {
|
||||
for _, path := range lowestCostPaths {
|
||||
for _, n := range path {
|
||||
possibleSafe[n.coords] = struct{}{}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user