aoc2023/day-13/one.go

63 lines
1.5 KiB
Go
Raw Normal View History

2023-12-14 03:44:14 +00:00
package main
import (
"strings"
log "github.com/sirupsen/logrus"
)
// findReflection returns the reflection point for an image
func findReflection(image []string) (int, bool) {
walkToEdge := func(lower, upper int) bool {
for lower >= 0 && upper < len(image) && strings.Compare(image[lower], image[upper]) == 0 {
lower--
upper++
}
lower++
return (lower == 0 || upper == len(image))
}
for i := 0; i < len(image)-1; i++ {
if strings.Compare(image[i], image[i+1]) == 0 {
log.Debug("start point: ", image[i], " vs ", image[i+1])
if walkToEdge(i, i+1) {
return i, true
}
}
}
return 0, false
}
// horizontalReflection returns the reflection point of a horizontal mirror
func horizontalReflection(image img, fn func(image []string) (int, bool)) (int, bool) {
n, ok := fn(image.raw)
return n, ok
}
// verticalReflection returns the reflection point of a vertical mirror
func verticalReflection(image img, fn func(image []string) (int, bool)) (int, bool) {
n, ok := fn(image.transposed())
return n, ok
}
// one returns a calculation based on reflection points for all images.
func one(lines []string) int {
parselines(lines)
sum := 0
for _, image := range images.img {
log.Debug("checking for horizontal reflection")
n, ok := horizontalReflection(image, findReflection)
if ok {
sum += 100 * (n + 1)
}
log.Debug("checking for vertical reflection")
n, ok = verticalReflection(image, findReflection)
if ok {
sum += (n + 1)
}
}
return sum
}