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" 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) 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 '#': case '#':
return nil, false return nil, false
case '.': case '.':
return stack, true return stack, true
case '[', ']': 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 := newQueue()
queue.Enqueue(graph.robot) queue.Enqueue(graph.robot)
visited := make(map[point]struct{}) visited := make(map[point]struct{})
@ -39,60 +39,44 @@ func exploreBFS(graph *graph, direction direction) (*orderedmap.OrderedMap[point
visited[current] = struct{}{} visited[current] = struct{}{}
ns := neighbours(current) ns := neighbours(current)
switch graph.valueAt(ns[direction]) { switch graph.valueAt(ns[dir]) {
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 '[', ']':
log.Debugf("adding %s to the queue", string(graph.valueAt(ns[direction]))) log.Debugf("adding %s to the queue", string(graph.valueAt(ns[dir])))
queue.Enqueue(ns[direction]) queue.Enqueue(ns[dir])
_, ok := om.Get(ns[direction]) _, ok := om.Get(ns[dir])
if !ok { if !ok {
om.Set(ns[direction], struct{}{}) om.Set(ns[dir], struct{}{})
} }
switch direction { var additionalDirections []direction
case N: if graph.valueAt(ns[dir]) == '[' {
log.Debugf("adding %s to the queue", string(graph.valueAt(ns[NE]))) switch dir {
queue.Enqueue(ns[NE]) case N:
_, ok := om.Get(ns[NE]) additionalDirections = []direction{NE}
if !ok { case S:
om.Set(ns[NE], struct{}{}) additionalDirections = []direction{SE}
} }
case S: } else if graph.valueAt(ns[dir]) == ']' {
log.Debugf("adding %s to the queue", string(graph.valueAt(ns[SE]))) switch dir {
queue.Enqueue(ns[SE]) case N:
_, ok := om.Get(ns[SE]) additionalDirections = []direction{NW}
if !ok { case S:
om.Set(ns[SE], struct{}{}) additionalDirections = []direction{SW}
} }
} }
case ']': for _, dir := range additionalDirections {
log.Debugf("adding %s to the queue", string(graph.valueAt(ns[direction]))) log.Debugf("adding %s to the queue", string(graph.valueAt(ns[dir])))
queue.Enqueue(ns[direction]) queue.Enqueue(ns[dir])
_, ok := om.Get(ns[direction]) _, ok := om.Get(ns[dir])
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[NW], struct{}{}) om.Set(ns[dir], 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{}{})
} }
} }
} }