advent-of-code/search.go

151 lines
3.4 KiB
Go
Raw Normal View History

package aoc
import (
"sort"
)
type priorityQueue[T any] struct {
elems []T
2024-01-01 09:26:31 -07:00
less func(a, b T) bool
maxDepth int
totalEnqueue int
totalDequeue int
}
// PriorityQueue implements a simple slice based queue.
// less is the function for sorting. reverse a and b to reverse the sort.
// T is the item
// U is a slice of T
func PriorityQueue[T any](less func(a, b T) bool) *priorityQueue[T] {
return &priorityQueue[T]{less: less}
}
func (pq *priorityQueue[T]) Enqueue(elem T) {
2024-01-01 09:26:31 -07:00
pq.totalEnqueue++
pq.elems = append(pq.elems, elem)
2024-01-01 09:26:31 -07:00
pq.maxDepth = max(pq.maxDepth, len(pq.elems))
}
func (pq *priorityQueue[T]) IsEmpty() bool {
2023-12-28 18:57:22 -07:00
return len(pq.elems) == 0
}
func (pq *priorityQueue[T]) Dequeue() (T, bool) {
pq.totalDequeue++
var elem T
if pq.IsEmpty() {
return elem, false
}
sort.Slice(pq.elems, func(i, j int) bool { return pq.less(pq.elems[i], pq.elems[j]) })
2023-12-28 18:57:22 -07:00
pq.elems, elem = pq.elems[:len(pq.elems)-1], pq.elems[len(pq.elems)-1]
return elem, true
}
// ManhattanDistance the distance between two points measured along axes at right angles.
2024-01-01 09:26:31 -07:00
func ManhattanDistance[T integer](a, b Point[T]) T {
return ABS(a[1]-b[1]) + ABS(a[0]-b[0])
}
type pather[C number, N comparable] interface {
2024-01-01 09:26:31 -07:00
Neighbors(N) []N
Cost(a, b N) C
Potential(a, b N) C
2024-01-01 09:57:08 -07:00
// OPTIONAL:
// Seen modify value used by seen pruning.
2024-01-01 09:26:31 -07:00
// Seen(N) N
2024-01-01 09:57:08 -07:00
// Target returns true if target reached.
// Target(N) bool
2024-01-01 09:26:31 -07:00
}
// FindPath uses the A* path finding algorithem.
// g is the graph source that implements the pather interface.
2024-01-01 14:12:53 -07:00
//
// C is an numeric type for calculating cost/potential
// N is the node values. is comparable for storing in visited table for pruning.
//
// 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.
func FindPath[C integer, N comparable](g pather[C, N], start, end N) (C, []N) {
2024-01-01 09:26:31 -07:00
var zero C
2024-01-01 14:12:53 -07:00
closed := make(map[N]bool)
2024-01-01 09:26:31 -07:00
type node struct {
cost C
potential C
parent *node
2024-01-01 09:57:08 -07:00
position N
2024-01-01 09:26:31 -07:00
}
NewPath := func(n *node) []N {
var path []N
for n.parent != nil {
2024-01-01 09:57:08 -07:00
path = append(path, n.position)
2024-01-01 09:26:31 -07:00
n = n.parent
}
2024-01-01 09:57:08 -07:00
path = append(path, n.position)
2024-01-01 09:26:31 -07:00
Reverse(path)
return path
}
less := func(a, b node) bool {
return b.cost+b.potential < a.cost+a.potential
}
pq := PriorityQueue(less)
2024-01-01 09:57:08 -07:00
pq.Enqueue(node{position: start})
2024-01-01 14:12:53 -07:00
closed[start] = false
2024-01-01 09:26:31 -07:00
defer func() {
Log("queue max depth = ", pq.maxDepth, "total enqueue = ", pq.totalEnqueue, "total dequeue = ", pq.totalDequeue)
2024-01-01 09:26:31 -07:00
}()
var seenFn = func(a N) N { return a }
if s, ok := g.(interface{ Seen(N) N }); ok {
seenFn = s.Seen
}
2024-01-01 09:57:08 -07:00
var targetFn = func(a N) bool { return true }
if s, ok := g.(interface{ Target(N) bool }); ok {
targetFn = s.Target
}
2024-01-01 09:26:31 -07:00
for !pq.IsEmpty() {
current, _ := pq.Dequeue()
2024-01-01 09:57:08 -07:00
cost, potential, n := current.cost, current.potential, current.position
2024-01-01 09:26:31 -07:00
seen := seenFn(n)
2024-01-01 14:12:53 -07:00
if closed[seen] {
2024-01-01 09:26:31 -07:00
continue
}
2024-01-01 14:12:53 -07:00
closed[seen] = true
2024-01-01 09:26:31 -07:00
2024-01-01 09:57:08 -07:00
if cost > 0 && potential == zero && targetFn(current.position) {
2024-01-01 09:26:31 -07:00
return cost, NewPath(&current)
}
for _, nb := range g.Neighbors(n) {
seen := seenFn(nb)
2024-01-01 14:12:53 -07:00
if closed[seen] {
2024-01-01 09:26:31 -07:00
continue
}
cost := g.Cost(n, nb) + current.cost
nextPath := node{
2024-01-01 09:57:08 -07:00
position: nb,
2024-01-01 09:26:31 -07:00
parent: &current,
cost: cost,
potential: g.Potential(nb, end),
}
2024-01-01 14:12:53 -07:00
// check if path is in open list
if _, open := closed[seen]; !open {
pq.Enqueue(nextPath)
closed[seen] = false // add to open list
}
2024-01-01 09:26:31 -07:00
}
}
return zero, nil
}