import java.util.*;
import java.lang.*;
import java.io.*;

class Main {
    public static String fixFirstInstruction (List<String> instructions, List<Integer> target){
        String[] instructionsList = instructions.toArray(new String[0]);
        ArrayList<String> firstInstructionsList = new ArrayList<>(Arrays.asList("FORWARD","TURN RIGHT","TURN LEFT","BACK"));
        System.err.println("TARGET (x,y) : (" + target.get(0) + "," + target.get(1)+")");
        for(String fixFirstInstruction : firstInstructionsList){
            int x=0;
            int y=0;
            char dir='R';
            instructionsList[0]=fixFirstInstruction;
            for (String instruction : instructionsList) {
                System.err.println(instruction);
                if (dir == 'R' && instruction.equals("FORWARD"))
                    x += 1;
                else if (dir == 'R' && instruction.equals("TURN LEFT"))
                    dir = 'U';
                else if (dir == 'R' && instruction.equals("TURN RIGHT"))
                    dir = 'D';
                else if (dir == 'R' && instruction.equals("BACK"))
                    x -= 1;
                else if (dir == 'D' && instruction.equals("FORWARD"))
                    y -= 1;
                else if (dir == 'D' && instruction.equals("TURN LEFT"))
                    dir = 'R';
                else if (dir == 'D' && instruction.equals("TURN RIGHT"))
                    dir = 'L';
                else if (dir == 'D' && instruction.equals("BACK"))
                    y += 1;
                else if (dir == 'L' && instruction.equals("FORWARD"))
                    x -= 1;
                else if (dir == 'L' && instruction.equals("TURN LEFT"))
                    dir = 'D';
                else if (dir == 'L' && instruction.equals("TURN RIGHT"))
                    dir = 'U';
                else if (dir == 'L' && instruction.equals("BACK"))
                    x += 1;
                else if (dir == 'U' && instruction.equals("FORWARD"))
                    y += 1;
                else if (dir == 'U' && instruction.equals("TURN LEFT"))
                    dir = 'L';
                else if (dir == 'U' && instruction.equals("TURN RIGHT"))
                    dir = 'R';
                else if (dir == 'U' && instruction.equals("BACK"))
                    y -= 1;
            }
            System.err.println("FINAL (x,y) : (" + x + "," + y+")");
            if(x==target.get(0) && y==target.get(1))
                return "Replace instruction 1 with " + fixFirstInstruction;
        }
        return "";
    }

    public static void main(String[] args) {
        List<String> instructions= Arrays.asList("FORWARD","TURN RIGHT","FORWARD","TURN RIGHT","FORWARD","FORWARD",
                "FORWARD","FORWARD");//FINAL (-3;-1)
        List<Integer> target = Arrays.asList(-1,4);
        System.out.println(fixFirstInstruction (instructions,target)); //ATTENDU : TURN RIGHT
    }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: