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