and a little more

This commit is contained in:
onyx-and-iris 2024-12-13 23:40:43 +00:00
parent dcf3479d88
commit 7b17f8edc1

View File

@ -41,6 +41,12 @@ func Solve(buf []byte) (int, error) {
return totalCost, nil return totalCost, nil
} }
const (
first = iota
second
diagonal
)
func analyzeSides(kind rune, graph *graph, path path) int { func analyzeSides(kind rune, graph *graph, path path) int {
log.Debugf("graph for values %s\n%s\n", string(kind), graph.debug(path.visited)) 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 { for current := range path.visited {
ns := neighbours(current) ns := neighbours(current)
if isCorner(graph, current, ns[N], ns[E]) { for _, points := range [][]point{
corners++ {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]) { if isInnerCorner(graph, current, points[first], points[second], points[diagonal]) {
corners++ 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) log.Debugf("this path has %d corners", corners)
@ -98,28 +101,6 @@ func isCorner(graph *graph, current, p, q point) bool {
return false 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 { func isInnerCorner(graph *graph, current, p, q, diagonal point) bool {
if !graph.isOutOfBounds(p) && !graph.isOutOfBounds(q) { if !graph.isOutOfBounds(p) && !graph.isOutOfBounds(q) {
if graph.sameKind(p, current) && graph.sameKind(q, current) && if graph.sameKind(p, current) && graph.sameKind(q, current) &&