mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-10 06:40:47 +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, graph, Visited, count)
|
||
|
log.Debugf("path walked: \n%s\n", graph.debug(Visited))
|
||
|
|
||
|
return count, nil
|
||
|
}
|
||
|
|
||
|
func nextStep(point point, graph *graph, visited map[coords]struct{}, count int) 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(graph.data)-1 && point.direction == S ||
|
||
|
point.X == len(graph.data[point.Y])-1 && point.direction == E {
|
||
|
return count
|
||
|
}
|
||
|
|
||
|
next := nextPoint(point)
|
||
|
log.Debug(next.String())
|
||
|
log.Debug(string(graph.data[next.Y][next.X]))
|
||
|
if graph.data[next.Y][next.X] == '#' {
|
||
|
next.recalibrate()
|
||
|
log.Debugf("switched direction to %d", next.direction)
|
||
|
}
|
||
|
|
||
|
return nextStep(next, graph, visited, count)
|
||
|
}
|