• + 1 comment

    My solution using C++. It's not the best or the most efficient take so i'll try to look for a better approach.

    void matrixRotation(std::vector<std::vector<int>> matrix, int r)
    {
        std::vector<std::vector<int>> res = matrix;
        /* Max number of iterations needed to rotate every layer in the 2d vector */
        uint16_t maxIterations = std::min(matrix.size(), matrix[0].size()) / 2;
        for (int iteration = 0; iteration < maxIterations; iteration++) {
            /* Limit of looping through each layer */
            int32_t row = matrix.size() - 2 * iteration, rowIterator = iteration;
            int32_t col = matrix[0].size() - 2 * iteration, colIterator = iteration;
            int perimeter = (2 * (row + col) - 4), tempRot = r;
            r = r % perimeter; // Removing redundant rotations
            int loopAround = 0;
            /* Top side of the current layer */
            while (loopAround < r && colIterator < col + iteration) {
                loopAround++, colIterator++;
            }
            if (colIterator - iteration >= col) {
                loopAround--, colIterator--;
            }
            /* Right side of the current layer */
            while (loopAround < r && rowIterator < row + iteration) {
                loopAround++, rowIterator++;
            }
            if (rowIterator - iteration >= row) {
                loopAround--, rowIterator--;
            }
            /* Bottom side of the current layer */
            while (loopAround < r && colIterator >= iteration) {
                loopAround++, colIterator--;
            }
            if (colIterator < iteration) {
                loopAround--, colIterator++;
            }
            /* Left side of the current layer */
            while (loopAround < r && rowIterator >= iteration) {
                loopAround++, rowIterator--;
            }
            if (rowIterator < iteration) {
                loopAround--, rowIterator++;
            }
            /* Now we have the location of the element to start the rotation from at matrix[rowIterator][colIterator] */
            /* Top side of the current layer except last element */
            for (int layerCol = iteration; layerCol < col + iteration - 1; layerCol++) {
                res[iteration][layerCol] = matrix[rowIterator][colIterator];
                if (rowIterator == iteration && colIterator < col + iteration - 1 && colIterator >= iteration) {
                    colIterator++;
                } else if (colIterator == col + iteration - 1 && rowIterator < row + iteration - 1 && rowIterator >= iteration) {
                    rowIterator++;
                } else if (rowIterator == row + iteration - 1 && colIterator <= col + iteration - 1 && colIterator > iteration) {
                    colIterator--;
                } else if (colIterator == iteration && rowIterator <= row + iteration - 1 && rowIterator > iteration) {
                    rowIterator--;
                }
            }
            /* Right side of the current layer except last element */
            for (int layerRow = iteration; layerRow < row + iteration - 1; layerRow++) {
                res[layerRow][col + iteration - 1] = matrix[rowIterator][colIterator];
                if (rowIterator == iteration && colIterator < col + iteration - 1 && colIterator >= iteration) {
                    colIterator++;
                } else if (colIterator == col + iteration - 1 && rowIterator < row + iteration - 1 && rowIterator >= iteration) {
                    rowIterator++;
                } else if (rowIterator == row + iteration - 1 && colIterator <= col + iteration - 1 && colIterator > iteration) {
                    colIterator--;
                } else if (colIterator == iteration && rowIterator <= row + iteration - 1 && rowIterator > iteration) {
                    rowIterator--;
                }
            }
            /* Bottom side of the current layer except last element */
            for (int layerCol = col + iteration - 1; layerCol > iteration; layerCol--) {
                res[row + iteration - 1][layerCol] = matrix[rowIterator][colIterator];
                if (rowIterator == iteration && colIterator < col + iteration - 1 && colIterator >= iteration) {
                    colIterator++;
                } else if (colIterator == col + iteration - 1 && rowIterator < row + iteration - 1 && rowIterator >= iteration) {
                    rowIterator++;
                } else if (rowIterator == row + iteration - 1 && colIterator <= col + iteration - 1 && colIterator > iteration) {
                    colIterator--;
                } else if (colIterator == iteration && rowIterator <= row + iteration - 1 && rowIterator > iteration) {
                    rowIterator--;
                }
            }
            /* Left side of the current layer except last element */
            for (int layerRow = row + iteration - 1; layerRow > iteration; layerRow--) {
                res[layerRow][iteration] = matrix[rowIterator][colIterator];
                if (rowIterator == iteration && colIterator < col + iteration - 1 && colIterator >= iteration) {
                    colIterator++;
                } else if (colIterator == col + iteration - 1 && rowIterator < row + iteration - 1 && rowIterator >= iteration) {
                    rowIterator++;
                } else if (rowIterator == row + iteration - 1 && colIterator <= col + iteration - 1 && colIterator > iteration) {
                    colIterator--;
                } else if (colIterator == iteration && rowIterator <= row + iteration - 1 && rowIterator > iteration) {
                    rowIterator--;
                }
            }
            r = tempRot;
        }
    
        for (int i = 0; i < matrix.size(); i++) {
            for (int j = 0; j < matrix[0].size(); j++) {
                std::cout << matrix[i][j] << " ";
            }
            std::cout << std::endl;
        }
        std::cout << std::endl;
        for (int i = 0; i < res.size(); i++) {
            for (int j = 0; j < res[0].size(); j++) {
                std::cout << res[i][j] << " ";
            }
            std::cout << std::endl;
        }
    }