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

87 lines
1.8 KiB
Go
Raw Normal View History

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 '[', ']':
log.Debugf("adding %s to the queue", string(graph.valueAt(ns[dir])))
queue.Enqueue(ns[dir])
_, ok := om.Get(ns[dir])
2024-12-18 14:45:27 +00:00
if !ok {
2024-12-25 15:59:52 +00:00
om.Set(ns[dir], struct{}{})
2024-12-18 14:45:27 +00:00
}
2024-12-25 15:59:52 +00:00
var additionalDirections []direction
if graph.valueAt(ns[dir]) == '[' {
switch dir {
case N:
additionalDirections = []direction{NE}
case S:
additionalDirections = []direction{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:
additionalDirections = []direction{NW}
case S:
additionalDirections = []direction{SW}
2024-12-18 14:45:27 +00:00
}
}
2024-12-25 15:59:52 +00:00
for _, dir := range additionalDirections {
log.Debugf("adding %s to the queue", string(graph.valueAt(ns[dir])))
queue.Enqueue(ns[dir])
_, ok := om.Get(ns[dir])
2024-12-18 14:45:27 +00:00
if !ok {
2024-12-25 15:59:52 +00:00
om.Set(ns[dir], struct{}{})
2024-12-18 14:45:27 +00:00
}
}
}
}
return om, true
}