mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-04-19 19:23:47 +01:00
Compare commits
No commits in common. "5a6947af2ce94befb98892c8a0baa7227f068e05" and "cdca21b3f3fe57a06850e88da228c5369c5e8d28" have entirely different histories.
5a6947af2c
...
cdca21b3f3
@ -6,24 +6,24 @@ import (
|
|||||||
orderedmap "github.com/wk8/go-ordered-map/v2"
|
orderedmap "github.com/wk8/go-ordered-map/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func exploreDFS(graph *graph, next point, dir direction, stack *stack) (*stack, bool) {
|
func exploreDFS(graph *graph, next point, direction direction, stack *stack) (*stack, bool) {
|
||||||
ns := neighbours(next)
|
ns := neighbours(next)
|
||||||
|
|
||||||
log.Debug(string(graph.valueAt(ns[dir])))
|
log.Debug(string(graph.valueAt(ns[direction])))
|
||||||
|
|
||||||
switch graph.valueAt(ns[dir]) {
|
switch graph.valueAt(ns[direction]) {
|
||||||
case '#':
|
case '#':
|
||||||
return nil, false
|
return nil, false
|
||||||
case '.':
|
case '.':
|
||||||
return stack, true
|
return stack, true
|
||||||
case '[', ']':
|
case '[', ']':
|
||||||
stack.Push(ns[dir])
|
stack.Push(ns[direction])
|
||||||
}
|
}
|
||||||
|
|
||||||
return exploreDFS(graph, ns[dir], dir, stack)
|
return exploreDFS(graph, ns[direction], direction, stack)
|
||||||
}
|
}
|
||||||
|
|
||||||
func exploreBFS(graph *graph, dir direction) (*orderedmap.OrderedMap[point, struct{}], bool) {
|
func exploreBFS(graph *graph, direction direction) (*orderedmap.OrderedMap[point, struct{}], bool) {
|
||||||
queue := newQueue()
|
queue := newQueue()
|
||||||
queue.Enqueue(graph.robot)
|
queue.Enqueue(graph.robot)
|
||||||
visited := make(map[point]struct{})
|
visited := make(map[point]struct{})
|
||||||
@ -39,38 +39,60 @@ func exploreBFS(graph *graph, dir direction) (*orderedmap.OrderedMap[point, stru
|
|||||||
visited[current] = struct{}{}
|
visited[current] = struct{}{}
|
||||||
|
|
||||||
ns := neighbours(current)
|
ns := neighbours(current)
|
||||||
switch graph.valueAt(ns[dir]) {
|
switch graph.valueAt(ns[direction]) {
|
||||||
case '.':
|
case '.':
|
||||||
if graph.valueAt(current) == '@' {
|
if graph.valueAt(current) == '@' {
|
||||||
return om, true
|
return om, true
|
||||||
}
|
}
|
||||||
case '#':
|
case '#':
|
||||||
return om, false
|
return om, false
|
||||||
case '[', ']':
|
case '[':
|
||||||
nextDirections := []direction{dir}
|
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{}{})
|
||||||
|
}
|
||||||
|
|
||||||
if graph.valueAt(ns[dir]) == '[' {
|
switch direction {
|
||||||
switch dir {
|
case N:
|
||||||
case N:
|
log.Debugf("adding %s to the queue", string(graph.valueAt(ns[NE])))
|
||||||
nextDirections = append(nextDirections, NE)
|
queue.Enqueue(ns[NE])
|
||||||
case S:
|
_, ok := om.Get(ns[NE])
|
||||||
nextDirections = append(nextDirections, SE)
|
if !ok {
|
||||||
|
om.Set(ns[NE], struct{}{})
|
||||||
}
|
}
|
||||||
} else if graph.valueAt(ns[dir]) == ']' {
|
case S:
|
||||||
switch dir {
|
log.Debugf("adding %s to the queue", string(graph.valueAt(ns[SE])))
|
||||||
case N:
|
queue.Enqueue(ns[SE])
|
||||||
nextDirections = append(nextDirections, NW)
|
_, ok := om.Get(ns[SE])
|
||||||
case S:
|
if !ok {
|
||||||
nextDirections = append(nextDirections, SW)
|
om.Set(ns[SE], struct{}{})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, nextDirection := range nextDirections {
|
case ']':
|
||||||
log.Debugf("adding %s to the queue", string(graph.valueAt(ns[nextDirection])))
|
log.Debugf("adding %s to the queue", string(graph.valueAt(ns[direction])))
|
||||||
queue.Enqueue(ns[nextDirection])
|
queue.Enqueue(ns[direction])
|
||||||
_, ok := om.Get(ns[nextDirection])
|
_, 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 {
|
if !ok {
|
||||||
om.Set(ns[nextDirection], struct{}{})
|
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{}{})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user