Advertisement
Nenogzar

08. Crossroads

May 14th, 2024
700
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.06 KB | None | 0 0
  1. from _collections import deque
  2.  
  3. green_light = int(input())
  4. free_window = int(input())
  5.  
  6. cars = deque()
  7. cars_counter = 0
  8. crashed = False
  9.  
  10. command = input()
  11. while command != "END":
  12.     if command == "green":
  13.         current_car = cars.popleft() if cars else None
  14.         left_seconds = green_light - len(current_car) if current_car else 0
  15.  
  16.         while cars and left_seconds > 0:
  17.             current_car = cars.popleft()
  18.             cars_counter += 1
  19.             left_seconds -= len(current_car)
  20.  
  21.         if left_seconds == 0 and current_car:
  22.             cars_counter += 1
  23.         if free_window >= abs(left_seconds) and current_car:
  24.             if left_seconds < 0:
  25.                 cars_counter += 1
  26.         else:
  27.             index = free_window + left_seconds
  28.             print(f"A crash happened!\n{current_car} was hit at {current_car[index]}.")
  29.             crashed = True
  30.             break
  31.     else:
  32.         cars.append(command)
  33.     command = input()
  34.  
  35. if not crashed:
  36.     print(f"Everyone is safe.\n{cars_counter} total cars passed the crossroads.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement