-
Notifications
You must be signed in to change notification settings - Fork 4
/
124_bishopDiagonal.go
78 lines (60 loc) · 2.03 KB
/
124_bishopDiagonal.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
Challenge: BishopDiagonal
https://codefights.com/arcade/code-arcade/chess-tavern/wGLCfzpdcA2o9kSpD/description
In the Land Of Chess, bishops don't really like each other. In fact, when two bishops happen to stand on the same diagonal, they immediately rush towards the opposite ends of that same diagonal.
Given the initial positions (in chess notation) of two bishops, bishop1 and bishop2, calculate their future positions. Keep in mind that bishops won't move unless they see each other along the same diagonal.
Example
For bishop1 = "d7" and bishop2 = "f5", the output should be
bishopDiagonal(bishop1, bishop2) = ["c8", "h3"].
For bishop1 = "d8" and bishop2 = "b5", the output should be
bishopDiagonal(bishop1, bishop2) = ["b5", "d8"].
The bishops don't belong to the same diagonal, so they don't move.
Input/Output
[time limit] 4000ms (go)
[input] string bishop1
Coordinates of the first bishop in chess notation.
[input] string bishop2
Coordinates of the second bishop in the same notation.
[output] array.string
Coordinates of the bishops in lexicographical order after they check the diagonals they stand on.
**/
import "sort"
func bishopDiagonal(b1 string, b2 string) []string {
x1, y1 := parse(b1)
x2, y2 := parse(b2)
// if share diagonal
if abs(x1-x2) == abs(y1-y2) {
mx, my := 1, 1
if x1 < x2 {
mx = -1
}
if y1 < y2 {
my = -1
}
// calculate final positions
for x1 >= 1 && x1 < 7 && y1 >= 1 && y1 < 7 {
x1 += mx
y1 += my
}
for x2 >= 1 && x2 < 7 && y2 >= 1 && y2 < 7 {
x2 += -mx
y2 += -my
}
}
s1 := string(rune(x1) + 'a') + string(rune(y1) + '1')
s2 := string(rune(x2) + 'a') + string(rune(y2) + '1')
res := []string{s1, s2}
sort.Strings(res)
return res
}
// absolute value
func abs(x int) int {
if x < 0 { return -x }
return x
}
// parse position
func parse(b string) (int, int) {
c := int(b[0] - 'a')
r := int(b[1] - '1')
return c, r
}