# Problem Description
This is a matrix pooling operation problem where a larger input matrix needs to be transformed into a smaller output matrix using specific rules. The pooling operation appears to reduce the size of the input matrix by processing 2×2 regions into single values in the output matrix. The goal is to implement this transformation according to the pattern shown in the examples.

# Visual Facts
1. Example 1:
   - Input: 2×2 matrix
   - Output: 1×1 matrix
   - Value 1 is selected from upper-left position

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

3. Example 3:
   - Input: 6×6 matrix
   - Output: 3×3 matrix
   - Each 2×2 section maps to one output value
   - Output matrix contains [1,2,0, 2,3,0, 2,4,2]

# Visual Patterns
1. Size Reduction Pattern:
   - Output matrix size is always half of input matrix in each dimension
   - Input dimensions must be even numbers
   - Input n×n matrix → Output (n/2)×(n/2) matrix

2. Value Selection Pattern:
   - Each 2×2 block in input maps to single value in output
   - The selected value appears to be the minimum value from each 2×2 block
   - For any 2×2 block:
     ```
     [a b]  → min(a,b,c,d)
     [c d]
     ```

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

4. General Rule:
   - For input matrix of size n×n
   - Output[i][j] = min(Input[2i][2j], Input[2i][2j+1], Input[2i+1][2j], Input[2i+1][2j+1])
   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], [2,1,1,3], [1,2,2,4], [3,2,1,0]]

    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, 2):
        pooled_row = []
        for j in range(0, cols, 2):
            block = [
                matrix[i][j], 
                matrix[i][j + 1] if j + 1 < cols else float('inf'), 
                matrix[i + 1][j] if i + 1 < rows else float('inf'), 
                matrix[i + 1][j + 1] if i + 1 < 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],
                [3,4]
            ],
            "expected":[
                [1]
            ]
        },
        {
            "input": [
                [1, 3, 4, 2],
                [2, 1, 1, 3],
                [1, 2, 2, 4],
                [3, 2, 1, 0]
            ],
            "expected": [
                [1, 1],
                [1, 0]
            ]
        }, {
            "input": [
                [1, 2, 3, 4],
                [5, 6, 7, 8],
                [9, 10, 11, 12],
                [13, 14, 15, 16]
            ],
            "expected": [
                [1, 3],
                [9, 11]
            ]
        }, {
            "input": [
                [1, 1],
                [1, 10]
            ],
            "expected": [
                [1]
            ]
        }, {
            "input": [
                [1, 2, 3, 4, 5, 6],
                [12, 11, 10, 9, 8, 7],
                [13, 14, 15, 16, 17, 18],
                [19, 20, 21, 22, 23, 24],
                [30, 29, 28, 27, 26, 25],
                [31, 32, 33, 34, 35, 36]
            ],
            "expected": [
                [1, 3, 5],
                [13, 15, 17],
                [29, 27, 25]
            ]
        }
    ]
    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()