mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-10 06:40:47 +00:00
58 lines
1.1 KiB
Go
58 lines
1.1 KiB
Go
package one
|
|
|
|
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
|
|
}
|
|
|
|
for _, dir := range dirs {
|
|
log.Debugf("about to explore '%s' direction from robot location %v", string(dir), graph.robot)
|
|
|
|
stack := newStack()
|
|
if ok := explore(graph.robot, directions(dir), stack, graph); !ok {
|
|
log.Debug("path ends with '#', continuing...")
|
|
continue
|
|
}
|
|
|
|
for !stack.IsEmpty() {
|
|
point := stack.Pop().(point)
|
|
graph.updateBox(point, directions(dir))
|
|
}
|
|
graph.updateRobot(directions(dir))
|
|
|
|
log.Debugf("\n%s\n", graph.String())
|
|
}
|
|
|
|
var sum int
|
|
for box := range graph.boxes {
|
|
sum += (100 * box.y) + box.x
|
|
}
|
|
|
|
return sum, nil
|
|
}
|
|
|
|
func explore(next point, direction direction, stack *stack, g *graph) bool {
|
|
ns := neighbours(next)
|
|
|
|
log.Debug(string(g.valueAt(ns[direction])))
|
|
|
|
switch g.valueAt(ns[direction]) {
|
|
case '#':
|
|
return false
|
|
case '.':
|
|
return true
|
|
case 'O':
|
|
stack.Push(ns[direction])
|
|
}
|
|
|
|
return explore(ns[direction], direction, stack, g)
|
|
}
|