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(directions(dir), graph) case W, E: exploreWestEast(directions(dir), graph) } 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(direction direction, g *graph) { stack := newStack() if ok := exploreDFS(g, g.robot, direction, stack); !ok { log.Debug("path ends with '#', continuing...") return } for !stack.IsEmpty() { point := stack.Pop().(point) g.updateBox(point, direction) } g.updateRobot(direction) } func exploreNorthSouth(direction direction, g *graph) { om, ok := exploreBFS(g, direction) if ok { for pair := om.Newest(); pair != nil; pair = pair.Prev() { g.updateBox(pair.Key, direction) } g.updateRobot(direction) } }