advent-of-code/search.go

131 lines
2.6 KiB
Go
Raw Normal View History

package aoc
import (
2024-01-01 09:26:31 -07:00
"fmt"
"sort"
)
2023-12-28 18:57:22 -07:00
type priorityQueue[T any, U []T] struct {
2024-01-01 09:26:31 -07:00
elems U
less func(a, b T) bool
maxDepth int
totalEnqueue int
}
2023-12-28 18:57:22 -07:00
func PriorityQueue[T any, U []T](less func(a, b T) bool) *priorityQueue[T, U] {
return &priorityQueue[T, U]{less: less}
}
2023-12-28 18:57:22 -07:00
func (pq *priorityQueue[T, U]) Enqueue(elem T) {
pq.elems = append(pq.elems, elem)
2024-01-01 09:26:31 -07:00
pq.totalEnqueue++
pq.maxDepth = max(pq.maxDepth, len(pq.elems))
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
func (pq *priorityQueue[T, I]) IsEmpty() bool {
return len(pq.elems) == 0
}
2023-12-28 18:57:22 -07:00
func (pq *priorityQueue[T, I]) Dequeue() (T, bool) {
var elem T
if pq.IsEmpty() {
return elem, false
}
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
}
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 any] interface {
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
}
type Path[C number, N any] []N
func FindPath[C integer, N comparable](g pather[C, N], start, end N) (C, Path[C, N]) {
var zero C
closed := make(map[N]bool)
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 09:26:31 -07:00
defer func() {
fmt.Println("queue max depth = ", pq.maxDepth, "total enqueue = ", pq.totalEnqueue)
}()
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)
if closed[seen] {
continue
}
closed[seen] = true
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)
if closed[seen] {
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),
}
pq.Enqueue(nextPath)
}
}
return zero, nil
}