# Problem Description
This is a matrix scoring problem where we need to calculate a score for any given position (x,y) in a matrix. The score is calculated by:
- Finding the sum of values in the same row as the target position (excluding the target position itself)
- Finding the sum of values in the same column as the target position (excluding the target position itself)
- Computing the difference between these sums (row sum - column sum)

# Visual Facts
1. Image shows two examples demonstrating the scoring calculation
2. Example 1:
   - 7x6 matrix with integers
   - Target position (4,3) marked in red with value -2
   - Blue cells are in row 4: [-6, 2, -5, 4, -8]
   - Green cells are in column 3: [3, 7, -4, -5, -4, 2]

3. Example 2:
   - 6x5 matrix with integers
   - Target position (6,1) marked in red with value -4
   - Blue cells in row 6: [6, -4, 3, -5]
   - Green cells in column 1: [3, 5, -2, -6, 9]

4. The equation at the top shows: Score(□) = ∑□ - ∑□ with red, blue, and green squares

# Visual Patterns
1. Scoring Pattern:
   - Score = Sum(row values excluding target) - Sum(column values excluding target)

2. Matrix Properties:
   - Coordinates are 1-indexed (not 0-indexed)
   - Matrix can be rectangular (not necessarily square)
   - Matrix contains both positive and negative integers


# Solution
def solution(matrix: list[list[int]], x: int, y: int) -> int:
    """
    Calculates a score based on coordinates (x,y) in a 2D matrix according to the specified equation.

    Parameters:
        matrix: list[list[int]]: A 2D matrix of integers
        x: int: The x-coordinate in the matrix (1-indexed)
        y: int: The y-coordinate in the matrix (1-indexed)

    Returns:
        int: The calculated score at position (x,y)
    """
    sum_row = sum(matrix[x-1]) 
    sum_col = sum(matrix[i][y-1] for i in range(len(matrix))) 
    return sum_row - sum_col


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

    for matrix, x, y, expected_output in test_cases:
        try:
            output = solution(matrix, x, y)
        except Exception as e:
            error_message = (
                f"An exception occurred while running the test case:\n"
                f"`matrix`: {matrix}\n"
                f"`x`: {x}\n"
                f"`y`: {y}\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"
                f"`x`: {x}\n"
                f"`y`: {y}\n\n"
                f"Actual Output: {output}\n"
                f"Expected Output: {expected_output}"
            )
            assert False, error_message

test()