dfs explore functions return only bool

This commit is contained in:
onyx-and-iris 2025-01-07 17:33:55 +00:00
parent ae590042b5
commit 7354aef102
3 changed files with 22 additions and 22 deletions

View File

@ -16,8 +16,8 @@ func Solve(buf []byte) (int, error) {
for _, dir := range dirs { for _, dir := range dirs {
log.Debugf("about to explore '%s' direction from robot location %v", string(dir), graph.robot) log.Debugf("about to explore '%s' direction from robot location %v", string(dir), graph.robot)
stack, ok := explore(graph, graph.robot, directions(dir), newStack()) stack := newStack()
if !ok { if ok := explore(graph.robot, directions(dir), stack, graph); !ok {
log.Debug("path ends with '#', continuing...") log.Debug("path ends with '#', continuing...")
continue continue
} }
@ -39,19 +39,19 @@ func Solve(buf []byte) (int, error) {
return sum, nil return sum, nil
} }
func explore(graph *graph, next point, direction direction, stack *stack) (*stack, bool) { func explore(next point, direction direction, stack *stack, g *graph) bool {
ns := neighbours(next) ns := neighbours(next)
log.Debug(string(graph.valueAt(ns[direction]))) log.Debug(string(g.valueAt(ns[direction])))
switch graph.valueAt(ns[direction]) { switch g.valueAt(ns[direction]) {
case '#': case '#':
return nil, false return false
case '.': case '.':
return stack, true return true
case 'O': case 'O':
stack.Push(ns[direction]) stack.Push(ns[direction])
} }
return explore(graph, ns[direction], direction, stack) return explore(ns[direction], direction, stack, g)
} }

View File

@ -6,16 +6,16 @@ import (
orderedmap "github.com/wk8/go-ordered-map/v2" orderedmap "github.com/wk8/go-ordered-map/v2"
) )
func exploreDFS(graph *graph, next point, dir direction, stack *stack) (*stack, bool) { func exploreDFS(graph *graph, next point, dir direction, stack *stack) bool {
ns := neighbours(next) ns := neighbours(next)
log.Debug(string(graph.valueAt(ns[dir]))) log.Debug(string(graph.valueAt(ns[dir])))
switch graph.valueAt(ns[dir]) { switch graph.valueAt(ns[dir]) {
case '#': case '#':
return nil, false return false
case '.': case '.':
return stack, true return true
case '[', ']': case '[', ']':
stack.Push(ns[dir]) stack.Push(ns[dir])
} }

View File

@ -20,9 +20,9 @@ func Solve(buf []byte) (int, error) {
switch directions(dir) { switch directions(dir) {
case N, S: case N, S:
exploreNorthSouth(graph, directions(dir)) exploreNorthSouth(directions(dir), graph)
case W, E: case W, E:
exploreWestEast(graph, directions(dir)) exploreWestEast(directions(dir), graph)
} }
log.Debugf("\n%s\n", graph.String()) log.Debugf("\n%s\n", graph.String())
@ -40,27 +40,27 @@ func Solve(buf []byte) (int, error) {
return sum, nil return sum, nil
} }
func exploreWestEast(graph *graph, direction direction) { func exploreWestEast(direction direction, g *graph) {
stack, ok := exploreDFS(graph, graph.robot, direction, newStack()) stack := newStack()
if !ok { if ok := exploreDFS(g, g.robot, direction, stack); !ok {
log.Debug("path ends with '#', continuing...") log.Debug("path ends with '#', continuing...")
return return
} }
for !stack.IsEmpty() { for !stack.IsEmpty() {
point := stack.Pop().(point) point := stack.Pop().(point)
graph.updateBox(point, direction) g.updateBox(point, direction)
} }
graph.updateRobot(direction) g.updateRobot(direction)
} }
func exploreNorthSouth(graph *graph, direction direction) { func exploreNorthSouth(direction direction, g *graph) {
om, ok := exploreBFS(graph, direction) om, ok := exploreBFS(g, direction)
if ok { if ok {
for pair := om.Newest(); pair != nil; pair = pair.Prev() { for pair := om.Newest(); pair != nil; pair = pair.Prev() {
graph.updateBox(pair.Key, direction) g.updateBox(pair.Key, direction)
} }
graph.updateRobot(direction) g.updateRobot(direction)
} }
} }