Perhaps slightly overkill to have a separate class to deal with the instructions, it was an artifact of my initial concept which would execute the same instructions x amount of times, before I realised it was the wrong approach. Still, it beats 100% so far and is extremely easy to follow!
private class DirectionalVector2 {
public DirectionalVector2(int x = 0, int y = 0, int direction = 0) {
this.x = x;
this.y = y;
this.direction = direction;
}
public int x = 0;
public int y = 0;
public int direction = 0;
public void ExecuteSteps(string instructions) {
for (int i = 0; i < instructions.Length; i++) {
char c = instructions[i];
if (c == 'G') {
Move(1);
} else if (c == 'L') {
RotateLeft();
} else if (c == 'R') {
RotateRight();
}
}
}
public void Move(int steps) {
if(direction == 0) {
y += steps;
}else if(direction == 1) {
x += steps;
} else if (direction == 2) {
y -= steps;
} else if (direction == 3) {
x -= steps;
}
}
public void RotateRight() {
direction++;
if (direction == 4) direction = 0;
}
public void RotateLeft() {
direction--;
if (direction == -1) direction = 3;
}
}
public bool IsRobotBounded(string instructions) {
DirectionalVector2 step = new DirectionalVector2();
step.ExecuteSteps(instructions);
return (step.x == 0 && step.y == 0) || step.direction != 0;
}
}