From 7b17f8edc103c83ee01f147b460a844f5360ae82 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Fri, 13 Dec 2024 23:40:43 +0000 Subject: [PATCH] and a little more --- day-12/internal/two/solve.go | 55 ++++++++++++------------------------ 1 file changed, 18 insertions(+), 37 deletions(-) diff --git a/day-12/internal/two/solve.go b/day-12/internal/two/solve.go index b4625a7..7e4628b 100644 --- a/day-12/internal/two/solve.go +++ b/day-12/internal/two/solve.go @@ -41,6 +41,12 @@ func Solve(buf []byte) (int, error) { return totalCost, nil } +const ( + first = iota + second + diagonal +) + func analyzeSides(kind rune, graph *graph, path path) int { log.Debugf("graph for values %s\n%s\n", string(kind), graph.debug(path.visited)) @@ -48,23 +54,20 @@ func analyzeSides(kind rune, graph *graph, path path) int { for current := range path.visited { ns := neighbours(current) - if isCorner(graph, current, ns[N], ns[E]) { - corners++ - } + for _, points := range [][]point{ + {ns[N], ns[E], point{current.x + 1, current.y - 1}}, + {ns[N], ns[W], point{current.x - 1, current.y - 1}}, + {ns[S], ns[E], point{current.x + 1, current.y + 1}}, + {ns[S], ns[W], point{current.x - 1, current.y + 1}}, + } { + if isCorner(graph, current, points[first], points[second]) { + corners++ + } - if isCorner(graph, current, ns[S], ns[E]) { - corners++ + if isInnerCorner(graph, current, points[first], points[second], points[diagonal]) { + corners++ + } } - - if isCorner(graph, current, ns[N], ns[W]) { - corners++ - } - - if isCorner(graph, current, ns[S], ns[W]) { - corners++ - } - - corners += numInnerCorner(graph, current, ns) } log.Debugf("this path has %d corners", corners) @@ -98,28 +101,6 @@ func isCorner(graph *graph, current, p, q point) bool { return false } -func numInnerCorner(graph *graph, current point, ns [4]point) int { - var corners int - - if isInnerCorner(graph, current, ns[N], ns[E], point{current.x + 1, current.y - 1}) { - corners++ - } - - if isInnerCorner(graph, current, ns[N], ns[W], point{current.x - 1, current.y - 1}) { - corners++ - } - - if isInnerCorner(graph, current, ns[S], ns[E], point{current.x + 1, current.y + 1}) { - corners++ - } - - if isInnerCorner(graph, current, ns[S], ns[W], point{current.x - 1, current.y + 1}) { - corners++ - } - - return corners -} - func isInnerCorner(graph *graph, current, p, q, diagonal point) bool { if !graph.isOutOfBounds(p) && !graph.isOutOfBounds(q) { if graph.sameKind(p, current) && graph.sameKind(q, current) &&