Advertisement
Onesible

Crossroads

Jan 13th, 2024
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. from collections import deque
  2.  
  3. green_window = int(input())
  4. free_window = int(input())
  5.  
  6. car_passed = 0
  7. cars = deque()
  8. crash = False
  9.  
  10. while True:
  11.     data = input()
  12.  
  13.     if data == "END":
  14.         break
  15.  
  16.     if data != "green":
  17.         cars.append(data)
  18.     else:
  19.         current_green_window = green_window
  20.  
  21.         while current_green_window > 0 and cars:
  22.             car = cars.popleft()
  23.             time_to_pass = len(car)
  24.  
  25.             if time_to_pass <= current_green_window:
  26.                 current_green_window -= time_to_pass
  27.                 car_passed += 1
  28.             elif time_to_pass <= current_green_window + free_window:
  29.                 car_passed += 1
  30.                 break
  31.             else:
  32.                 print(f"A crash happened!\n"
  33.                       f"{car} was hit at {car[current_green_window + free_window]}.")
  34.                 crash = True
  35.                 break
  36.  
  37.     if crash:
  38.         break
  39.  
  40. if not crash:
  41.     print("Everyone is safe.")
  42.     print(f"{car_passed} total cars passed the crossroads.")
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement