Compare commits

..

No commits in common. "5a6947af2ce94befb98892c8a0baa7227f068e05" and "cdca21b3f3fe57a06850e88da228c5369c5e8d28" have entirely different histories.

View File

@ -6,24 +6,24 @@ 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, direction direction, stack *stack) (*stack, bool) {
ns := neighbours(next)
log.Debug(string(graph.valueAt(ns[dir])))
log.Debug(string(graph.valueAt(ns[direction])))
switch graph.valueAt(ns[dir]) {
switch graph.valueAt(ns[direction]) {
case '#':
return nil, false
case '.':
return stack, true
case '[', ']':
stack.Push(ns[dir])
stack.Push(ns[direction])
}
return exploreDFS(graph, ns[dir], dir, stack)
return exploreDFS(graph, ns[direction], direction, stack)
}
func exploreBFS(graph *graph, dir direction) (*orderedmap.OrderedMap[point, struct{}], bool) {
func exploreBFS(graph *graph, direction direction) (*orderedmap.OrderedMap[point, struct{}], bool) {
queue := newQueue()
queue.Enqueue(graph.robot)
visited := make(map[point]struct{})
@ -39,38 +39,60 @@ func exploreBFS(graph *graph, dir direction) (*orderedmap.OrderedMap[point, stru
visited[current] = struct{}{}
ns := neighbours(current)
switch graph.valueAt(ns[dir]) {
switch graph.valueAt(ns[direction]) {
case '.':
if graph.valueAt(current) == '@' {
return om, true
}
case '#':
return om, false
case '[', ']':
nextDirections := []direction{dir}
case '[':
log.Debugf("adding %s to the queue", string(graph.valueAt(ns[direction])))
queue.Enqueue(ns[direction])
_, ok := om.Get(ns[direction])
if !ok {
om.Set(ns[direction], struct{}{})
}
if graph.valueAt(ns[dir]) == '[' {
switch dir {
case N:
nextDirections = append(nextDirections, NE)
case S:
nextDirections = append(nextDirections, SE)
switch direction {
case N:
log.Debugf("adding %s to the queue", string(graph.valueAt(ns[NE])))
queue.Enqueue(ns[NE])
_, ok := om.Get(ns[NE])
if !ok {
om.Set(ns[NE], struct{}{})
}
} else if graph.valueAt(ns[dir]) == ']' {
switch dir {
case N:
nextDirections = append(nextDirections, NW)
case S:
nextDirections = append(nextDirections, SW)
case S:
log.Debugf("adding %s to the queue", string(graph.valueAt(ns[SE])))
queue.Enqueue(ns[SE])
_, ok := om.Get(ns[SE])
if !ok {
om.Set(ns[SE], struct{}{})
}
}
for _, nextDirection := range nextDirections {
log.Debugf("adding %s to the queue", string(graph.valueAt(ns[nextDirection])))
queue.Enqueue(ns[nextDirection])
_, ok := om.Get(ns[nextDirection])
case ']':
log.Debugf("adding %s to the queue", string(graph.valueAt(ns[direction])))
queue.Enqueue(ns[direction])
_, ok := om.Get(ns[direction])
if !ok {
om.Set(ns[direction], struct{}{})
}
switch direction {
case N:
log.Debugf("adding %s to the queue", string(graph.valueAt(ns[NW])))
queue.Enqueue(ns[NW])
_, ok := om.Get(ns[NW])
if !ok {
om.Set(ns[nextDirection], struct{}{})
om.Set(ns[NW], struct{}{})
}
case S:
log.Debugf("adding %s to the queue", string(graph.valueAt(ns[SW])))
queue.Enqueue(ns[SW])
_, ok := om.Get(ns[SW])
if !ok {
om.Set(ns[SW], struct{}{})
}
}
}