add new solutions
This commit is contained in:
parent
489fa73880
commit
0f5f9e331c
11 changed files with 539 additions and 0 deletions
40
solutions/10/q1041/solution.go
Normal file
40
solutions/10/q1041/solution.go
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue