dfs explore functions return only bool

This commit is contained in:
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 {
log.Debugf("about to explore '%s' direction from robot location %v", string(dir), graph.robot)
stack, ok := explore(graph, graph.robot, directions(dir), newStack())
if !ok {
stack := newStack()
if ok := explore(graph.robot, directions(dir), stack, graph); !ok {
log.Debug("path ends with '#', continuing...")
continue
}
@@ -39,19 +39,19 @@ func Solve(buf []byte) (int, error) {
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)
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 '#':
return nil, false
return false
case '.':
return stack, true
return true
case 'O':
stack.Push(ns[direction])
}
return explore(graph, ns[direction], direction, stack)
return explore(ns[direction], direction, stack, g)
}