refactor switch-case

This commit is contained in:
onyx-and-iris 2024-12-25 15:59:52 +00:00
parent cdca21b3f3
commit 33ee076cf1

View File

@ -6,24 +6,24 @@ import (
orderedmap "github.com/wk8/go-ordered-map/v2"
)
func exploreDFS(graph *graph, next point, direction direction, stack *stack) (*stack, bool) {
func exploreDFS(graph *graph, next point, dir direction, stack *stack) (*stack, bool) {
ns := neighbours(next)
log.Debug(string(graph.valueAt(ns[direction])))
log.Debug(string(graph.valueAt(ns[dir])))
switch graph.valueAt(ns[direction]) {
switch graph.valueAt(ns[dir]) {
case '#':
return nil, false
case '.':
return stack, true
case '[', ']':
stack.Push(ns[direction])
stack.Push(ns[dir])
}
return exploreDFS(graph, ns[direction], direction, stack)
return exploreDFS(graph, ns[dir], dir, stack)
}
func exploreBFS(graph *graph, direction direction) (*orderedmap.OrderedMap[point, struct{}], bool) {
func exploreBFS(graph *graph, dir direction) (*orderedmap.OrderedMap[point, struct{}], bool) {
queue := newQueue()
queue.Enqueue(graph.robot)
visited := make(map[point]struct{})
@ -39,60 +39,44 @@ func exploreBFS(graph *graph, direction direction) (*orderedmap.OrderedMap[point
visited[current] = struct{}{}
ns := neighbours(current)
switch graph.valueAt(ns[direction]) {
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[direction])))
queue.Enqueue(ns[direction])
_, ok := om.Get(ns[direction])
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[direction], struct{}{})
om.Set(ns[dir], 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{}{})
var additionalDirections []direction
if graph.valueAt(ns[dir]) == '[' {
switch dir {
case N:
additionalDirections = []direction{NE}
case S:
additionalDirections = []direction{SE}
}
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{}{})
} else if graph.valueAt(ns[dir]) == ']' {
switch dir {
case N:
additionalDirections = []direction{NW}
case S:
additionalDirections = []direction{SW}
}
}
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])
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[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{}{})
om.Set(ns[dir], struct{}{})
}
}
}