# Problem Description
This is a watchtower connectivity problem where we need to identify isolated watchtowers in a 2D grid system. The main goal is to determine which watchtowers are not connected to any network of other watchtowers, based on specific connection rules. The function should return the coordinates of these isolated watchtowers.

# Visual Facts
1. Grid System:
   - Coordinate system ranges from 0-8 on both x and y axes
   - Towers are placed at integer coordinates
   - Multiple towers can exist at different heights on the same vertical or horizontal line

2. Tower Representations:
   - Connected towers are shown in green
   - Disconnected towers are shown in yellow
   - Green lines indicate valid connections between towers

3. Example 1 Configuration:
   - Has 5 total towers
   - 4 connected towers form a network
   - 1 isolated tower at (2,2)

4. Example 2 Configuration:
   - Has 6 total towers
   - 5 connected towers form a network
   - 1 isolated tower at (4,2)

# Visual Patterns
1. Connection Rules:
   - Towers seem to connect when they share either:
     a) The same y-coordinate (horizontal connection)
     b) The same x-coordinate (vertical connection)
   - Connections are transitive: if A connects to B and B connects to C, then A is considered connected to C

2. Isolation Criteria:
   - A tower is considered isolated if it has no direct horizontal or vertical connections to any other tower
   - All isolated towers in the examples are positioned lower on the grid (y=2) compared to the connected networks
   - The height difference alone doesn't determine isolation; it's the ability to form valid connections

3. Network Formation:
   - A single valid connection to any part of a connected network makes a tower part of that network
   - Connected towers form a single cohesive network where each tower can "reach" every other connected tower through some path

4. Key Mathematical Properties:
   - Two towers are directly connected if they share exactly one coordinate (x or y)
   - Distance between towers doesn't matter as long as they share a coordinate
   - A tower must share at least one coordinate with another tower to be considered connected


# Solution
from typing import Tuple, Set

def solution(towers: Set[Tuple[int, int]]) -> Set[Tuple[int, int]]:
    """
    Identifies watchtowers that are not connected to any others based on given connection rules.

    Args:
        towers: A set of tuples, each containing (x, y) coordinates of a watchtower.

    Returns:
        A set of tuples containing (x, y) coordinates of unconnected watchtowers.
    """
    n = len(towers)
    unconnected = []
    towers = list(towers)

    for i in range(n):
        x_i, y_i = towers[i]

        has_same_x = False
        has_same_y = False

        for j in range(n):
            if i == j:
                continue
            x_j, y_j = towers[j]

            if x_j == x_i and y_j != y_i:
                has_same_x = True

            if y_j == y_i and x_j != x_i:
                has_same_y = True

            if has_same_x and has_same_y:
                break
        if not (has_same_x or has_same_y):
            unconnected.append((x_i, y_i))

    return set(unconnected)


# Test Script
def test():
    test_cases = [
        {
            "input": set(()),
            "expected": set()
        },
        {
            "input": set([(1,1), (3,4)]),
            "expected": set([(1,1), (3,4)])
        },
        {
            "input": set([(1,1), (1,2)]),
            "expected": set()
        },
        {
            "input": set([(1,1), (2,2)]),
            "expected": set([(1,1), (2,2)])
        },
        {
            "input": set([(0,0), (0,1), (1,0), (1,1)]),
            "expected": set()
        },
        {
            "input": set([(2,3), (2,5), (3,5), (3,4)]),
            "expected": set()
        },
        {
            "input": set([(0,0), (1,1), (2,2), (2,3)]),
            "expected": set([(0,0), (1,1)])
        },
        {
            "input": set([(1,1), (1,2), (2,2), (5,5), (5,6), (6,5), (7,7)]),
            "expected": set([(7,7)])
        },
    ]

    for i, tc in enumerate(test_cases, start=1):
        towers_input = tc["input"]
        expected_output = tc["expected"]
        
        try:
            output = solution(towers_input)
        except Exception as e:
            error_message = (
                f"An exception occurred while running the test case:\n"
                f"`towers`: {towers_input}\n\n"
                f"Exception: '{str(e)}'"
            )
            assert False, error_message
            
        if output != expected_output:
            error_message = (
                f"The following Test Case failed:\n"
                f"`towers`: {towers_input}\n\n"
                f"Actual Output: {output}\n"
                f"Expected Output: {expected_output}"
            )
            assert False, error_message

test()