aoc2024/day-15/internal/one/solve.go

58 lines
1.1 KiB
Go
Raw Normal View History

2024-12-16 11:56:53 +00:00
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)
2025-01-07 17:33:55 +00:00
stack := newStack()
if ok := explore(graph.robot, directions(dir), stack, graph); !ok {
2024-12-16 11:56:53 +00:00
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
}
2025-01-07 17:33:55 +00:00
func explore(next point, direction direction, stack *stack, g *graph) bool {
2024-12-16 11:56:53 +00:00
ns := neighbours(next)
2025-01-07 17:33:55 +00:00
log.Debug(string(g.valueAt(ns[direction])))
2024-12-16 11:56:53 +00:00
2025-01-07 17:33:55 +00:00
switch g.valueAt(ns[direction]) {
2024-12-16 11:56:53 +00:00
case '#':
2025-01-07 17:33:55 +00:00
return false
2024-12-16 11:56:53 +00:00
case '.':
2025-01-07 17:33:55 +00:00
return true
2024-12-16 11:56:53 +00:00
case 'O':
stack.Push(ns[direction])
}
2025-01-07 17:33:55 +00:00
return explore(ns[direction], direction, stack, g)
2024-12-16 11:56:53 +00:00
}