aoc2024/day-15/internal/two/explore.go

103 lines
2.2 KiB
Go

package two
import (
log "github.com/sirupsen/logrus"
orderedmap "github.com/wk8/go-ordered-map/v2"
)
func exploreDFS(graph *graph, next point, direction direction, stack *stack) (*stack, bool) {
ns := neighbours(next)
log.Debug(string(graph.valueAt(ns[direction])))
switch graph.valueAt(ns[direction]) {
case '#':
return nil, false
case '.':
return stack, true
case '[', ']':
stack.Push(ns[direction])
}
return exploreDFS(graph, ns[direction], direction, stack)
}
func exploreBFS(graph *graph, direction direction) (*orderedmap.OrderedMap[point, struct{}], bool) {
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)
switch graph.valueAt(ns[direction]) {
case '.':
if graph.valueAt(current) == '@' {
return om, true
}
case '#':
return om, false
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[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{}{})
}
}
}
}
return om, true
}