Skip to content

Commit 2ba89a3

Browse files
author
openset
committed
Add: Robot Bounded In Circle
1 parent cd23589 commit 2ba89a3

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed
Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
package robot_bounded_in_circle
22

33
func isRobotBounded(instructions string) bool {
4-
x, y, p := 0, 0, 0
4+
x, y, dx, dy := 0, 0, 0, 1
55
for _, i := range instructions {
66
switch i {
77
case 'G':
8-
if p == 1 || p == 3 {
9-
x += 2 - p
10-
} else {
11-
y += 1 - p
12-
}
8+
x, y = x+dx, y+dy
139
case 'L':
14-
p = (p + 3) % 4
10+
dx, dy = -dy, dx
1511
case 'R':
16-
p = (p + 1) % 4
12+
dx, dy = dy, -dx
1713
}
1814
}
19-
return p != 0 || x == 0 && y == 0
15+
return x == 0 && y == 0 || dx != 0 || dy != 1
2016
}

problems/robot-bounded-in-circle/robot_bounded_in_circle_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ func TestIsRobotBounded(t *testing.T) {
3333
input: "GLRLLGLL",
3434
expected: true,
3535
},
36+
{
37+
input: "GLGRGLGLGLGL",
38+
expected: false,
39+
},
3640
}
3741
for _, tc := range tests {
3842
output := isRobotBounded(tc.input)

0 commit comments

Comments
 (0)