From 08706ecc11707fe31ecee5c329465defe45feb04 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Fri, 6 Dec 2024 22:23:01 +0000 Subject: [PATCH] make use of graph factory function. move isLoop logic into guard clause. --- day-06/internal/one/graph.go | 2 +- day-06/internal/two/graph.go | 6 +++--- day-06/internal/two/solve.go | 10 ++++------ day-06/internal/two/util.go | 2 +- 4 files changed, 9 insertions(+), 11 deletions(-) diff --git a/day-06/internal/one/graph.go b/day-06/internal/one/graph.go index 9a9c9d3..ef58ca1 100644 --- a/day-06/internal/one/graph.go +++ b/day-06/internal/one/graph.go @@ -7,8 +7,8 @@ import ( ) type graph struct { - data []string startPoint point + data []string } func newGraph() *graph { diff --git a/day-06/internal/two/graph.go b/day-06/internal/two/graph.go index e02bbcc..ec45ad2 100644 --- a/day-06/internal/two/graph.go +++ b/day-06/internal/two/graph.go @@ -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 { diff --git a/day-06/internal/two/solve.go b/day-06/internal/two/solve.go index caefdfc..643530b 100644 --- a/day-06/internal/two/solve.go +++ b/day-06/internal/two/solve.go @@ -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 }() diff --git a/day-06/internal/two/util.go b/day-06/internal/two/util.go index cf3ab89..19f5169 100644 --- a/day-06/internal/two/util.go +++ b/day-06/internal/two/util.go @@ -7,7 +7,7 @@ import ( ) func parseLines(r io.Reader) (*graph, error) { - graph := newGraph() + graph := &graph{} count := 0 scanner := bufio.NewScanner(r)