# Problem Description
This problem involves determining whether two lines intersect. Each line is defined by two points on a circle. The circle has 8 evenly spaced points numbered 1 through 8. The lines can extend infinitely. The function needs to return True if the lines intersect and False if they don't.

# Visual Facts
1. The circle contains 8 points numbered 1 through 8.
2. Points are evenly spaced around the circumference.
3. Points are arranged clockwise in the order: 8, 1, 3, 2, 5, 4, 7, 6.
4. Two lines are shown:
   - A blue line connecting points 7 and 4.
   - An orange line connecting points 3 and 5.
5. The example shows these lines intersecting outside the circle.
6. Each point has a fixed position on the circle's circumference.

# Visual Patterns
1. **Line Intersection Pattern**:
   - Lines intersect when they cross each other's path either inside or outside the circle.
   
2. **Point Position Pattern**:
   - Each point's position can be thought of as having an angular value (45° apart).
   - 8 is at the top, 1 is at the top-right, 3 is at the right, and so on
   
3. ***Step to solve**:
   Suppose two lines are defined by points A, B and C, D.
   1. Define a mapping to reorder the line points:
       mapping = {
           1: 1,
           2: 3,
           3: 2,
           4: 5,
           5: 4,
           6: 7,
           7: 6,
           8: 8
       }
   2. Map each point in line1 and line2 using the mapping:
      line1 = map each point in line1 using the mapping
      line2 = map each point in line2 using the mapping
   3. Sort the points of line1 and line2:
      a, b = sort line1
      c, d = sort line2
   4. Check if the two lines intersect inside the circle:
      If (a < c < b < d) or (c < a < d < b):
      Return True (lines intersect inside the circle)
   5. Check if the two lines have a common endpoint:
      If a == c or a == d or b == c or b == d:
      Return True (lines intersect at a common endpoint)
   6. Calculate the length intervals to check if the lines intersect outside the circle:
      1. When a < b < c < d, len1 = a + (8 - d), len2 = c - b.
      2. When c < d < a < b, len1 = c + (8 - b), len2 = a - d.
      3. When a < c < d < b, len1 = c - a, len2 = b - d.
      4. When c < a < b < d, len1 = a - c, len2 = d - b.
   7. If len1 != len2, the lines will intersect. Otherwise, they are parallel.

# Solution
from typing import Tuple

def solution(line1: Tuple[int, int], line2: Tuple[int, int]) -> bool:
    """
    Determines if two line segments ultimately intersect.
    
    Parameters:
    line1: A tuple containing the numbers at both ends of the first line segment.
    line2: A tuple containing the numbers at both ends of the second line segment.
    
    Returns:
    bool: True if the line segments intersect, False otherwise.
    """
    mapping = {
        1: 1,
        2: 3,
        3: 2,
        4: 5,
        5: 4,
        6: 7,
        7: 6,
        8: 8
    }
    line1 = tuple(mapping[x] for x in line1)
    line2 = tuple(mapping[x] for x in line2)
    a, b = sorted(line1)
    c, d = sorted(line2)
    
    # Check if the points (a, b) and (c, d) intersect on the circle
    if (a < c < b < d) or (c < a < d < b):
        return True
    
    if (a < b < c < d):
        len1 = a + (8 - d)
        len2 = c - b
    
    if (c < d < a < b):
        len1 = c + (8 - b)
        len2 = a - d
    
    if (a < c < d < b):
        len1 = c - a
        len2 = b - d
    
    if (c < a < b < d):
        len1 = a - c
        len2 = d - b
    
    if a == c or a == d or b ==c or b == d:
        return True

    if len1 == len2:
        return False
    else:
        return True

# Test Script
def test():
    test_cases = [
        ((3, 7), (1, 5), True),
        ((2, 8), (1, 3), False),
        ((2, 8), (5, 8), True),
        ((3, 6), (1, 8), False),
        ((1, 4), (5, 8), True),
        ((1, 6), (2, 8), True),
        ((3, 7), (2, 4), False),
        ((1, 7), (3, 2), True),
        ((1, 8), (3, 2), True),
        ((1, 6), (3, 8), True),
        ((1, 7), (4, 3), False),
        ((4, 5), (1, 8), False),
        ((2, 7), (3, 4), True),
        ((1, 3), (6, 7), True),
        ((7, 8), (4, 1), False)
    ]

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

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

test()