aoc2024/day-20/internal/one/solve.go

73 lines
1.3 KiB
Go
Raw Normal View History

2024-12-20 20:48:13 +00:00
package one
import (
"bytes"
"math"
"github.com/onyx-and-iris/aoc2024/day-20/internal/point"
log "github.com/sirupsen/logrus"
"github.com/elliotchance/orderedmap/v3"
)
const cheatChar = 'X'
var Path *orderedmap.OrderedMap[point.Point, int]
func Solve(buf []byte) (int, error) {
r := bytes.NewReader(buf)
graph, err := parseLines(r)
if err != nil {
return 0, err
}
Path = orderedmap.NewOrderedMap[point.Point, int]()
Path.Set(graph.start, 0)
Path = graph.path(graph.start, Path, 1)
log.Debugf("initial path: %d\n", Path.Len()-1)
log.Debugf("Unique path:\n%s\n", graph.debug(Path))
visited := make(map[point.Point]struct{})
var sum int
for point := range Path.Keys() {
for dir, n := range neighbours(point) {
if graph.isOutOfBounds(n) {
continue
}
if graph.valueAt(n) != '#' {
continue
}
if _, ok := visited[n]; ok {
continue
}
if graph.isValidCheatPosition(n) {
2024-12-20 23:23:53 +00:00
b := graph.getConnectingPoint(direction(dir), n)
2024-12-20 20:48:13 +00:00
graph.set(n, 'X')
v1, ok := Path.Get(point)
if !ok {
log.Fatalf("%v not in path", point)
}
v2, ok := Path.Get(b)
if !ok {
log.Fatalf("%v not in path", b)
}
diff := int(math.Abs(float64(v1-v2))) - 1
log.Debugf("diff: %d\n", diff)
if diff >= 100 {
sum++
}
visited[n] = struct{}{}
}
}
}
return sum, nil
}