# Problem Description
This is a cup-swapping puzzle where:
- There are 4 cups indexed from 1 to 4
- A red ball is placed under one of the cups initially
- A sequence of swap operations (A through F) is performed
- We need to track the final position of the ball after all swaps are completed
- Each swap operation has a predefined pattern of which cups exchange positions

# Visual Facts
1. There are 6 different swap operations labeled A through F
2. Each operation involves swapping exactly two cups
3. The cups are indexed 1, 2, 3, and 4 from left to right
4. A red ball appears in different positions to show movement
5. Black arrows indicate the swap directions
6. The specific swaps are:
   - A: swaps cups 1↔2
   - B: swaps cups 1↔3
   - C: swaps cups 2↔3
   - D: swaps cups 2↔4
   - E: swaps cups 3↔4
   - F: swaps cups 1↔4

# Visual Patterns
1. Position Pattern:
   - The ball moves with its cup during a swap
   - Each swap only affects two cups at a time
   - The other two cups remain in their positions during each swap

2. Operation Properties:
   - Each operation is reversible (doing the same swap twice returns to original state)
   - Multiple swaps can be chained together
   - The ball's final position depends on both:
     * Its initial position
     * The sequence of swaps performed


# Solution
from typing import List

def solution(swaps: List[str], initial_position: int) -> int:
    """
    Determine the final position of the ball after a series of swaps.

    Parameters:
        swaps (List[str]): A list of operation ids ('A', 'B', 'C', 'D', 'E', or 'F') representing the sequence of cup swap operations.
        initial_position (int): The index of the cup where the ball is initially placed. (1-indexed)

    Returns:
        int: The index of the cup where the ball is finally located. (1-indexed)
    """
    swap_operations = {
        'A': (1, 2),
        'B': (1, 3),
        'C': (2, 3),
        'D': (2, 4),
        'E': (3, 4),
        'F': (1, 4)
    }
    
    position = initial_position
    
    for swap in swaps:
        if swap in swap_operations:
            cup1, cup2 = swap_operations[swap]
            if position == cup1:
                position = cup2
            elif position == cup2:
                position = cup1
    
    return position


# Test Script
def test():
    test_cases = [
        (['A'], 1, 2),
        (['A'], 3, 3),
        (['B'], 3, 1),
        (['A', 'B', 'C'], 2, 2),
        (['D', 'E', 'A', 'C', 'B'], 4, 3),
        (['E', 'C', 'A', 'B', 'C', 'E', 'D'], 3, 3),
        (['A', 'C', 'E', 'D'], 1, 2),
        (['A', 'E', 'D', 'A', 'C', 'A', 'D', 'E', 'B'], 2, 4),
        (['B', 'E', 'A', 'C', 'D', 'A', 'C'], 4, 4),
        (['A', 'B', 'C', 'D', 'E', 'C', 'A'], 1, 4),
        (['B', 'E', 'A', 'C', 'D', 'A', 'C', 'A', 'C', 'E', 'B', 'A', 'D'], 4, 4),
    ]

    for i, (swaps, initial_position, expected_output) in enumerate(test_cases):
        original_swaps = swaps[:]
        try:
            output = solution(swaps, initial_position)
        except Exception as e:
            error_message = (
                f"An exception occurred while running the test case:\n"
                f"`swaps`: {original_swaps}\n"
                f"`initial_position`: {initial_position}\n\n"
                f"Exception: '{str(e)}'"
            )
            assert False, error_message

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

test()