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, ok := explore(graph, graph.robot, directions(dir), newStack())
|
||
|
if !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(graph *graph, next point, direction direction, stack *stack) (*stack, bool) {
|
||
|
ns := neighbours(next)
|
||
|
|
||
|
log.Debug(string(graph.valueAt(ns[direction])))
|
||
|
|
||
|
switch graph.valueAt(ns[direction]) {
|
||
|
case '#':
|
||
|
return nil, false
|
||
|
case '.':
|
||
|
return stack, true
|
||
|
case 'O':
|
||
|
stack.Push(ns[direction])
|
||
|
}
|
||
|
|
||
|
return explore(graph, ns[direction], direction, stack)
|
||
|
}
|