# Problem Description
This is a matrix transformation problem where elements need to be rearranged according to specific movement rules. The input is a 2D matrix of integers, and the output is a transformed matrix of the same dimensions where elements have been moved according to a cyclical pattern.

# Visual Facts
1. The image shows 4 different example transformations:
   - 2×3 matrix transformation
   - 4×3 matrix transformation
   - 4×4 matrix transformation
   - 3×5 matrix transformation

2. Colors are used to track element movements:
   - Light blue cells
   - Orange cells
   - Green cells

3. All input matrices have consistent color patterns:
   - last row is always light blue
   - last column elements (except last row) are green
   - Remaining elements are orange

4. Output matrices maintain the same dimensions as their inputs

# Visual Patterns
1. Movement Rules:
   - Elements move in a clockwise spiral pattern
   - Given a matrix of M*N dimensions, the transformation rules are as follows: (1-indexed)
     - Bottom row elements move to the top row, Matrix[M][i] -> Matrix[1][i]
     - Last column elements (except bottom row) move to the leftmost position of the row above, Matrix[i][N] -> Matrix[i+1][1] (i < M)
     - Other elements move one position right and one position down, Matrix[i][j] -> Matrix[i+1][j+1] (i < M, j < N)

2. Pattern Consistency:
   - The transformation rules remain consistent regardless of matrix dimensions
   - The transformation preserves all original values, just rearranges them
   - The pattern works for both positive and negative integers


# Solution
def solution(matrix: list[list[int]]) -> list[list[int]]:
    """
    Manipulates a 2D matrix of integers according to the demonstrated transformation rules.

    Parameters:
        matrix (list[list[int]]): A 2D matrix of integers.

    Returns:
        list[list[int]]: The transformed matrix of the same dimensions as the input.       
    """
    # Initialize the row and column sizes
    new_matrix = [[0] * len(matrix[0]) for _ in range(len(matrix))]

    new_matrix[0] = matrix[-1]

    for i in range(1, len(matrix)):
        new_matrix[i][0] = matrix[i - 1][-1]

    for i in range(1, len(matrix)):
        for j in range(1, len(matrix[0])):
            new_matrix[i][j] = matrix[i - 1][j - 1]


    return new_matrix


# Test Script
def test():
    test_cases = [
        ([[-3, 1, 7], [3, 2, -7]], [[3, 2, -7], [7, -3, 1]]),
        ([[-3, 1, 7], [5, -6, -4], [-5, 4, -2], [3, 2, -7]], [[3, 2, -7], [7, -3, 1], [-4, 5, -6], [-2, -5, 4]]),
        ([[8, 2, 6, 1], [4, 0, 7, 9], [2, 1, 6, 7], [3, 2, 4, 5]], [[3, 2, 4, 5], [1, 8, 2, 6], [9, 4, 0, 7], [7, 2, 1, 6]]),
        ([[8, 1, 5, 9, 1, 4]], [[8, 1, 5, 9, 1, 4]]),
        ([[0, 1, 0, 2], [7, 6, 6, 1], [7, 3, 3, 6], [6, 6, 4, 5], [3, 9, 7, 1], [6, 1, 9, 5]], [[6, 1, 9, 5], [2, 0, 1, 0], [1, 7, 6, 6], [6, 7, 3, 3], [5, 6, 6, 4], [1, 3, 9, 7]]),
        ([[6], [3]], [[3], [6]]),
        ([[4, 9], [2, 4]], [[2, 4], [9, 4]]),
        ([[6, 8, 8, 7], [5, 0, 4, 6], [9, 9, 9, 8], [2, 1, 7, 9], [4, 8, 1, 9], [9, 9, 0, 0]], [[9, 9, 0, 0], [7, 6, 8, 8], [6, 5, 0, 4], [8, 9, 9, 9], [9, 2, 1, 7], [9, 4, 8, 1]]),
        ([[8, 5, 2, 0, 8, 5], [8, 1, 4, 3, 5, 8], [6, 0, 8, 0, 6, 3], [5, 1, 0, 5, 9, 0]], [[5, 1, 0, 5, 9, 0], [5, 8, 5, 2, 0, 8], [8, 8, 1, 4, 3, 5], [3, 6, 0, 8, 0, 6]]),
        ([[6, 9, 1, 3], [5, 8, 9, 8], [5, 2, 7, 9], [8, 0, 9, 6]], [[8, 0, 9, 6], [3, 6, 9, 1], [8, 5, 8, 9], [9, 5, 2, 7]]),
    ]

    for matrix, expected_output in test_cases:
        try:
            output = solution(matrix)
        except Exception as e:
            error_message = (
                f"An exception occurred while running the test case:\n"
                f"`matrix`: {matrix}\n\n"
                f"Exception: '{str(e)}'"
            )
            assert False, error_message

        if output != expected_output:
            error_message = (
                f"The following Test Case failed:\n"
                f"`matrix`: {matrix}\n\n"
                f"Actual Output: {output}\n"
                f"Expected Output: {expected_output}"
            )
            assert False, error_message

test()