Guest User

08. Crossroads

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