mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-09 22:30:47 +00:00
dfs explore functions return only bool
This commit is contained in:
parent
ae590042b5
commit
7354aef102
@ -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)
|
||||
}
|
||||
|
@ -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])
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user