#John Kesler
#Cary Academy ACSL 4
#INT-3 

input_data = [i.strip() for i in open('test-cases.txt', 'r').readlines()]

def check_r9(initial_pos, new_pos):
    # simple checking to see if a move qualifies for rule 9
    # if the marker moves through certain boundaries, we know if qualifies
    boundaries = {(6, 8), (11, 13), (16, 18), (21, 23), (26, 28), (34, 36), (39, 41), (44, 46), (49, 51)}
    # ew
    for start, end in boundaries:
        if initial_pos <= start and new_pos >= end:
            return True
    return False

def run_case(i):
    # first break apart the input
    i = [int(j) for j in i.split(' ')]
    opponent_markers = i[:3]
    player_markers = i[3:6]
    roll_count = i[6]
    rolls = i[7:]

    # precalculated stuff heh
    primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47}
    squares = {3**2, 4**2, 5**2, 6**2, 7**2}

    for roll in rolls:
        # case where no more markers
        if player_markers == []:
            break
        # get marker to move/where to move to
        lowest_marker = min(player_markers)
        next_spot = lowest_marker + roll
        # r4
        if next_spot in opponent_markers + player_markers:
            continue
        # r6
        if next_spot > 52:
            continue
        if next_spot == 52:
            player_markers.remove(lowest_marker)
            continue
        # r7/r8/r9
        if next_spot in primes:
            obstructions = [j for j in opponent_markers + player_markers if next_spot < j <= next_spot+6]
            next_spot = min(obstructions) - 1 if obstructions != [] else next_spot + 6
        elif next_spot in squares:
            obstructions = [j for j in opponent_markers + player_markers if next_spot-6 <= j < next_spot]
            next_spot = max(obstructions) + 1 if obstructions != [] else next_spot - 6
        elif check_r9(lowest_marker, next_spot):
            updated = False
            for spot in range(lowest_marker+1, next_spot+1)[::-1]:
                if spot % roll == 0 and spot not in player_markers + opponent_markers:
                    next_spot = spot
                    updated = True
                    break
            if not updated:
                next_spot = lowest_marker
        # update markers
        player_markers.remove(lowest_marker)
        player_markers.append(next_spot)

    return ' '.join([str(j) for j in sorted(player_markers)]) if player_markers != [] else 'GAME OVER'

for case, i in enumerate(input_data):
    print(f'{case+1}: {run_case(i)}')

________________________________________