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