-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobotBoundedCircle.go
More file actions
41 lines (37 loc) · 872 Bytes
/
RobotBoundedCircle.go
File metadata and controls
41 lines (37 loc) · 872 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main
func isRobotBounded(instructions string) bool {
x, y := 0, 0
// Direction: 1 = North, 2 = East, 3 = South, 4 = West
direction := 1
for i := 0; i < len(instructions); i++ {
switch instructions[i] {
case 'G':
switch direction {
case 1:
y++
case 2:
x++
case 3:
y--
case 4:
x--
}
case 'L':
direction--
if direction == 0 {
direction = 4
}
case 'R':
direction++
if direction == 5 {
direction = 1
}
}
}
if (x == 0 && y == 0) || direction != 1 {
return true
}
return false
}
// Runtime:=1ms Beats 76.19%
// Memory:=2.19MB Beats 69.05%