Bishop Diagonal

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

  • [execution time limit] 4 seconds (js)

  • [input] string bishop1

    Coordinates of the first bishop in chess notation.

    Guaranteed constraints:
    bishop1.length = 2,
    'a' ≤ bishop1[0] ≤ 'h',
    1 ≤ bishop1[1] ≤ 8.

    Note (Chess notation): Each square of the chessboard is identified by a unique coordinate pair—a letter and a number. The vertical columns of squares from white’s left to the right are labeled ‘a’ through ‘h’. The horizontal rows of squares are numbered 1 to 8 starting from white’s side of the board. Thus each square has a unique identification as a string consisting of two characters: the first is the column label, and the second in the row number.

  • [input] string bishop1

    Coordinates of the first bishop in chess notation.

    Guaranteed constraints:
    bishop2.length = 2,
    'a' ≤ bishop2[0] ≤ 'h',
    1 ≤ bishop2[1] ≤ 8.

  • [output] array.string

    • Coordinates of the bishops in lexicographical order after they check the diagonals they stand on.

    Note (lexicographical order): A way of sorting strings, similar to alphabetical order but generalized to all kinds of characters.

    When comparing two strings, s and t, we compare each pair of characters with equal indices (s[i] and t[i]), starting with i = 0:

    • if s[i] < t[i] or if s[i] is undefined, then we conclude that s < t,
    • if s[i] > t[i] or if t[i] is undefined, then we conclude that s > t,
    • if s[i] = t[i] then we repeat the process by comparing s[i + 1] to t[i + 1].

    If the two strings have equal length and s[i] = t[i] for every character, then we conclude that s = t

    Examples:

    • "snow" > "snoring" because the first string contains a greater character at index i = 2
    • "cat" < "caterpillar" because the first string is undefined at index i = 3

[JavaScript (ES6)] Syntax Tips

1
2
3
4
5
6
// Prints help message to the console
// Returns a string
function helloWorld(name) {
console.log("This prints to the console when you Run Tests");
return "Hello, " + name;
}

Solution


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
function bishopDiagonal(bishop1, bishop2) {
  var bishopCoords1 = bishop1.split("");
  var bishopCoords2 = bishop2.split("");
  var tmp;
  bishopCoords1[0] = 1 + bishopCoords1[0].charCodeAt(0) - "a".charCodeAt(0);
  bishopCoords1[1] = Number(bishopCoords1[1]);
  bishopCoords2[0] = 1 + bishopCoords2[0].charCodeAt(0) - "a".charCodeAt(0);
  bishopCoords2[1] = Number(bishopCoords2[1]);
  var newPos1 = bishopCoords1.slice();
  var newPos2 = bishopCoords2.slice();
  if (
    bishopCoords1[0] + bishopCoords1[1] ===
    bishopCoords2[0] + bishopCoords2[1]
  ) {
    if (bishopCoords1[0] + bishopCoords1[1] > 9) {
      newPos1[0] = 8;
      newPos2[1] = 8;
      newPos1[1] = bishopCoords1[0] + bishopCoords1[1] - 8;
      newPos2[0] = bishopCoords1[0] + bishopCoords1[1] - 8;
    } else {
      newPos1[0] = 1;
      newPos2[1] = 1;
      newPos1[1] = bishopCoords1[0] + bishopCoords1[1] - 1;
      newPos2[0] = bishopCoords1[0] + bishopCoords1[1] - 1;
    }
  } else if (
    bishopCoords1[0] - bishopCoords1[1] ===
    bishopCoords2[0] - bishopCoords2[1]
  ) {
    if (bishopCoords1[0] - bishopCoords1[1] < 0) {
      newPos1[0] = 1;
      newPos1[1] = 1 + bishopCoords1[1] - bishopCoords1[0];
      newPos2[0] = 8 + bishopCoords1[0] - bishopCoords1[1];
      newPos2[1] = 8;
    } else {
      newPos1[0] = 1 + bishopCoords1[0] - bishopCoords1[1];
      newPos1[1] = 1;
      newPos2[0] = 8;
      newPos2[1] = 8 - (bishopCoords1[0] - bishopCoords1[1]);
    }
  }

  newPos1[0] = String.fromCharCode(newPos1[0] - 1 + "a".charCodeAt(0));
  newPos1 = newPos1.join("");
  newPos2[0] = String.fromCharCode(newPos2[0] - 1 + "a".charCodeAt(0));
  newPos2 = newPos2.join("");
  return [newPos1, newPos2].sort();
}