# Problem Description
This is a geometric scoring problem where points are placed on a coordinate grid with associated numerical values. The goal is to calculate a total score based on valid points within and on a given rectangular boundary. Points are considered valid if they lie within or on the boundary edges, except for corner points which are always invalid. The score is calculated by summing the absolute values of valid point values.

# Visual Facts
1. Each example shows a coordinate grid system (0-8 on both axes)
2. Green lines indicate the bounding rectangle
3. Points are marked with their numerical values
4. Points are marked as either valid (green) or invalid (red)
5. Example 1:
   - Points: (-2, 4, 5, 5) with coordinates visible on grid
   - Score = |4| + |-2| = 6
6. Example 2:
   - Points: (-2, 4) with coordinates visible
   - Score = |-2| = 2
   - Corner points are explicitly marked as "(corners)"
7. Example 3:
   - Multiple points with values: 8, 3, -2, 4, 1, 2, -3
   - Score = |-2| + ... + |1| = 12

# Visual Patterns
1. Point Validity Rules:
   - Points inside the rectangle are always valid
   - Points on the edges (not corners) of the rectangle are valid
   - Points at the corners of the rectangle are always invalid
   - Points outside the rectangle are invalid ("out of bounds")

2. Scoring Rules:
   - Only valid points contribute to the final score
   - The score is calculated by summing the absolute values of valid point values
   - Corner points are excluded from scoring even if they lie exactly on corners

3. Rectangle Properties:
   - Rectangles are always aligned with the coordinate axes
   - Rectangle boundaries can intersect with points
   - Rectangle dimensions can vary but maintain rectangular shape

4. Coordinate System:
   - Uses standard cartesian coordinate system
   - Grid points are integer values
   - Both x and y coordinates are positive integers in the shown examples
   - Grid appears to use 1-unit spacing


# Solution
from typing import List, Tuple

def solution(points: List[List], bounding_rect: List[Tuple[int, int]]) -> int:
    """
    Calculate the matrix score based on points within specified boundaries.

    Args:
        points: List of lists, where each inner list contains:
                - A point value (integer)
                - Point coordinates as (x, y) tuple
        bounding_rect: List of two tuples defining the matrix boundaries:
                - First tuple (x1, y1): Top-left corner coordinates 
                - Second tuple (x2, y2): Bottom-right corner coordinates, where x2 > x1 and y2 > y1

    Returns:
        Total score of all points within the matrix boundaries
    """
    # Extract bounds
    (x1, y1), (x2, y2) = bounding_rect

    # Initialize total score
    total_score = 0

    # Iterate over all points
    for value, (x, y) in points:
        # Check if the point is within the bounds
        if x1 <= x <= x2 and y1 <= y <= y2:
            # Check if the point is on one of the four corners
            if (x, y) == (x1, y1) or (x, y) == (x1, y2) or (x, y) == (x2, y1) or (x, y) == (x2, y2):
                # Skip corner points
                continue
            # Otherwise, add the value of the point to the score
            total_score += abs(value)

    return total_score

# Test Script

def test():
    test_cases = [
        {
            "input": {
                "points": [[-2, (3, 3)], [4, (5, 5)], [5, (1, 1)], [5, (5, 1)]],
                "bounds": [(1, 2), (5, 6)]
            },
            "expected": 6
        },
        {
            "input": {
                "points": [[4, (3, 3)], [-2, (4, 4)]],
                "bounds": [(3, 3), (5, 5)]
            },
            "expected": 2
        },
        {
            "input": {
                "points": [[4, (3, 3)], [5, (3, 5)], [3, (5, 3)], [-2, (5, 5)]],
                "bounds": [(3, 3), (5, 5)]
            },
            "expected": 0
        },
        {
            "input": {
                "points": [[8, (2, 5)], [3, (2, 3)], [-2, (3, 3)], [4, (4, 1)], [1, (5, 4)], [2, (6, 3)], [-3, (6, 1)]],
                "bounds": [(2, 1), (6, 5)]
            },
            "expected":12
        },
        {
            "input": {
                "points": [[4, (1, 1)], [3, (3, 3)], [-2, (4, 4)], [5, (2, 2)]],
                "bounds": [(2, 2), (4, 4)]
            },
            "expected": 3 
        },
        {
            "input": {
                "points": [[4, (1, 1)], [3, (6, 6)]],
                "bounds": [(2, 2), (4, 4)]
            },
            "expected": 0
        },
        {
            "input": {
                "points": [[5, (3, 1)], [4, (6, 5)]],
                "bounds": [(3, 1), (6, 5)]
            },
            "expected": 0
        },
        {
            "input": {
                "points": [[4, (1, 1)], [3, (3, 3)], [-2, (4, 4)], [5, (2, 2)]],
                "bounds": [(1, 1), (4, 4)]
            },
            "expected": 8
        }
    ]

    for i, test_case in enumerate(test_cases, start=1):
        points = test_case["input"]["points"]
        bounding_rect = test_case["input"]["bounds"]
        expected_output = test_case["expected"]

        try:
            output = solution(points, bounding_rect)
        except Exception as e:
            error_message = (
                f"An exception occurred while running test case {i}:\n"
                f"`points`: {points}\n"
                f"`bounding_rect`: {bounding_rect}\n\n"
                f"Exception: '{str(e)}'"
            )
            assert False, error_message

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

test()