# 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 each squared values in the same row as the target position (excluding the target position itself)
- Finding the sum of each squared 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(□) = ∑sqrt(□) - ∑sqrt(□) with red, blue, and green squares

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


# 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][i]**2 for i in range(len(matrix[0]))) 
    sum_col = sum(matrix[i][y-1]**2 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],
            ], 4, 3, 26
        ),
        ([
            [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, -109
        ),
        ([
            [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, -113
        ),
        ([
            [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, 9
        ),
        ([
            [1,  -2,  3,  4],
            [5,   4,  6, -8],
            [4,   9, -4,  3],
            [7,   2, -8, -5]
        ], 3, 3, -3
        ),
        ([
            [1,  -2,  3,  4],
            [5,   4,  6, -8],
            [4,   9, -4,  3],
            [7,   2, -8, -5]
        ], 1, 4, -84
        ),
        ([
            [1,  -2,  3,  4],
            [5,   4,  6, -8],
            [4,   9, -4,  3],
            [7,   2, -8, -5]
        ], 4, 1, 51
        ),
        ([
            [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, -69
        ),
        ([
            [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, -34
        ),
        ([
            [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, 2
        ),
        
    ]

    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`: \n"
                f"{'\n'.join(str(row) for row in 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`: \n"
                f"{'\n'.join(str(row) for row in 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()