Advertisement
viligen

crossroads

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