mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-10 06:40:47 +00:00
87 lines
1.8 KiB
Go
87 lines
1.8 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) (*stack, bool) {
|
|
ns := neighbours(next)
|
|
|
|
log.Debug(string(graph.valueAt(ns[dir])))
|
|
|
|
switch graph.valueAt(ns[dir]) {
|
|
case '#':
|
|
return nil, false
|
|
case '.':
|
|
return stack, 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 '[', ']':
|
|
log.Debugf("adding %s to the queue", string(graph.valueAt(ns[dir])))
|
|
queue.Enqueue(ns[dir])
|
|
_, ok := om.Get(ns[dir])
|
|
if !ok {
|
|
om.Set(ns[dir], struct{}{})
|
|
}
|
|
|
|
var additionalDirections []direction
|
|
if graph.valueAt(ns[dir]) == '[' {
|
|
switch dir {
|
|
case N:
|
|
additionalDirections = []direction{NE}
|
|
case S:
|
|
additionalDirections = []direction{SE}
|
|
}
|
|
} else if graph.valueAt(ns[dir]) == ']' {
|
|
switch dir {
|
|
case N:
|
|
additionalDirections = []direction{NW}
|
|
case S:
|
|
additionalDirections = []direction{SW}
|
|
}
|
|
}
|
|
|
|
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])
|
|
if !ok {
|
|
om.Set(ns[dir], struct{}{})
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return om, true
|
|
}
|