Guest User

Untitled

a guest
Sep 11th, 2021
31
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Crossroads
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. int greenLightDuration = int.Parse(Console.ReadLine());
  11. int freeWindowDuration = int.Parse(Console.ReadLine());
  12. int currentTime = greenLightDuration;
  13.  
  14. Queue<string> cars = new Queue<string>();
  15.  
  16. bool isCrashed = false;
  17.  
  18. int carPass = 0;
  19.  
  20. string command = Console.ReadLine();
  21.  
  22. while (command != "END")
  23. {
  24. if (command == "green")
  25. {
  26. if (greenLightDuration == 0)
  27. {
  28. command = Console.ReadLine();
  29. continue;
  30. }
  31.  
  32. while (cars.Count > 0)
  33. {
  34.  
  35. if (cars.Peek().Length <= currentTime)
  36. {
  37. currentTime -= cars.Peek().Length;
  38. cars.Dequeue();
  39. carPass++;
  40. }
  41. else
  42. {
  43. if (cars.Peek().Length <= currentTime + freeWindowDuration)
  44. {
  45. carPass++;
  46. cars.Dequeue();
  47. break;
  48. }
  49. else
  50. {
  51. isCrashed = true;
  52. Console.WriteLine("A crash happened!");
  53. int indexCrashed = currentTime + freeWindowDuration;
  54. string carText = cars.Peek();
  55. string subs = carText.Substring(indexCrashed, 1);
  56. Console.WriteLine($"{carText} was hit at {subs}.");
  57. return;
  58. }
  59. }
  60.  
  61. }
  62. currentTime = greenLightDuration;
  63.  
  64. }
  65. else
  66. {
  67. cars.Enqueue(command);
  68. }
  69.  
  70. command = Console.ReadLine();
  71. }
  72.  
  73. if (!isCrashed)
  74. {
  75. Console.WriteLine("Everyone is safe.");
  76. Console.WriteLine($"{carPass} total cars passed the crossroads.");
  77. }
  78.  
  79. }
  80.  
  81. }
  82. }
RAW Paste Data