2
public boolean isRobotBounded(String instructions) {3
if (instructions.length() == 0) {7
Robot bender = new Robot();8
int[] start = new int[] {0, 0};10
// 4 represents the max 90 degree turns that can restart initial orientation.11
for (int i = 0; i < 4; i++) {12
boolean orientationChanged = bender.performSet(instructions);14
int[] location = bender.location;15
if (location[0] == start[0] && location[1] == start[1]) {19
// If robot never turns and the first instruction isn't at start, exit.20
else if (!orientationChanged) {34
boolean orientationChangeCheck;38
location = new int[] {0, 0};41
orientations = new int[][] {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};43
// Start pointing north45
orientation = orientations[orientationPos];47
// Track if robot has turned48
orientationChangeCheck = false;51
public boolean performSet(String orders) {52
this.orientationChangeCheck = false;54
for (int i = 0; i < orders.length(); i++) {55
this.perform(orders.charAt(i));58
return this.orientationChangeCheck;61
public void perform(char order) {64
} else if (order == 'L' || order == 'R') {71
public void turn(char direction) {72
if (direction == 'L') {73
this.orientationPos = this.orientationPos == 0 ? 3 : this.orientationPos - 1;74
} else if (direction == 'R') {75
this.orientationPos = (this.orientationPos + 1) % 4;78
this.orientation = this.orientations[this.orientationPos];79
this.orientationChangeCheck = true;83
this.location[0] += this.orientation[0];84
this.location[1] += this.orientation[1];