mirror of
https://github.com/onyx-and-iris/aoc2023.git
synced 2024-09-27 10:16:59 +01:00
49 lines
1.1 KiB
Go
49 lines
1.1 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) {
|
||
|
n := numDiffs(image[lower], image[upper])
|
||
|
log.Debug(n, " difference(s) found at line ", lower)
|
||
|
diffs += n
|
||
|
lower--
|
||
|
upper++
|
||
|
}
|
||
|
lower++
|
||
|
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
|
||
|
}
|