Skip to content

Commit cd23589

Browse files
author
openset
committed
Add: Robot Bounded In Circle
1 parent 6d4d160 commit cd23589

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package robot_bounded_in_circle
2+
3+
func isRobotBounded(instructions string) bool {
4+
x, y, p := 0, 0, 0
5+
for _, i := range instructions {
6+
switch i {
7+
case 'G':
8+
if p == 1 || p == 3 {
9+
x += 2 - p
10+
} else {
11+
y += 1 - p
12+
}
13+
case 'L':
14+
p = (p + 3) % 4
15+
case 'R':
16+
p = (p + 1) % 4
17+
}
18+
}
19+
return p != 0 || x == 0 && y == 0
20+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package robot_bounded_in_circle
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
input string
7+
expected bool
8+
}
9+
10+
func TestIsRobotBounded(t *testing.T) {
11+
tests := [...]caseType{
12+
{
13+
input: "GGLLGG",
14+
expected: true,
15+
},
16+
{
17+
input: "GG",
18+
expected: false,
19+
},
20+
{
21+
input: "GL",
22+
expected: true,
23+
},
24+
{
25+
input: "GGLLGGGGRRGG",
26+
expected: true,
27+
},
28+
{
29+
input: "GGRRGG",
30+
expected: true,
31+
},
32+
{
33+
input: "GLRLLGLL",
34+
expected: true,
35+
},
36+
}
37+
for _, tc := range tests {
38+
output := isRobotBounded(tc.input)
39+
if output != tc.expected {
40+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)