package leetcode;

/**
 * https://leetcode.com/problems/walking-robot-simulation/
 */
public class Problem874 {
    public int robotSim(int[] commands, int[][] obstacles) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem874 prob = new Problem874();
        System.out.println(prob.robotSim(new int[]{4, -1, 3}, new int[][]{})); // 25
        System.out.println(prob.robotSim(new int[]{4, -1, 4, -2, 4}, new int[][]{
            {2, 4}
        })); // 65
    }
}

@@ -12,16 +12,65 @@
 tcode;%0A%0A
+import java.util.HashSet;%0Aimport java.util.Set;%0A%0A
 /**%0A * h
@@ -227,334 +227,2084 @@
     
-// TODO%0A        return 0
+int max = 0;%0A        Set%3CString%3E set = new HashSet%3C%3E()
 ;%0A    
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem874 prob = new Problem874();%0A        System.out.println(prob.robotSim(new int%5B%5D%7B4,
+for (int%5B%5D obstacle : obstacles) %7B%0A            set.add(key(obstacle%5B0%5D, obstacle%5B1%5D));%0A        %7D%0A        int x = 0;%0A        int y = 0;%0A        Direction direction = Direction.UP;%0A        for (int cmd : commands) %7B%0A            if (cmd == -2 %7C%7C cmd == -1) %7B%0A                direction = newDirection(direction, cmd);%0A            %7D else %7B%0A                for (int i = 0; i %3C cmd; i++) %7B%0A                    if (direction == Direction.UP) %7B%0A                        if (set.contains(key(x, y + 1))) %7B%0A                            break;%0A                        %7D%0A                        y++;%0A                    %7D else if (direction == Direction.LEFT) %7B%0A                        if (set.contains(key(x
  -
+ 
 1, 
-3%7D, new int%5B%5D%5B%5D%7B%7D)); // 25%0A        System.out.println(prob.robotSim(new int%5B%5D%7B4, -1, 4, -2, 4%7D, new int%5B%5D%5B%5D%7B%0A            %7B2, 4%7D%0A        %7D)); // 65
+y))) %7B%0A                            break;%0A                        %7D%0A                        x--;%0A                    %7D else if (direction == Direction.DOWN) %7B%0A                        if (set.contains(key(x, y - 1))) %7B%0A                            break;%0A                        %7D%0A                        y--;%0A                    %7D else %7B // Direction.RIGHT%0A                        if (set.contains(key(x + 1, y))) %7B%0A                            break;%0A                        %7D%0A                        x++;%0A                    %7D%0A                %7D%0A                max = Math.max(max, (x * x) + (y * y));%0A            %7D%0A        %7D%0A        return max;%0A    %7D%0A%0A    private enum Direction %7B%0A        LEFT, RIGHT, UP, DOWN%0A    %7D%0A%0A    private static Direction newDirection(Direction current, int cmd) %7B%0A        if (current == Direction.UP) %7B%0A            return (cmd == -2) ? Direction.LEFT : Direction.RIGHT;%0A        %7D else if (current == Direction.LEFT) %7B%0A            return (cmd == -2) ? Direction.DOWN : Direction.UP;%0A        %7D else if (current == Direction.DOWN) %7B%0A            return (cmd == -2) ? Direction.RIGHT : Direction.LEFT;%0A        %7D else %7B // Direction.RIGHT%0A            return (cmd == -2) ? Direction.UP : Direction.DOWN;%0A        %7D%0A    %7D%0A%0A    private static String key(int x, int y) %7B%0A        return x + %22%7C%22 + y;
 %0A   

Solve problem 874