Advertisement
Guest User

Untitled

a guest
May 21st, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. namespace _10._Crossroads
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. int greenLightSeconds = int.Parse(Console.ReadLine());
  11. int initialGreenLightSeconds = greenLightSeconds;
  12.  
  13. int freeWindow = int.Parse(Console.ReadLine());
  14.  
  15. Queue<string> queue = new Queue<string>();
  16.  
  17. int carsPassed = 0;
  18. while (true)
  19. {
  20. string command = Console.ReadLine();
  21.  
  22. if (command == "END")
  23. {
  24. Console.WriteLine("Everyone is safe.");
  25. Console.WriteLine($"{carsPassed} total cars passed the crossroads.");
  26. break;
  27. }
  28. while (command == "green"&&queue.Count>0)
  29. {
  30. string currentCar = queue.Peek();
  31.  
  32. if (greenLightSeconds > 0)
  33. {
  34. if (freeWindow + greenLightSeconds - currentCar.Length < 0)
  35. {
  36. Console.WriteLine("A crash happened!");
  37. Console.WriteLine($"{currentCar} was hit at {currentCar[freeWindow + greenLightSeconds]}.");
  38. return;
  39. }
  40. greenLightSeconds -= currentCar.Length;
  41. queue.Dequeue();
  42. carsPassed++;
  43. }
  44. else
  45. {
  46. greenLightSeconds = initialGreenLightSeconds;
  47. break;
  48. }
  49.  
  50. }
  51. if (command != "END" && command != "green")
  52. {
  53. greenLightSeconds = initialGreenLightSeconds;
  54. queue.Enqueue(command);
  55. }
  56. }
  57. }
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement