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

40 lines
1004 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"
"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)
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:] {
nextCorruption := point.Point{X: coords[0], Y: coords[1]}
2024-12-18 18:48:11 +00:00
log.Debugf("adding corruption %v", nextCorruption)
graph.addCorruption(nextCorruption)
2024-12-18 18:48:11 +00:00
ok := graph.bfs()
if !ok {
2024-12-18 18:48:11 +00:00
return config.NumCorruptions + i + 1
}
}
return 0
}