From 75855266345aaf998b58c5a4cbbcda2988576a20 Mon Sep 17 00:00:00 2001 From: xuu Date: Thu, 4 Jan 2024 17:14:46 -0700 Subject: [PATCH] chore(day17): simplify FindPath. A* is still slower :| --- day17/main.go | 63 +++++++++++++++++--------- day17/main_test.go | 18 ++++---- search.go | 107 +++++++++++++++++++++------------------------ 3 files changed, 102 insertions(+), 86 deletions(-) diff --git a/day17/main.go b/day17/main.go index f4b5b9f..f1d45be 100644 --- a/day17/main.go +++ b/day17/main.go @@ -119,6 +119,7 @@ func (g *graph) Neighbors(current position) []position { if forward := current.step(); current.steps < g.max && g.m.Valid(forward.loc) { nbs = append(nbs, forward) } + return nbs } @@ -130,12 +131,13 @@ func (g *graph) Cost(a, b position) int16 { } // Potential calculates distance to target -// func (g *graph) Potential(a, b position) int16 { -// return aoc.ManhattanDistance(a.loc, b.loc) -// } +func (g *graph) Potential(a position) int16 { + return aoc.ManhattanDistance(a.loc, g.target) +} -func (g *graph) Target(a position) bool { - if a.loc == g.target && a.steps >= g.min { +// Target returns true when target reached. receives node and cost. +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 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. // It returns correct for part1 but not part 2.. -// func (g *graph) Seen(a position) position { -// if g.seenFn != nil { -// return g.seenFn(a) -// } -// return a -// } +func (g *graph) Seen(a position) position { + if g.seenFn != nil { + return g.seenFn(a) + } + return a +} func seenFn(a position) position { if a.direction == U { @@ -157,7 +159,7 @@ func seenFn(a position) position { if a.direction == L { a.direction = R } - a.steps = 0 + // a.steps = 0 return a } @@ -167,30 +169,51 @@ func search(m Map, minSteps, maxSteps int8, seenFn func(position) position) int target := Point{rows - 1, cols - 1} 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) - printGraph(m, path) + log("total map reads = ", g.reads, "cost = ", cost) + printGraph(m, path, closed, g.seenFn) return int(cost) } -// printGraph with the path overlay -func printGraph(m Map, path []position) { +// printGraph with the path/cost overlay +func printGraph(m Map, path []position, closed map[position]int16, seenFn func(a position) position) { pts := make(map[Point]position, len(path)) for _, pt := range path { 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 { + if r == 0 { + for c := range row { + if c == 0 { + fmt.Print(" ") + } + fmt.Printf("% 5d", c) + } + fmt.Println("") + } for c := range row { - if _, ok := pts[Point{int16(r), int16(c)}]; ok { - fmt.Print("*") + 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 } - fmt.Print(".") + fmt.Print(" ....") } fmt.Println("") } diff --git a/day17/main_test.go b/day17/main_test.go index 64e490d..d7fd635 100644 --- a/day17/main_test.go +++ b/day17/main_test.go @@ -28,14 +28,14 @@ func TestExample(t *testing.T) { is.Equal(result.valuePT2, 94) } -// func TestSolution(t *testing.T) { -// is := is.New(t) -// scan := bufio.NewScanner(bytes.NewReader(input)) +func TestSolution(t *testing.T) { + is := is.New(t) + scan := bufio.NewScanner(bytes.NewReader(input)) -// result, err := run(scan) -// is.NoErr(err) + result, err := run(scan) + is.NoErr(err) -// t.Log(result) -// is.Equal(result.valuePT1, 843) -// is.Equal(result.valuePT2, 1017) -// } + t.Log(result) + is.Equal(result.valuePT1, 843) + is.Equal(result.valuePT2, 1017) +} diff --git a/search.go b/search.go index 7a7bc99..8b16094 100644 --- a/search.go +++ b/search.go @@ -67,22 +67,26 @@ func (s *stack[T]) Pop() T { // ManhattanDistance the distance between two points measured along axes at right angles. 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 { + // Neighbors returns all neighbors to node N that should be considered next. Neighbors(N) []N + + // Cost returns Cost(a, b N) C + // Target returns true when target reached. receives node and cost. + Target(N, C) bool + // OPTIONAL: // Add heuristic for running as A* search. - // Potential(a, b N) C + // Potential(N) C // Seen modify value used by seen pruning. // Seen(N) N - // Target returns true if target reached. - // Target(N) bool } // 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. // 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 - 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 { cost C @@ -104,7 +117,7 @@ func FindPath[C integer, N comparable](g pather[C, N], start, end N) (C, []N) { position N } - NewPath := func(n *node) []N { + newPath := func(n *node) []N { var path []N for n.parent != nil { path = append(path, n.position) @@ -117,65 +130,45 @@ func FindPath[C integer, N comparable](g pather[C, N], start, end N) (C, []N) { } less := func(a, b node) bool { - return b.cost+b.potential < a.cost+a.potential + return b.cost+b.potential < a.cost+a.potential } - pq := PriorityQueue(less) - pq.Enqueue(node{position: start}) - closed[start] = false + closed := make(map[N]C) + open := PriorityQueue(less) - defer func() { - Log("queue max depth = ", pq.maxDepth, "total enqueue = ", pq.totalEnqueue, "total dequeue = ", pq.totalDequeue) - }() + open.Enqueue(node{position: start, potential: potentialFn(start)}) + closed[start] = zero - var seenFn = func(a N) N { return a } - if s, ok := g.(interface{ Seen(N) N }); ok { - seenFn = s.Seen - } + // defer func() { + // Log( + // "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 } - if s, ok := g.(interface{ Target(N) bool }); ok { - targetFn = s.Target - } - - 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(¤t) - } - - for _, nb := range g.Neighbors(n) { - seen := seenFn(nb) - if closed[seen] { - continue - } - - cost := g.Cost(n, nb) + current.cost - nextPath := node{ + for !open.IsEmpty() { + current, _ := open.Dequeue() + for _, nb := range g.Neighbors(current.position) { + next := node{ position: nb, parent: ¤t, - cost: cost, - potential: potentialFn(nb, end), + cost: g.Cost(current.position, nb) + current.cost, + potential: potentialFn(nb), } - // check if path is in open list - if _, open := closed[seen]; !open { - pq.Enqueue(nextPath) - closed[seen] = false // add to open list + + seen := seenFn(nb) + cost, ok := closed[seen] + 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 }