2024-12-18 14:45:27 +00:00
|
|
|
package two
|
|
|
|
|
|
|
|
import (
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
orderedmap "github.com/wk8/go-ordered-map/v2"
|
|
|
|
)
|
|
|
|
|
2024-12-25 15:59:52 +00:00
|
|
|
func exploreDFS(graph *graph, next point, dir direction, stack *stack) (*stack, bool) {
|
2024-12-18 14:45:27 +00:00
|
|
|
ns := neighbours(next)
|
|
|
|
|
2024-12-25 15:59:52 +00:00
|
|
|
log.Debug(string(graph.valueAt(ns[dir])))
|
2024-12-18 14:45:27 +00:00
|
|
|
|
2024-12-25 15:59:52 +00:00
|
|
|
switch graph.valueAt(ns[dir]) {
|
2024-12-18 14:45:27 +00:00
|
|
|
case '#':
|
|
|
|
return nil, false
|
|
|
|
case '.':
|
|
|
|
return stack, true
|
|
|
|
case '[', ']':
|
2024-12-25 15:59:52 +00:00
|
|
|
stack.Push(ns[dir])
|
2024-12-18 14:45:27 +00:00
|
|
|
}
|
|
|
|
|
2024-12-25 15:59:52 +00:00
|
|
|
return exploreDFS(graph, ns[dir], dir, stack)
|
2024-12-18 14:45:27 +00:00
|
|
|
}
|
|
|
|
|
2024-12-25 15:59:52 +00:00
|
|
|
func exploreBFS(graph *graph, dir direction) (*orderedmap.OrderedMap[point, struct{}], bool) {
|
2024-12-18 14:45:27 +00:00
|
|
|
queue := newQueue()
|
|
|
|
queue.Enqueue(graph.robot)
|
|
|
|
visited := make(map[point]struct{})
|
|
|
|
om := orderedmap.New[point, struct{}]()
|
|
|
|
|
|
|
|
for !queue.IsEmpty() {
|
|
|
|
current := queue.Dequeue().(point)
|
|
|
|
|
|
|
|
_, ok := visited[current]
|
|
|
|
if ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
visited[current] = struct{}{}
|
|
|
|
|
|
|
|
ns := neighbours(current)
|
2024-12-25 15:59:52 +00:00
|
|
|
switch graph.valueAt(ns[dir]) {
|
2024-12-18 14:45:27 +00:00
|
|
|
case '.':
|
|
|
|
if graph.valueAt(current) == '@' {
|
|
|
|
return om, true
|
|
|
|
}
|
|
|
|
case '#':
|
|
|
|
return om, false
|
2024-12-25 15:59:52 +00:00
|
|
|
case '[', ']':
|
2024-12-25 21:01:35 +00:00
|
|
|
nextDirections := []direction{dir}
|
2024-12-18 14:45:27 +00:00
|
|
|
|
2024-12-25 15:59:52 +00:00
|
|
|
if graph.valueAt(ns[dir]) == '[' {
|
|
|
|
switch dir {
|
|
|
|
case N:
|
2024-12-25 21:01:35 +00:00
|
|
|
nextDirections = append(nextDirections, NE)
|
2024-12-25 15:59:52 +00:00
|
|
|
case S:
|
2024-12-25 21:01:35 +00:00
|
|
|
nextDirections = append(nextDirections, SE)
|
2024-12-18 14:45:27 +00:00
|
|
|
}
|
2024-12-25 15:59:52 +00:00
|
|
|
} else if graph.valueAt(ns[dir]) == ']' {
|
|
|
|
switch dir {
|
|
|
|
case N:
|
2024-12-25 21:01:35 +00:00
|
|
|
nextDirections = append(nextDirections, NW)
|
2024-12-25 15:59:52 +00:00
|
|
|
case S:
|
2024-12-25 21:01:35 +00:00
|
|
|
nextDirections = append(nextDirections, SW)
|
2024-12-18 14:45:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-25 21:01:35 +00:00
|
|
|
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])
|
2024-12-18 14:45:27 +00:00
|
|
|
if !ok {
|
2024-12-25 21:01:35 +00:00
|
|
|
om.Set(ns[nextDirection], struct{}{})
|
2024-12-18 14:45:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return om, true
|
|
|
|
}
|