mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-10 14:50:46 +00:00
52 lines
1.1 KiB
Go
52 lines
1.1 KiB
Go
package one
|
|
|
|
import (
|
|
"bytes"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
var Visited = make(map[coords]struct{})
|
|
|
|
func Solve(buf []byte) (int, error) {
|
|
r := bytes.NewReader(buf)
|
|
graph, err := parseLines(r)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
log.Debug(graph.startPoint.String())
|
|
|
|
var count int
|
|
count = nextStep(graph.startPoint, Visited, count, graph)
|
|
log.Debugf("path walked: \n%s\n", graph.debug(Visited))
|
|
|
|
return count, nil
|
|
}
|
|
|
|
func nextStep(point point, visited map[coords]struct{}, count int, g *graph) int {
|
|
_, ok := visited[point.coords]
|
|
if !ok {
|
|
visited[point.coords] = struct{}{}
|
|
count++
|
|
}
|
|
|
|
log.Debugf("count: %d\n", count)
|
|
|
|
if point.X == 0 && point.direction == W ||
|
|
point.Y == 0 && point.direction == N ||
|
|
point.Y == len(g.data)-1 && point.direction == S ||
|
|
point.X == len(g.data[point.Y])-1 && point.direction == E {
|
|
return count
|
|
}
|
|
|
|
next := nextPoint(point)
|
|
log.Debug(next.String())
|
|
log.Debug(string(g.data[next.Y][next.X]))
|
|
if g.data[next.Y][next.X] == '#' {
|
|
next.recalibrate()
|
|
log.Debugf("switched direction to %d", next.direction)
|
|
}
|
|
|
|
return nextStep(next, visited, count, g)
|
|
}
|