SHARE
TWEET

08. Crossroad

milengerov Jan 21st, 2020 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from collections import deque
  2.  
  3.  
  4. def crossroad():
  5.     global queue, green_time, safe_time, passed_cars, crash
  6.     green_time_left = green_time
  7.     safe_time_left = safe_time
  8.     queue = deque(queue)
  9.  
  10.     while green_time_left > 0 and queue:
  11.         car_in_crosssection = queue.popleft()
  12.         car_queue = deque(car_in_crosssection)
  13.         while car_queue:
  14.             char = car_queue.popleft()
  15.             green_time_left -= 1
  16.             if green_time_left < 0:
  17.                 safe_time_left -= 1
  18.                 if safe_time_left < 0:
  19.                     crash = True
  20.                     print("A crash happened!")
  21.                     print(f"{car_in_crosssection} was hit at {char}.")
  22.                     exit()
  23.         passed_cars.append(car_in_crosssection)
  24.  
  25.  
  26. green_time = int(input())
  27. safe_time = int(input())
  28. queue = []
  29. passed_cars = []
  30. crash = False
  31.  
  32. while True:
  33.     car = input()
  34.  
  35.     if car == "END":
  36.         break
  37.     elif car == "green":
  38.         crossroad()
  39.     else:
  40.         queue.append(car)
  41.  
  42. if not crash:
  43.     print("Everyone is safe.")
  44.     print(f"{len(passed_cars)} total cars passed the crossroads.")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top