Advertisement
Fati0345

08. Crossroads

Jan 14th, 2025
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.38 KB | None | 0 0
  1.  
  2. from collections import deque
  3.  
  4. def adding_cars(command_input, cars_line):
  5.     while command_input != "END":
  6.         cars_line.append(command_input)
  7.         command_input = input()
  8.     return cars_line
  9.  
  10. def car_crashing(current_car, green_light_sec, free_window_sec):
  11.     green_light_sec += free_window_sec
  12.     character_hit = current_car[green_light_sec]
  13.     print(f"A crash happened!\n{current_car} was hit at {character_hit}.")
  14.  
  15. green_light = int(input())
  16. free_window = int(input())
  17. command = input()
  18.  
  19. cars_deque = deque()
  20. curr_green_light = green_light
  21. total_cars_passed = 0
  22.  
  23. adding_cars(command, cars_deque)
  24.  
  25. while cars_deque:
  26.     curr_el = cars_deque.popleft()
  27.     if curr_el != "green" and curr_green_light > 0:
  28.         if len(curr_el) == curr_green_light:
  29.             total_cars_passed += 1
  30.             curr_green_light = 0
  31.         elif len(curr_el) < curr_green_light:
  32.             total_cars_passed += 1
  33.             curr_green_light -= len(curr_el)
  34.         elif curr_green_light > 0 and len(curr_el) <= curr_green_light + free_window:
  35.             total_cars_passed += 1
  36.             curr_green_light = 0
  37.         else:
  38.             car_crashing(curr_el, curr_green_light, free_window)
  39.             break
  40.     elif curr_el == "green":
  41.         curr_green_light = green_light
  42. else:
  43.     print(f"Everyone is safe.\n{total_cars_passed} total cars passed the crossroads.")
  44.  
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement