mirror of
https://github.com/onyx-and-iris/aoc2023.git
synced 2024-11-15 15:10:49 +00:00
46 lines
1.0 KiB
Go
46 lines
1.0 KiB
Go
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) {
|
|
diffs += numDiffs(image[lower], image[upper])
|
|
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
|
|
}
|