도장찍기

WooBuntu·2020년 9월 2일
0

JS 100제

목록 보기
29/34

도장찍기

const shape = [
  [1, 1, 1, 2],
  [2, 0, 0, 0],
  [1, 1, 1, 1],
  [0, 0, 0, 0],
];

const count = 1;

function rotate90(matrix) {
  const newMatrix = new Array(matrix.length)
    .fill(null)
    .map(() => new Array(matrix.length).fill(null));
  for (let row = 0; row < matrix.length; row++) {
    for (let col = 0; col < matrix.length; col++) {
      newMatrix[col][matrix.length - row - 1] = matrix[row][col];
    }
  }

  return newMatrix;
}

function stamp(shape, count) {
  let rotated = JSON.parse(JSON.stringify(shape));
  for (let i = 0; i < count % 4; i++) {
    rotated = rotate90(rotated);
  }
  return rotated.map((row, idxOfRow) => {
    return row.map((col, idxOfCol) => {
      return col + shape[idxOfRow][idxOfCol];
    });
  });
}

stamp(shape, count).forEach((row) => console.log(row));

0개의 댓글