# Problem Description
This problem involves performing a pooling operation on a given matrix. The pooling operation reduces the size of the matrix by selecting specific elements from sub-regions of the original matrix. The goal is to implement this transformation according to the patterns shown in the examples.

# Visual Facts
1. Example Case 1:
   - Input: 3×3 matrix
   - Output: 1×1 matrix
   - The value 1 is selected from the upper-left position of the input matrix.

2. Example Case 2:
   - Input: 6×6 matrix
   - Output: 2×2 matrix
   - Each 3×3 section in the input matrix maps to one value in the output matrix.
   - The output values [1, 0, 2, 3] come from specific positions in the input matrix.

3. Example Case 3:
   - Input: 9×9 matrix
   - Output: 3×3 matrix
   - Each 3×3 section in the input matrix maps to one value in the output matrix.
   - The output matrix contains [1, 3, 4, 0, 0, 5, 2, 2, 3].

# Visual Patterns
1. Size Reduction Pattern:
   - The output matrix size is reduced by a factor of 3 in each dimension.
   - Input dimensions must be multiples of 3.
   - An input matrix of size n×n results in an output matrix of size (n/3)×(n/3).

2. Value Selection Pattern:
   - Each 3×3 block in the input matrix maps to a single value in the output matrix.
   - The selected value is the minimum value from each 3×3 block.
   - For any 3×3 block:
     ```
     [a b c]
     [d e f]  → min(a, b, c, d, e, f, g, h, i)
     [g h i]
     ```

3. Scanning Pattern:
   - The matrix is scanned from left to right, top to bottom.
   - Each 3×3 non-overlapping block is processed independently.
   - No blocks overlap or share elements.

4. General Rule:
   - For an input matrix of size n×n:
     - Output[i][j] = min(Input[3i][3j], Input[3i][3j+1], Input[3i][3j+2], Input[3i+1][3j], Input[3i+1][3j+1], Input[3i+1][3j+2], Input[3i+2][3j], Input[3i+2][3j+1], Input[3i+2][3j+2])
   - Where i and j are indices in the output matrix.

# Solution
def solution(matrix: list[list[int]]) -> list[list[int]]:
    """
    Refer to the example cases illustrated in the figure, identify and implement the pooling operation on the matrix.

    Input:
    - matrix: A 2d list representing the initial matrix. For example, [[1,3,4], [2,1,1], [1,2,2]]

    Output:
    - A 2d list representing the resulting matrix after the pooling operation.
    """
    rows = len(matrix)
    cols = len(matrix[0])
    pooled_matrix = []
    for i in range(0, rows, 3):
        pooled_row = []
        for j in range(0, cols, 3):
            block = [
                matrix[i][j], 
                matrix[i][j + 1] if j + 1 < cols else float('inf'), 
                matrix[i][j + 2] if j + 2 < cols else float('inf'), 
                matrix[i + 1][j] if i + 1 < rows else float('inf'), 
                matrix[i + 2][j] if i + 2 < rows else float('inf'), 
                matrix[i + 1][j + 1] if i + 1 < rows and j + 1 < cols else float('inf'),
                matrix[i + 2][j + 2] if i + 2 < rows and j + 2 < cols else float('inf'),
                matrix[i + 1][j + 2] if i + 1 < rows and j + 2 < cols else float('inf'),
                matrix[i + 2][j + 1] if i + 2 < rows and j + 1 < cols else float('inf'),
            ]
            min_value = min(block)
            pooled_row.append(min_value)
        pooled_matrix.append(pooled_row)
    return pooled_matrix


# Test Script
def test():
    test_cases = [
        {
            "input": [
                [1, 2, 6],
                [3, 4, 3],
                [8, 7, 9],
            ],
            "expected": [
                [1]
            ]
        },
        {
            "input": [
                [1, 3, 4, 2, 0, 3],
                [2, 1, 1, 3, 2, 6],
                [1, 2, 2, 4, 4, 7],
                [3, 2, 1, 0, 1, 0],
                [1, 7, 5, 2, 2, 0],
                [2, 9, 1, 2, 3, 1],
            ],
            "expected": [
                [1, 0], 
                [1, 0]
            ]
        }, {
            "input": [
                [1, 2, 3, 4, 3, 4],
                [5, 6, 7, 8, 1, 0],
                [9, 10, 11, 12, 2, 9],
                [13, 14, 15, 16, 0, 1],
                [1, 6, 3, 8, 9, 7],
                [0, 4, 3, 3, 0, 2],
            ],
            "expected": [
                [1, 0], 
                [0, 0]
            ]
        }, {
            "input": [
                [1, 1, 4],
                [1, 10, 3],
                [2, 1, 2],
            ],
            "expected": [
                [1]
            ]
        }, {
            "input": [
                [1, 2, 3, 4, 5, 6, 4, 5, 1],
                [12, 11, 10, 9, 8, 7, 0, 3, 4],
                [13, 14, 15, 16, 17, 18, 2, 9, 7],
                [19, 20, 21, 22, 23, 24, 4, 8, 6],
                [30, 29, 28, 27, 26, 25, 2, 2, 4],
                [31, 32, 33, 34, 35, 36, 7, 7, 8],
                [43, 44, 12, 22, 45, 46, 65, 23, 25],
                [56, 45, 23, 27, 32, 35, 36, 57, 64],
                [54, 34, 43, 34, 23, 33, 45, 43, 54],
            ],
            "expected": [
                [1, 4, 0], 
                [19, 22, 2], 
                [12, 22, 23]
            ]
        }
    ]
    for i, test_case in enumerate(test_cases):
        matrix = test_case["input"]
        expected_output = test_case["expected"]
        
        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()