Level
프로그래머스 Lv2
Day
15일
kakao
2018 카카오 블라인드
 
function solution(m, n, board) {
  const dx = [1, 1, 0];
  const dy = [0, 1, 1];
  let ans = 0;

  const rotated = Array(n)
    .fill(0)
    .map((row) => Array(m).fill(0));

    
  // 시계방향 회전
  for (let y = 0; y < m; y++) {
    for (let x = 0; x < n; x++) {
      rotated[x][m - 1 - y] = board[y][x];
    }
  }

  let hasSquare = true;
    
  const isSquare = (y, x) => {
      const root = rotated[y][x];

      for (let i = 0; i < 3; i++) {
        const xx = x + dx[i];
        const yy = y + dy[i];

        if (rotated[yy] === undefined || rotated[yy][xx] === undefined) // rotated를 벗어날 떄
          return false;
        if (root !== rotated[yy][xx]) { // root와 값이 다를 때
          return false;
        }
      }
      return true;
  };    
    
  while (hasSquare) {
    const removed = Array(n)
      .fill(0)
      .map((row) => Array(m).fill(0));

    // 현재 rotate에서 얻게 될 score 계산
    let totalRemoved = 0;

    for (let y = 0; y < n; y++) {
      for (let x = 0; x < m; x++) {

        if (isSquare(y, x)) {
          // delete 마킹, 현재 블락 totalRemoved 덧셈
          let removedCount = 4;
          if(removed[y][x]){
              removedCount--;
          }
          removed[y][x] = 1;

          for (let i = 0; i < 3; i++) {
            const xx = x + dx[i];
            const yy = y + dy[i];

            if (removed[yy][xx]) {
              removedCount--;
            }
            removed[yy][xx] = 1;
          }

          totalRemoved += removedCount;
        }
      }
    }

    if (totalRemoved === 0) {
      hasSquare = false;
    }

    // 보드 블락 내리기
    for (let y = 0; y < n; y++) {
      const row = rotated[y];
      rotated[y] = row.filter((char, i) => !removed[y][i]);
    }
    ans += totalRemoved;
  }

  return ans;
}