chore(day17): simplify FindPath. A* is still slower :|

This commit is contained in:
xuu 2024-01-04 17:14:46 -07:00
parent 924c8d74f3
commit faac603440
Signed by: xuu
GPG Key ID: 8B3B0604F164E04F
3 changed files with 103 additions and 87 deletions

View File

@ -119,6 +119,7 @@ func (g *graph) Neighbors(current position) []position {
if forward := current.step(); current.steps < g.max && g.m.Valid(forward.loc) { if forward := current.step(); current.steps < g.max && g.m.Valid(forward.loc) {
nbs = append(nbs, forward) nbs = append(nbs, forward)
} }
return nbs return nbs
} }
@ -130,12 +131,13 @@ func (g *graph) Cost(a, b position) int16 {
} }
// Potential calculates distance to target // Potential calculates distance to target
// func (g *graph) Potential(a, b position) int16 { func (g *graph) Potential(a position) int16 {
// return aoc.ManhattanDistance(a.loc, b.loc) return aoc.ManhattanDistance(a.loc, g.target)
// } }
func (g *graph) Target(a position) bool { // Target returns true when target reached. receives node and cost.
if a.loc == g.target && a.steps >= g.min { func (g *graph) Target(a position, c int16) bool {
if a.loc == g.target && a.steps >= g.min && a.steps <= g.max {
return true return true
} }
return false return false
@ -143,12 +145,12 @@ func (g *graph) Target(a position) bool {
// Seen attempt at simplifying the seen to use horizontal/vertical and no steps. // Seen attempt at simplifying the seen to use horizontal/vertical and no steps.
// It returns correct for part1 but not part 2.. // It returns correct for part1 but not part 2..
// func (g *graph) Seen(a position) position { func (g *graph) Seen(a position) position {
// if g.seenFn != nil { if g.seenFn != nil {
// return g.seenFn(a) return g.seenFn(a)
// } }
// return a return a
// } }
func seenFn(a position) position { func seenFn(a position) position {
if a.direction == U { if a.direction == U {
@ -157,7 +159,7 @@ func seenFn(a position) position {
if a.direction == L { if a.direction == L {
a.direction = R a.direction = R
} }
a.steps = 0 // a.steps = 0
return a return a
} }
@ -167,30 +169,51 @@ func search(m Map, minSteps, maxSteps int8, seenFn func(position) position) int
target := Point{rows - 1, cols - 1} target := Point{rows - 1, cols - 1}
g := graph{min: minSteps, max: maxSteps, m: m, target: target, seenFn: seenFn} g := graph{min: minSteps, max: maxSteps, m: m, target: target, seenFn: seenFn}
cost, path := aoc.FindPath[int16, position](&g, position{loc: start}, position{loc: target}) cost, path, closed := aoc.FindPath[int16, position](&g, position{loc: start}, position{loc: target})
log("total map reads = ", g.reads) log("total map reads = ", g.reads, "cost = ", cost)
printGraph(m, path) printGraph(m, path, closed, g.seenFn)
return int(cost) return int(cost)
} }
// printGraph with the path overlay // printGraph with the path/cost overlay
func printGraph(m Map, path []position) { func printGraph(m Map, path []position, closed map[position]int16, seenFn func(a position) position) {
pts := make(map[Point]position, len(path)) pts := make(map[Point]position, len(path))
for _, pt := range path { for _, pt := range path {
pts[pt.loc] = pt pts[pt.loc] = pt
} }
clpt := make(map[position]position, len(closed))
for pt := range closed {
clpt[position{loc: pt.loc, steps: pt.steps}] = pt
}
for r, row := range m { for r, row := range m {
if r == 0 {
for c := range row { for c := range row {
if _, ok := pts[Point{int16(r), int16(c)}]; ok { if c == 0 {
fmt.Print("*") fmt.Print(" ")
}
fmt.Printf("% 5d", c)
}
fmt.Println("")
}
for c := range row {
if c == 0 {
fmt.Printf("% 5d", r)
}
if pt, ok := pts[Point{int16(r), int16(c)}]; ok {
if seenFn != nil {
pt = seenFn(pt)
}
fmt.Printf("% 5d", closed[pt])
continue continue
} }
fmt.Print(".") fmt.Print(" ....")
} }
fmt.Println("") fmt.Println("")
} }

View File

@ -24,18 +24,18 @@ func TestExample(t *testing.T) {
is.NoErr(err) is.NoErr(err)
t.Log(result) t.Log(result)
is.Equal(result.valuePT1, 102) // is.Equal(result.valuePT1, 102)
is.Equal(result.valuePT2, 94) is.Equal(result.valuePT2, 94)
} }
// func TestSolution(t *testing.T) { func TestSolution(t *testing.T) {
// is := is.New(t) is := is.New(t)
// scan := bufio.NewScanner(bytes.NewReader(input)) scan := bufio.NewScanner(bytes.NewReader(input))
// result, err := run(scan) result, err := run(scan)
// is.NoErr(err) is.NoErr(err)
// t.Log(result) t.Log(result)
// is.Equal(result.valuePT1, 843) // is.Equal(result.valuePT1, 843)
// is.Equal(result.valuePT2, 1017) is.Equal(result.valuePT2, 1017)
// } }

105
search.go
View File

@ -67,22 +67,26 @@ func (s *stack[T]) Pop() T {
// ManhattanDistance the distance between two points measured along axes at right angles. // ManhattanDistance the distance between two points measured along axes at right angles.
func ManhattanDistance[T integer](a, b Point[T]) T { func ManhattanDistance[T integer](a, b Point[T]) T {
return ABS(a[1]-b[1]) + ABS(a[0]-b[0]) return ABS(a[0]-b[0]) + ABS(a[1]-b[1])
} }
type pather[C number, N comparable] interface { type pather[C number, N comparable] interface {
// Neighbors returns all neighbors to node N that should be considered next.
Neighbors(N) []N Neighbors(N) []N
// Cost returns
Cost(a, b N) C Cost(a, b N) C
// Target returns true when target reached. receives node and cost.
Target(N, C) bool
// OPTIONAL: // OPTIONAL:
// Add heuristic for running as A* search. // Add heuristic for running as A* search.
// Potential(a, b N) C // Potential(N) C
// Seen modify value used by seen pruning. // Seen modify value used by seen pruning.
// Seen(N) N // Seen(N) N
// Target returns true if target reached.
// Target(N) bool
} }
// FindPath uses the A* path finding algorithem. // FindPath uses the A* path finding algorithem.
@ -93,9 +97,18 @@ type pather[C number, N comparable] interface {
// //
// start, end are nodes that dileniate the start and end of the search path. // start, end are nodes that dileniate the start and end of the search path.
// The returned values are the calculated cost and the path taken from start to end. // The returned values are the calculated cost and the path taken from start to end.
func FindPath[C integer, N comparable](g pather[C, N], start, end N) (C, []N) { func FindPath[C integer, N comparable](g pather[C, N], start, end N) (C, []N, map[N]C) {
var zero C var zero C
closed := make(map[N]bool)
var seenFn = func(a N) N { return a }
if s, ok := g.(interface{ Seen(N) N }); ok {
seenFn = s.Seen
}
var potentialFn = func(N) C { var zero C; return zero }
if p, ok := g.(interface{ Potential(N) C }); ok {
potentialFn = p.Potential
}
type node struct { type node struct {
cost C cost C
@ -104,7 +117,7 @@ func FindPath[C integer, N comparable](g pather[C, N], start, end N) (C, []N) {
position N position N
} }
NewPath := func(n *node) []N { newPath := func(n *node) []N {
var path []N var path []N
for n.parent != nil { for n.parent != nil {
path = append(path, n.position) path = append(path, n.position)
@ -120,62 +133,42 @@ func FindPath[C integer, N comparable](g pather[C, N], start, end N) (C, []N) {
return b.cost+b.potential < a.cost+a.potential return b.cost+b.potential < a.cost+a.potential
} }
pq := PriorityQueue(less) closed := make(map[N]C)
pq.Enqueue(node{position: start}) open := PriorityQueue(less)
closed[start] = false
defer func() { open.Enqueue(node{position: start, potential: potentialFn(start)})
Log("queue max depth = ", pq.maxDepth, "total enqueue = ", pq.totalEnqueue, "total dequeue = ", pq.totalDequeue) closed[start] = zero
}()
var seenFn = func(a N) N { return a } // defer func() {
if s, ok := g.(interface{ Seen(N) N }); ok { // Log(
seenFn = s.Seen // "queue max depth = ", open.maxDepth,
} // "total enqueue = ", open.totalEnqueue,
// "total dequeue = ", open.totalDequeue,
// "total closed = ", len(closed),
// )
// }()
var targetFn = func(a N) bool { return true } for !open.IsEmpty() {
if s, ok := g.(interface{ Target(N) bool }); ok { current, _ := open.Dequeue()
targetFn = s.Target for _, nb := range g.Neighbors(current.position) {
} next := node{
var potentialFn = func(a, b N) C { var zero C; return zero }
if s, ok := g.(interface{ Potential(a, b N) C }); ok {
potentialFn = s.Potential
}
for !pq.IsEmpty() {
current, _ := pq.Dequeue()
cost, potential, n := current.cost, current.potential, current.position
seen := seenFn(n)
if closed[seen] {
continue
}
closed[seen] = true
if cost > 0 && potential == zero && targetFn(current.position) {
return cost, NewPath(&current)
}
for _, nb := range g.Neighbors(n) {
seen := seenFn(nb)
if closed[seen] {
continue
}
cost := g.Cost(n, nb) + current.cost
nextPath := node{
position: nb, position: nb,
parent: &current, parent: &current,
cost: cost, cost: g.Cost(current.position, nb) + current.cost,
potential: potentialFn(nb, end), potential: potentialFn(nb),
} }
// check if path is in open list
if _, open := closed[seen]; !open { seen := seenFn(nb)
pq.Enqueue(nextPath) cost, ok := closed[seen]
closed[seen] = false // add to open list if !ok || next.cost < cost {
open.Enqueue(next)
closed[seen] = next.cost
}
if next.potential == zero && g.Target(next.position, next.cost) {
return next.cost, newPath(&next), closed
} }
} }
} }
return zero, nil return zero, nil, closed
} }