# Problem Description
This is a problem about calculating a "U value" between adjacent rectangles in a circular arrangement of rectangles. The U value appears to be a measurement derived from the heights of two adjacent rectangles. The problem requires implementing a function that takes a list of rectangle heights and a target index, and returns the U value calculated at that position.

# Visual Facts
1. Each diagram shows a sequence of 5 rectangles (indexed 0-4)
2. The rectangles have different heights marked in orange numbers
3. The image shows 4 different examples of U value calculations
4. U values are highlighted in blue in each example
5. The highlighted U values are:
   - At index 0: U value = 12
   - At index 1: U value = 4
   - At index 3: U value = 6
   - At index 4: U value = 10

# Visual Patterns
1. Formula Pattern:
   - The U value appears to be calculated as: U = min(current_height, next_height) * 2
   - For example: At index 0 in first example:
     * Heights are 8 and 6
     * min(8,6) = 6
     * U value = 6 * 2 = 12

2. Adjacency Pattern:
   - When calculating U value at index i, we look at rectangle[i] and rectangle[i+1]
   - For the last index (4), it wraps around to index 0 (circular arrangement)

3. Calculation Rules:
   - U value is always positive
   - U value is always even (since it's multiplication by 2)
   - U value cannot be larger than the smallest height of the two adjacent rectangles multiplied by 2

4. Position Independence:
   - The U value calculation method remains consistent regardless of the position in the sequence
   - The calculation always considers exactly two adjacent rectangles

# Solution
from typing import List, Tuple

def solution(rectangles: List[int], target_idx: int) -> int:
    """
    Calculate the U value between two adjacent rectangles.

    Args:
        rectangles: A list of integers representing rectangle heights from left to right.
        target_idx: The 0-based index position at which to calculate the U value.  

    Returns:
        The U value between rectangles at target_idx.     
    """
    i = target_idx
    j = (target_idx + 1) % len(rectangles)
    width = 2
    height = min(rectangles[i], rectangles[j])
    return height * width


# Test Script

def test():
    test_cases = [
        {
            "rectangles": [8, 6, 2, 3, 5],
            "target_idx": 0,
            "expected": 12
        },

        {
            "rectangles": [8, 6, 2, 3, 5],
            "target_idx": 1,
            "expected": 4
        },
        {
            "rectangles": [8, 6, 2, 3, 5],
            "target_idx": 3,
            "expected": 6 
        },
        {
            "rectangles": [8, 6, 2, 3, 5],
            "target_idx": 3,
            "expected": 6
        },
        {
            "rectangles": [10, 5],
            "target_idx": 0,
            "expected": 10
        },
        {
            "rectangles": [1, 2, 3, 4, 5],
            "target_idx": 0,
            "expected": 2
        },
        {
            "rectangles": [1, 2, 3, 4, 5],
            "target_idx": 1,
            "expected": 4
        },
        {
            "rectangles": [5, 5, 5, 5, 5, 5],
            "target_idx": 2,
            "expected": 10
        },
        {
            "rectangles": [5, 5, 5, 5, 5, 5],
            "target_idx": 1,
            "expected": 10
        },
        {
            "rectangles": [2, 8, 5, 5, 5, 5],
            "target_idx": 5,
            "expected": 4
        },
    ]

    for i, tc in enumerate(test_cases, start=1):
        rectangles = tc["rectangles"]
        target_idx = tc["target_idx"] 
        expected_output = tc["expected"]

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

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

test()