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