mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-10 06:40:47 +00:00
58 lines
1.1 KiB
Go
58 lines
1.1 KiB
Go
package one
|
|
|
|
import (
|
|
"bytes"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/onyx-and-iris/aoc2024/day-10/internal/queue"
|
|
)
|
|
|
|
const target = '9'
|
|
|
|
func Solve(buf []byte) (int, error) {
|
|
r := bytes.NewReader(buf)
|
|
graph, err := parseLines(r)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
var totalScore int
|
|
queue := queue.New[point]()
|
|
for _, pos := range graph.startPositions {
|
|
if graph.isOutOfBounds(pos) {
|
|
continue
|
|
}
|
|
queue.Enqueue(pos)
|
|
|
|
endpoints := make(map[point]struct{})
|
|
for !queue.IsEmpty() {
|
|
current := queue.Dequeue()
|
|
|
|
if graph.valueAt(current) == target {
|
|
_, ok := endpoints[current]
|
|
if !ok {
|
|
endpoints[current] = struct{}{}
|
|
}
|
|
continue
|
|
}
|
|
|
|
for _, n := range neighbours(current) {
|
|
if graph.isOutOfBounds(n) {
|
|
continue
|
|
}
|
|
|
|
if graph.valueAt(n)-graph.valueAt(current) == 1 {
|
|
log.Tracef("pushing %v with value %s back onto the queue\n", n, string(graph.valueAt(n)))
|
|
queue.Enqueue(n)
|
|
}
|
|
}
|
|
}
|
|
|
|
log.Debugf("score for trailhead %v - %d\n", pos, len(endpoints))
|
|
totalScore += len(endpoints)
|
|
}
|
|
|
|
return totalScore, nil
|
|
}
|