Last active
September 17, 2020 10:49
-
-
Save RP-3/09cfd654c2acaddd77e87301f15b8aea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
IDEA: Iff: | |
- The robot is facing North after the instructions, AND | |
- The robot was displaced by any non-zero amount, THEN | |
the robot will hare off into the sunset. | |
Otherwise, the robot will loop endlessly. | |
Proof: Lots of drawing on square-ruled paper. | |
*/ | |
func isRobotBounded(instructions string) bool { | |
vectors := [4][2]int{{1, 0}, {0, 1}, {-1, 0}, {0, -1}} | |
heading := uint8(0) | |
pos := [2]int{0, 0} | |
for _, c := range instructions { | |
switch c { | |
case 'G': | |
pos[0] += vectors[heading][0] | |
pos[1] += vectors[heading][1] | |
case 'L': | |
heading = (heading + 3) % 4 | |
case 'R': | |
heading = (heading + 1) % 4 | |
} | |
} | |
return (heading != 0) || (pos[0] == 0 && pos[1] == 0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment