CCCPaste

Man_O_War_without_func

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 0 <= idx < len(warship): # атакуваме част от warship под някакъв индек, който го проверяваме дали не е в ranga на warship, който е 0 до len(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 0 <= start_idx < len(pirate_ship) or not 0 <= end_inx < len(pirate_ship):
            continue
        for idx in range(start_idx, end_inx + 1): # ако индекса е валиден циклим за всеки един сектор damage-a
            pirate_ship[idx] -= damage
            if pirate_ship[idx] <= 0: # ако в даден момент сектора стане <= 0 прекъсваме програмата и for цикъла
                print(f"You lost! The pirate ship has sunken.")
                flag = False
                break
    elif command == "Repair": # поправя се определена секция, която е под индекс pirate_ship[idx]
        idx = int(command_split[1])
        health = int(command_split[2]) # добавя се health към съответната секция 
        if not 0 <= idx < len(pirate_ship):
            continue
        pirate_ship[idx] = min(maximum_section_health, pirate_ship[idx] + health) # взима се по-малкия 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)}")