Created
September 4, 2018 13:13
-
-
Save almond-bongbong/c42143ec4c7054ee0d2c7fa3904426e2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function turn(m, n, board, answer) { | |
var removeItem = []; | |
for (var i = 1; i < m; i++) { | |
for (var j = 1; j < n; j++) { | |
var item = board[i][j]; | |
if (item === '0') continue; | |
var topLeft = board[i - 1][j - 1]; | |
var top = board[i - 1][j]; | |
var left = board[i][j - 1]; | |
if (item === topLeft && item === top && item === left) { | |
removeItem.push((i - 1) + ',' + (j - 1)); | |
removeItem.push((i - 1) + ',' + j); | |
removeItem.push(i + ',' + (j - 1)); | |
removeItem.push(i + ',' + j); | |
} | |
} | |
} | |
removeItem = removeItem.reduce((acc, cur) => { | |
if (acc.indexOf(cur) < 0) { | |
acc.push(cur); | |
} | |
return acc; | |
}, []); | |
if (removeItem.length > 0) { | |
answer += removeItem.length; | |
removeItem.forEach(i => { | |
var y = parseInt(i.split(',')[0], 10); | |
var x = parseInt(i.split(',')[1], 10); | |
board[y][x] = '0'; | |
}); | |
for (var i = 1; i < m; i++) { | |
for (var j = 0; j < n; j++) { | |
var flag = true; | |
var sY = i; | |
var sX = j; | |
while(sY > 0) { | |
if (board[sY][sX] === '0') { | |
board[sY][sX] = board[sY - 1][sX]; | |
board[sY - 1][sX] = '0'; | |
} | |
sY--; | |
} | |
} | |
} | |
answer = turn(m, n, board, answer); | |
} | |
return answer; | |
} | |
function solution(m, n, board) { | |
var answer = 0; | |
board = board.map(row => row.split('')); | |
answer = turn(m, n, board, answer); | |
return answer; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment