CCCPaste

Man_O_War

def is_valid_idx(idx, seq):
    return 0 <= idx < len(seq)


pirate_ship = [int(x) for x in input().split(">")] # секциите на пиратския кораб като цели числа
warship = [int(x) for x in input().split(">")] # секциите на бойния кораб като цели числа
maximum_section_health = int(input()) # максималкия здравина/здраве на секция

flag = True
while flag:
    line = input()
    if line == "Retire":
        break

    command_split = line.split()
    command = command_split[0] # извеждаме само командите, които са на индекс 0

    # дали индекс е валиден го има и за трите команди, което може да се изведе като обща функция
    if command == "Fire":
        idx = int(command_split[1]) # извеждаме текущия индекс като цяло число
        damage = int(command_split[2]) # извеждаме текущия damage като цяло число
        if not is_valid_idx(idx, warship): # валидирам индекс, т.е проверяваме ако индекса НЕ Е валиден
            continue
        warship[idx] -= damage # от секцията под този индекс на бпойния кораб изважадаме damage
        if warship[idx] <= 0: # по условия, ако секцията стане <= 0, т.е. health <= 0 се принтира съобщения
            flag = False
            print(f"You won! The enemy ship has sunken.")

    elif command == "Defend": # разликата е, че се атакуват всички клетки. Трябва да се валидират и двата индекса, начален и краен
        start_idx = int(command_split[1])
        end_inx = int(command_split[2])
        damage = int(command_split[3])
        if not is_valid_idx(start_idx, pirate_ship) or not is_valid_idx(end_inx, pirate_ship):
            continue
        for idx in range(start_idx, end_inx + 1):
            pirate_ship[idx] -= damage
            if pirate_ship[idx] <= 0:
                print(f"You lost! The pirate ship has sunken.")
                flag = False
                break
    elif command == "Repair":
        idx = int(command_split[1])
        health = int(command_split[2])
        if not is_valid_idx(idx, pirate_ship):
            continue
        pirate_ship[idx] = min(maximum_section_health, pirate_ship[idx] + health)

    elif command == "Status":
        threshold = maximum_section_health * 0.2
        counter = 0
        for section in pirate_ship:
            if section < threshold:
                counter += 1
        print(f"{counter} sections need repair.")

if flag:
    print(f"Pirate ship status: {sum(pirate_ship)}")
    print(f"Warship status: {sum(warship)}")