41 lines
700 B
Go
41 lines
700 B
Go
// Package q1041 implements a solution for https://leetcode.com/problems/robot-bounded-in-circle/
|
|
package q1041
|
|
|
|
type Dir int8
|
|
|
|
const (
|
|
UP Dir = iota
|
|
RIGHT
|
|
DOWN
|
|
LEFT
|
|
)
|
|
|
|
func (d Dir) TurnL() Dir { return (d + 3) % 4 }
|
|
func (d Dir) TurnR() Dir { return (d + 1) % 4 }
|
|
|
|
var vects = [][2]int{{0, -1}, {1, 0}, {0, 1}, {-1, 0}}
|
|
|
|
func isRobotBounded(instructions string) bool {
|
|
x, y, d := 0, 0, UP
|
|
|
|
for range 4 {
|
|
for i := range len(instructions) {
|
|
switch instructions[i] {
|
|
case 'G':
|
|
x += vects[d][0]
|
|
y += vects[d][1]
|
|
case 'L':
|
|
d = d.TurnL()
|
|
case 'R':
|
|
d = d.TurnR()
|
|
}
|
|
}
|
|
|
|
if x == 0 && y == 0 && d == UP {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
var _ = isRobotBounded
|