Skip to content

Commit b5ba2c5

Browse files
author
Shuo
authored
Merge pull request #636 from openset/develop
Add: Alphabet Board Path
2 parents af58d23 + 92b4201 commit b5ba2c5

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package alphabet_board_path
2+
3+
import "strings"
4+
5+
func alphabetBoardPath(target string) string {
6+
var sb strings.Builder
7+
// form [x1,y1] move to [x2,y2]
8+
move := func(x1, y1, x2, y2 int) {
9+
for y1 > y2 {
10+
y1--
11+
sb.WriteByte('L')
12+
}
13+
for x1 > x2 {
14+
x1--
15+
sb.WriteByte('U')
16+
}
17+
for x1 < x2 {
18+
x1++
19+
sb.WriteByte('D')
20+
}
21+
for y1 < y2 {
22+
y1++
23+
sb.WriteByte('R')
24+
}
25+
return
26+
}
27+
28+
x1, y1 := 0, 0
29+
for _, r := range target {
30+
x2, y2 := coordinate(r)
31+
move(x1, y1, x2, y2)
32+
sb.WriteByte('!')
33+
x1, y1 = x2, y2
34+
}
35+
return sb.String()
36+
}
37+
38+
func coordinate(r rune) (x, y int) {
39+
a := int(r - 'a')
40+
x, y = a/5, a%5
41+
return
42+
}

0 commit comments

Comments
 (0)