-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsolution.js
63 lines (57 loc) · 1.16 KB
/
solution.js
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
"use strict";
/**
* @typedef {Object} Bishop
* @property {number} row
* @property {number} column
*/
/**
* @param {Bishop} bishop1
* @param {Bishop} bishop2
*/
function isAttacking(bishop1, bishop2) {
const rowDiff = Math.abs(bishop1.row - bishop2.row);
const columnDiff = Math.abs(bishop1.column - bishop2.column);
return rowDiff === columnDiff;
}
/**
*
* @param {[Bishop]} bishops
*/
function countAttacks(bishops) {
let attackCount = 0;
for (let i = 0; i < bishops.length; i++) {
for (let j = i + 1; j < bishops.length; j++) {
if (isAttacking(bishops[i], bishops[j])) attackCount++;
}
}
return attackCount;
}
/*
[b 0 0 0 0]
[0 b b 0 0]
[0 0 b 0 0]
[0 0 0 0 0]
[b 0 0 0 0]
*/
const bishops1 = [
{ row: 0, column: 0 },
{ row: 1, column: 2 },
{ row: 1, column: 1 },
{ row: 2, column: 2 },
{ row: 4, column: 0 }
];
/*
[b 0 0 0 0]
[0 0 b 0 0]
[0 0 b 0 0]
[0 0 0 0 0]
[b 0 0 0 0]
*/
const bishops2 = [
{ row: 0, column: 0 },
{ row: 1, column: 2 },
{ row: 2, column: 2 },
{ row: 4, column: 0 }
];
console.log(countAttacks(bishops1)); // 4
console.log(countAttacks(bishops2)); // 2