-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathOthello.js
31 lines (25 loc) · 998 Bytes
/
Othello.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
// https://www.codingame.com/ide/puzzle/othello
let board = [...new Array(8)].map(() => readline().split(''));
const [color, move] = readline().split(' ');
const toBoardIndexes = (char) => [char[1] - 1, char.charCodeAt(0) - 97];
const [x, y] = toBoardIndexes(move);
const mirror = (a) => {
let m = a.join('').match(/BW+(?=b)|bW+(?=B)|WB+(?=w)|wB+(?=W)/g);
return m ? m.reduce((s, a) => s + a.length - 1, 0) : 0;
};
const checkAllDirections = (x, y) => mirror(board[y]) +
mirror(board.map((a) => a[x])) +
mirror(board.map((a, i) => a[x - y + i])) +
mirror(board.map((a, i) => a[x + y - i]));
const squareBeforeChange = board[x][y];
board[x][y] = color.toLowerCase();
const c = checkAllDirections(y, x);
board = board.join();
const tokens = {W: board.match(/W/gi).length, B: board.match(/B/gi).length};
tokens[color] += c;
tokens[color === 'W' ? 'B' : 'W'] -= c;
console.log(
squareBeforeChange !== '-' ? 'NOPE' :
c !== 0 ? `${tokens.W} ${tokens.B}` :
'NULL'
);