import java.util.*;

public class RepairDroid
{
    public static final String INITIAL_INPUT = Integer.toString(DroidMovement.NORTH);

    public RepairDroid (Vector<String> instructions, boolean debug)
    {
        _debug = debug;
        _theComputer = new Intcode(instructions, INITIAL_INPUT, _debug);
        _currentLocation = new Coordinate(0, 0);  // starting location
        _theMap = new Maze();

        _theMap.addContent(_currentLocation, TileId.TRAVERSE);

        _visited = null;
    }

    public final int moveToOxygenStation ()
    {
        int numberOfSteps = 0;

        _visited = new ArrayList<Coordinate>();

        // create a map first!

        recursiveSearch(_currentLocation);

        return numberOfSteps;
    }

    public void printGrid ()
    {
        System.out.println(_theMap);
    }

    // need to reset the computer if we backtrack!
    
    private boolean recursiveSearch (Coordinate from)
    {
        if (_theMap.isWall(from) || _theMap.isExplored(from))
            return false;
        
        _currentLocation = from;
        _visited.add(_currentLocation);

        _theMap.addContent(_currentLocation, TileId.TRAVERSE);

        if (_theMap.isOxygenStation(from))
            return true;

        //while (!_theComputer.hasHalted())
        {
            Coordinate to = new Coordinate(from.getX(), from.getY()+1);

            System.out.println("\n"+_theMap);

            if (!tryToMove(String.valueOf(DroidMovement.NORTH), from, to))
            {
                to = new Coordinate(from.getX(), from.getY()-1);

                System.out.println("\n"+_theMap);

                if (!tryToMove(String.valueOf(DroidMovement.SOUTH), from, to))
                {
                    to = new Coordinate(from.getX()+1, from.getY());

                    System.out.println("\n"+_theMap);

                    if (!tryToMove(String.valueOf(DroidMovement.EAST), from, to))
                    {
                        to = new Coordinate(from.getX()-1, from.getY());

                        System.out.println("\n"+_theMap);

                        return tryToMove(String.valueOf(DroidMovement.WEST), from, to);
                    }
                }
            }
        }

        return false;
    }

    private boolean tryToMove (String direction, Coordinate from, Coordinate to)
    {
        System.out.println("**Trying to move from: "+from+" to "+to+" with direction "+DroidMovement.toString(direction));

        _theComputer.setInput(direction);

        _theComputer.executeUntilInput();

        System.out.println("**Waiting for input and output is: "+_theComputer.hasOutput());

        if (_theComputer.hasOutput())
        {
            int response = Integer.parseInt(_theComputer.getOutput());

            System.out.println("**Response is "+DroidStatus.toString(response));

            switch (response)
            {
                case DroidStatus.ARRIVED:
                {
                    _theMap.addContent(to, TileId.OXYGEN_STATION);
                    _currentLocation = to;

                    return true;
                }
                case DroidStatus.COLLISION:
                {
                    _theMap.addContent(to, TileId.WALL);  // didn't move as we hit a wall

                    return false;
                }
                case DroidStatus.MOVED:
                {
                    _theMap.addContent(to, TileId.TRAVERSE);
                    _currentLocation = to;

                    return recursiveSearch(to);
                }
                default:
                    System.out.println("Unknown response: "+response);
            }
        }
        else
            System.out.println("Error - no output after move instruction!");

        return false;
    }

    private final Coordinate[] getNextPositions (Coordinate coord)
    {
        Coordinate[] coords = new Coordinate[4];

        coords[0] = new Coordinate(coord.getX(), coord.getY() -1);
        coords[1] = new Coordinate(coord.getX(), coord.getY() +1);
        coords[2] = new Coordinate(coord.getX() -1, coord.getY());
        coords[3] = new Coordinate(coord.getX() +1, coord.getY());

        return coords;
    }

    private boolean _debug;
    private Intcode _theComputer;
    private Coordinate _currentLocation;
    private Maze _theMap;
    private List<Coordinate> _visited;
}
@@ -467,34 +467,8 @@
 SE);
-%0A%0A        _visited = null;
 %0A   
@@ -557,57 +557,8 @@
 0;%0A%0A
-        _visited = new ArrayList%3CCoordinate%3E();%0A%0A
     
@@ -753,119 +753,51 @@
     
-// need to reset the computer if we backtrack!
+public void foo ()%0A    %7B
 %0A    
-%0A
     
-private boolean recursiveSearch (Coordinate from)%0A
+    
     
-%7B
+/*
 %0A   
@@ -1096,27 +1096,295 @@
 n true;%0A
+            */
 %0A    
+%7D%0A%0A
     /
-/
+*%0A     * If we run into a wall then try a different direction.%0A     * If we can't move other than backwards then do that.%0A     * Don't move into areas we've already been.%0A     */%0A    %0A    private boolean recursiveSearch (Coordinate from)%0A    %7B%0A        
 while (!
@@ -1445,55 +1445,40 @@
 nate
- to = new Coordinate(from.getX(), from.getY()+1
+%5B%5D moves = getNextPositions(from
 );%0A%0A
@@ -1586,34 +1586,40 @@
 t.NORTH), from, 
-to
+moves%5B0%5D
 ))%0A            %7B
@@ -1623,74 +1623,8 @@
   %7B%0A
-                to = new Coordinate(from.getX(), from.getY()-1);%0A%0A
     
@@ -1736,34 +1736,40 @@
 t.SOUTH), from, 
-to
+moves%5B1%5D
 ))%0A             
@@ -1777,78 +1777,8 @@
   %7B%0A
-                    to = new Coordinate(from.getX()+1, from.getY());%0A%0A
     
@@ -1905,18 +1905,24 @@
 , from, 
-to
+moves%5B2%5D
 ))%0A     
@@ -1942,82 +1942,8 @@
   %7B%0A
-                        to = new Coordinate(from.getX()-1, from.getY());%0A%0A
     
@@ -2080,18 +2080,24 @@
 , from, 
-to
+moves%5B3%5D
 );%0A     
@@ -2267,24 +2267,40 @@
 te to)%0A    %7B
+                
 %0A        Sys
@@ -2408,24 +2408,165 @@
 rection));%0A%0A
+        if (_theMap.isExplored(to))%0A        %7B%0A            System.out.println(%22**Been there already.%22);%0A%0A            return false;%0A        %7D%0A%0A
         _the
@@ -3015,24 +3015,53 @@
 tus.ARRIVED:
+  // arrived at the station!!
 %0A           
@@ -3484,32 +3484,143 @@
               %7B%0A
+                    /*%0A                     * Droid moved so let's try to move again.%0A                     */%0A%0A
                 
@@ -4569,44 +4569,5 @@
 ap;%0A
-    private List%3CCoordinate%3E _visited;%0A
 %7D

Update RepairDroid.java