Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Collections.Generic;
- namespace _10._Crossroads
- {
- class Program
- {
- static void Main(string[] args)
- {
- int greenLightSeconds = int.Parse(Console.ReadLine());
- int initialGreenLightSeconds = greenLightSeconds;
- int freeWindow = int.Parse(Console.ReadLine());
- Queue<string> queue = new Queue<string>();
- int carsPassed = 0;
- while (true)
- {
- string command = Console.ReadLine();
- if (command == "END")
- {
- Console.WriteLine("Everyone is safe.");
- Console.WriteLine($"{carsPassed} total cars passed the crossroads.");
- break;
- }
- while (command == "green"&&queue.Count>0)
- {
- string currentCar = queue.Peek();
- if (greenLightSeconds > 0)
- {
- if (freeWindow + greenLightSeconds - currentCar.Length < 0)
- {
- Console.WriteLine("A crash happened!");
- Console.WriteLine($"{currentCar} was hit at {currentCar[freeWindow + greenLightSeconds]}.");
- return;
- }
- greenLightSeconds -= currentCar.Length;
- queue.Dequeue();
- carsPassed++;
- }
- else
- {
- greenLightSeconds = initialGreenLightSeconds;
- break;
- }
- }
- if (command != "END" && command != "green")
- {
- greenLightSeconds = initialGreenLightSeconds;
- queue.Enqueue(command);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement