2024-12-18 18:48:11 +00:00
|
|
|
package two
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/onyx-and-iris/aoc2024/day-18/internal/config"
|
2024-12-19 21:32:21 +00:00
|
|
|
"github.com/onyx-and-iris/aoc2024/day-18/internal/point"
|
2024-12-18 18:48:11 +00:00
|
|
|
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)
|
2024-12-19 21:32:21 +00:00
|
|
|
log.Debug(corruptedCoords)
|
2024-12-18 18:48:11 +00:00
|
|
|
|
|
|
|
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:] {
|
2024-12-19 21:32:21 +00:00
|
|
|
nextCorruption := point.Point{X: coords[0], Y: coords[1]}
|
2024-12-18 18:48:11 +00:00
|
|
|
|
|
|
|
log.Debugf("adding corruption %v", nextCorruption)
|
2024-12-19 21:32:21 +00:00
|
|
|
graph.addCorruption(nextCorruption)
|
2024-12-18 18:48:11 +00:00
|
|
|
|
2024-12-19 21:32:21 +00:00
|
|
|
ok := graph.bfs()
|
|
|
|
if !ok {
|
2024-12-18 18:48:11 +00:00
|
|
|
return config.NumCorruptions + i + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|