mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-09 14:20:48 +00:00
make use of graph factory function.
move isLoop logic into guard clause.
This commit is contained in:
parent
8bf3f603d5
commit
08706ecc11
@ -7,8 +7,8 @@ import (
|
||||
)
|
||||
|
||||
type graph struct {
|
||||
data []string
|
||||
startPoint point
|
||||
data []string
|
||||
}
|
||||
|
||||
func newGraph() *graph {
|
||||
|
@ -7,12 +7,12 @@ import (
|
||||
)
|
||||
|
||||
type graph struct {
|
||||
data []string
|
||||
startPoint point
|
||||
data []string
|
||||
}
|
||||
|
||||
func newGraph() *graph {
|
||||
return &graph{}
|
||||
func newGraph(startPoint point, data []string) *graph {
|
||||
return &graph{startPoint: startPoint, data: data}
|
||||
}
|
||||
|
||||
func (g *graph) String() string {
|
||||
|
@ -24,9 +24,7 @@ func Solve(buf []byte) (int, error) {
|
||||
continue
|
||||
}
|
||||
|
||||
g := newGraph()
|
||||
g.startPoint = graph.startPoint
|
||||
g.data = slices.Clone(graph.data)
|
||||
g := newGraph(graph.startPoint, slices.Clone(graph.data))
|
||||
g.data[loc.Y] = replaceAtIndex(g.data[loc.Y], 'O', loc.X)
|
||||
|
||||
p := g.startPoint
|
||||
@ -43,13 +41,13 @@ func Solve(buf []byte) (int, error) {
|
||||
}
|
||||
|
||||
_, ok := visited[p]
|
||||
if !ok {
|
||||
visited[p] = struct{}{}
|
||||
} else {
|
||||
if ok {
|
||||
log.Tracef("loop: \n%s\n", g.trace(visited))
|
||||
isLoop <- true
|
||||
return
|
||||
}
|
||||
|
||||
visited[p] = struct{}{}
|
||||
}
|
||||
isLoop <- false
|
||||
}()
|
||||
|
@ -7,7 +7,7 @@ import (
|
||||
)
|
||||
|
||||
func parseLines(r io.Reader) (*graph, error) {
|
||||
graph := newGraph()
|
||||
graph := &graph{}
|
||||
|
||||
count := 0
|
||||
scanner := bufio.NewScanner(r)
|
||||
|
Loading…
Reference in New Issue
Block a user