From 7354aef1026dcdf1c4bcf302ebb784699cd320cf Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Tue, 7 Jan 2025 17:33:55 +0000 Subject: [PATCH] dfs explore functions return only bool --- day-15/internal/one/solve.go | 16 ++++++++-------- day-15/internal/two/explore.go | 6 +++--- day-15/internal/two/solve.go | 22 +++++++++++----------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/day-15/internal/one/solve.go b/day-15/internal/one/solve.go index df58174..4aac242 100644 --- a/day-15/internal/one/solve.go +++ b/day-15/internal/one/solve.go @@ -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) } diff --git a/day-15/internal/two/explore.go b/day-15/internal/two/explore.go index 83d21e5..49b0e28 100644 --- a/day-15/internal/two/explore.go +++ b/day-15/internal/two/explore.go @@ -6,16 +6,16 @@ import ( 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) log.Debug(string(graph.valueAt(ns[dir]))) switch graph.valueAt(ns[dir]) { case '#': - return nil, false + return false case '.': - return stack, true + return true case '[', ']': stack.Push(ns[dir]) } diff --git a/day-15/internal/two/solve.go b/day-15/internal/two/solve.go index 57ba61d..03d2b6b 100644 --- a/day-15/internal/two/solve.go +++ b/day-15/internal/two/solve.go @@ -20,9 +20,9 @@ func Solve(buf []byte) (int, error) { switch directions(dir) { case N, S: - exploreNorthSouth(graph, directions(dir)) + exploreNorthSouth(directions(dir), graph) case W, E: - exploreWestEast(graph, directions(dir)) + exploreWestEast(directions(dir), graph) } log.Debugf("\n%s\n", graph.String()) @@ -40,27 +40,27 @@ func Solve(buf []byte) (int, error) { return sum, nil } -func exploreWestEast(graph *graph, direction direction) { - stack, ok := exploreDFS(graph, graph.robot, direction, newStack()) - if !ok { +func exploreWestEast(direction direction, g *graph) { + stack := newStack() + if ok := exploreDFS(g, g.robot, direction, stack); !ok { log.Debug("path ends with '#', continuing...") return } for !stack.IsEmpty() { point := stack.Pop().(point) - graph.updateBox(point, direction) + g.updateBox(point, direction) } - graph.updateRobot(direction) + g.updateRobot(direction) } -func exploreNorthSouth(graph *graph, direction direction) { - om, ok := exploreBFS(graph, direction) +func exploreNorthSouth(direction direction, g *graph) { + om, ok := exploreBFS(g, direction) if ok { 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) } }