mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-10 06:40:47 +00:00
67 lines
1.3 KiB
Go
67 lines
1.3 KiB
Go
package two
|
|
|
|
import (
|
|
"bytes"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func Solve(buf []byte) (int, error) {
|
|
r := bytes.NewReader(buf)
|
|
graph, dirs, err := parseLines(r)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
log.Debugf("\n%s\n", graph.String())
|
|
|
|
for _, dir := range dirs {
|
|
log.Debugf("about to explore '%s' direction from robot location (%s)", string(dir), graph.robot.String())
|
|
|
|
switch directions(dir) {
|
|
case N, S:
|
|
exploreNorthSouth(graph, directions(dir))
|
|
case W, E:
|
|
exploreWestEast(graph, directions(dir))
|
|
}
|
|
|
|
log.Debugf("\n%s\n", graph.String())
|
|
}
|
|
|
|
var sum int
|
|
for y, line := range graph.data {
|
|
for x, r := range line {
|
|
if r == '[' {
|
|
sum += (100 * y) + x
|
|
}
|
|
}
|
|
}
|
|
|
|
return sum, nil
|
|
}
|
|
|
|
func exploreWestEast(graph *graph, direction direction) {
|
|
stack, ok := exploreDFS(graph, graph.robot, direction, newStack())
|
|
if !ok {
|
|
log.Debug("path ends with '#', continuing...")
|
|
return
|
|
}
|
|
|
|
for !stack.IsEmpty() {
|
|
point := stack.Pop().(point)
|
|
graph.updateBox(point, direction)
|
|
}
|
|
graph.updateRobot(direction)
|
|
}
|
|
|
|
func exploreNorthSouth(graph *graph, direction direction) {
|
|
om, ok := exploreBFS(graph, direction)
|
|
|
|
if ok {
|
|
for pair := om.Newest(); pair != nil; pair = pair.Prev() {
|
|
graph.updateBox(pair.Key, direction)
|
|
}
|
|
graph.updateRobot(direction)
|
|
}
|
|
}
|