aoc2024/day-18/internal/two/solve.go

40 lines
999 B
Go
Raw Normal View History

2024-12-18 18:48:11 +00:00
package two
import (
"bytes"
"fmt"
"github.com/onyx-and-iris/aoc2024/day-18/internal/config"
log "github.com/sirupsen/logrus"
)
func Solve(buf []byte, config config.Config) (string, error) {
r := bytes.NewReader(buf)
graph, corruptedCoords, err := parseLines(r, config)
if err != nil {
return "", err
}
log.Debugf("start: %v end: %v", graph.start, graph.end)
indx := runUntilNoPath(graph, corruptedCoords, config)
return fmt.Sprintf("%d,%d", corruptedCoords[indx][0], corruptedCoords[indx][1]), nil
}
func runUntilNoPath(graph *graph, corruptedCoords [][]int, config config.Config) int {
for i, coords := range corruptedCoords[config.NumCorruptions+1:] {
nextCorruption := point{coords[0], coords[1]}
log.Debugf("adding corruption %v", nextCorruption)
graph.addCorruption(coords)
path, err := graph.dijkstra(graph.start, graph.end)
if err != nil {
log.Debug(err)
return config.NumCorruptions + i + 1
}
log.Debugf("\n%s\n", graph.debug(path))
}
return 0
}