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

81 lines
1.7 KiB
Go

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