-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproblem_068.js
85 lines (72 loc) Β· 1.68 KB
/
problem_068.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* Finds total attack count
* @param {number[][]} boardSize
* @param {number[][]} positions
* @return {number}
*/
function findTotalAttack(boardSize, positions) {
let attackPositions = [];
let totalCount = 0;
// Iterate through each section
for (let location in positions) {
let countAndAttack = addBishop(position[location], attackPositions, boardSize);
// Increase count and attack positions
totalCount += countAndAttack[0];
attackPositions.add(countAndAttack[1]);
}
return totalCount;
}
/**
* Adds Bishop and totals count
* @param {number[][]} location
* @param {number[][]} attackPositions
* @param {number} boardSize
* @return {*}
*/
function addBishop(location, attackPositions, boardSize) {
let currCount = 0;
let currAttackPositions = [];
// Adding bishop to existing location
if (attackPositions.includes(location)) count += 1;
// Check Diagonal
let i = location[0];
let j = location[1];
while (i > 0 && j > 0) {
i -= 1;
j -= 1;
currAttackPositions.push([i, j]);
}
// Check Diagonal
i = location[0];
j = location[1];
while (i > 0 && j < boardSize - 1) {
i -= 1;
j += 1;
currAttackPositions.push([i, j]);
}
// Check Diagonal
i = location[0];
j = location[1];
while (i < boardSize - 1 && j > 0) {
i += 1;
j -= 1;
currAttackPositions.push([i, j]);
}
// Check Diagonal
i = location[0];
j = location[1];
while (i < boardSize - 1 && j < boardSize - 1) {
i += 1;
j += 1;
currAttackPositions.push([i, j]);
}
attackPositions.push(currAttackPositions);
return [totalCount, attackPositions];
}
const boardSize = 5;
const positions = [
[0, 0],
[1, 2],
[2, 2],
[4, 0],
];