2023-12-27 14:07:32 -07:00
|
|
|
package aoc
|
|
|
|
|
|
|
|
import (
|
2024-01-02 17:02:12 -07:00
|
|
|
"maps"
|
2023-12-27 14:07:32 -07:00
|
|
|
"sort"
|
|
|
|
)
|
|
|
|
|
2024-01-01 10:59:40 -07:00
|
|
|
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
|
2024-01-01 10:59:40 -07:00
|
|
|
totalDequeue int
|
2023-12-27 14:07:32 -07:00
|
|
|
}
|
|
|
|
|
2024-01-01 10:59:40 -07:00
|
|
|
// 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}
|
2023-12-27 14:07:32 -07:00
|
|
|
}
|
2024-01-01 10:59:40 -07:00
|
|
|
func (pq *priorityQueue[T]) Enqueue(elem T) {
|
2024-01-01 09:26:31 -07:00
|
|
|
pq.totalEnqueue++
|
2024-01-01 10:59:40 -07:00
|
|
|
|
|
|
|
pq.elems = append(pq.elems, elem)
|
2024-01-01 09:26:31 -07:00
|
|
|
pq.maxDepth = max(pq.maxDepth, len(pq.elems))
|
2023-12-27 14:07:32 -07:00
|
|
|
}
|
2024-01-01 10:59:40 -07:00
|
|
|
func (pq *priorityQueue[T]) IsEmpty() bool {
|
2023-12-28 18:57:22 -07:00
|
|
|
return len(pq.elems) == 0
|
2023-12-27 14:07:32 -07:00
|
|
|
}
|
2024-01-01 10:59:40 -07:00
|
|
|
func (pq *priorityQueue[T]) Dequeue() (T, bool) {
|
|
|
|
pq.totalDequeue++
|
|
|
|
|
2023-12-27 14:07:32 -07:00
|
|
|
var elem T
|
|
|
|
if pq.IsEmpty() {
|
|
|
|
return elem, false
|
|
|
|
}
|
|
|
|
|
2024-01-01 10:59:40 -07:00
|
|
|
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]
|
2023-12-27 14:07:32 -07:00
|
|
|
return elem, true
|
|
|
|
}
|
|
|
|
|
2024-01-02 17:02:12 -07:00
|
|
|
type stack[T any] []T
|
|
|
|
|
|
|
|
func Stack[T any](a ...T) *stack[T] {
|
|
|
|
var s stack[T] = a
|
|
|
|
return &s
|
|
|
|
}
|
|
|
|
func (s *stack[T]) Push(a ...T) {
|
|
|
|
if s == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
*s = append(*s, a...)
|
|
|
|
}
|
|
|
|
func (s *stack[T]) IsEmpty() bool {
|
|
|
|
return s == nil || len(*s) == 0
|
|
|
|
}
|
|
|
|
func (s *stack[T]) Pop() T {
|
|
|
|
var a T
|
|
|
|
if s.IsEmpty() {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
a, *s = (*s)[len(*s)-1], (*s)[:len(*s)-1]
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2024-01-01 10:59:40 -07:00
|
|
|
// 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])
|
|
|
|
}
|
|
|
|
|
2024-01-01 10:59:40 -07:00
|
|
|
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 10:59:40 -07:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-01-01 10:59:40 -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.
|
|
|
|
//
|
2024-01-01 10:59:40 -07:00
|
|
|
// 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() {
|
2024-01-01 10:59:40 -07:00
|
|
|
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(¤t)
|
|
|
|
}
|
|
|
|
|
|
|
|
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: ¤t,
|
|
|
|
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
|
2023-12-27 14:07:32 -07:00
|
|
|
}
|
2024-01-02 17:02:12 -07:00
|
|
|
|
|
|
|
// FindPath uses the A* path finding algorithem.
|
|
|
|
// g is the graph source that implements the pather interface.
|
|
|
|
//
|
|
|
|
// 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 FindPaths[C integer, N comparable](g pather[C, N], start, end N) ([]C, [][]N) {
|
|
|
|
var zero C
|
|
|
|
// closed := make(map[N]bool)
|
|
|
|
|
|
|
|
type node struct {
|
|
|
|
cost C
|
|
|
|
potential C
|
|
|
|
parent *node
|
|
|
|
position N
|
|
|
|
closed map[N]bool
|
|
|
|
}
|
|
|
|
|
|
|
|
NewPath := func(n *node) []N {
|
|
|
|
var path []N
|
|
|
|
for n.parent != nil {
|
|
|
|
path = append(path, n.position)
|
|
|
|
n = n.parent
|
|
|
|
}
|
|
|
|
path = append(path, n.position)
|
|
|
|
|
|
|
|
Reverse(path)
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
|
|
|
less := func(b, a node) bool {
|
|
|
|
return b.cost+b.potential < a.cost+a.potential
|
|
|
|
}
|
|
|
|
|
|
|
|
pq := PriorityQueue(less)
|
|
|
|
pq.Enqueue(node{position: start, closed: make(map[N]bool)})
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
Log("queue max depth = ", pq.maxDepth, "total enqueue = ", pq.totalEnqueue, "total dequeue = ", pq.totalDequeue)
|
|
|
|
}()
|
|
|
|
|
|
|
|
var seenFn = func(a N) N { return a }
|
|
|
|
if s, ok := g.(interface{ Seen(N) N }); ok {
|
|
|
|
seenFn = s.Seen
|
|
|
|
}
|
|
|
|
|
|
|
|
var targetFn = func(a N) bool { return true }
|
|
|
|
if s, ok := g.(interface{ Target(N) bool }); ok {
|
|
|
|
targetFn = s.Target
|
|
|
|
}
|
|
|
|
|
|
|
|
var paths [][]N
|
|
|
|
var costs []C
|
|
|
|
|
|
|
|
for !pq.IsEmpty() {
|
|
|
|
current, _ := pq.Dequeue()
|
|
|
|
cost, potential, n := current.cost, current.potential, current.position
|
|
|
|
|
|
|
|
seen := seenFn(n)
|
|
|
|
if current.closed[seen] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
current.closed[seen] = true
|
|
|
|
|
|
|
|
if cost > 0 && potential == zero && cost > Max(0, costs...) && targetFn(current.position) {
|
|
|
|
paths = append([][]N(nil), NewPath(¤t))
|
|
|
|
costs = append([]C(nil), cost)
|
|
|
|
Log("new record = ", cost)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, nb := range g.Neighbors(n) {
|
|
|
|
seen := seenFn(nb)
|
|
|
|
if current.closed[seen] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
cost := g.Cost(n, nb) + current.cost
|
|
|
|
next := node{
|
|
|
|
position: nb,
|
|
|
|
parent: ¤t,
|
|
|
|
cost: cost,
|
|
|
|
potential: g.Potential(nb, end),
|
|
|
|
closed: maps.Clone(current.closed),
|
|
|
|
}
|
|
|
|
// check if path is in open list
|
|
|
|
if _, open := current.closed[seen]; !open {
|
|
|
|
next.closed[seen] = false // add to open list
|
|
|
|
pq.Enqueue(next)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return costs, paths
|
|
|
|
}
|