Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace ExamRetake_24April2018
- {
- class Crossroads
- {
- static void Main()
- {
- int greenLight = int.Parse(Console.ReadLine());
- int freeWindow = int.Parse(Console.ReadLine());
- Queue<string> cars = new Queue<string>();
- bool carCrashed = false;
- string theCarThatCrashed = "";
- int crashStart = 0;
- int carsCounter = 0;
- string input = "";
- while(input != "END")
- {
- input = Console.ReadLine();
- if(input == "END")
- {
- break;
- }
- if(input == "green")
- {
- int passingTime = greenLight;
- int bufferTime = freeWindow;
- string currentCar = "";
- while(passingTime > 0 && cars.Count > 0)
- {
- if(passingTime <= 0 || cars.Count <= 0)
- {
- break;
- }
- currentCar = cars.Dequeue();
- if (currentCar.Length <= passingTime)
- {
- passingTime -= currentCar.Length;
- carsCounter++;
- }
- else
- {
- if(currentCar.Length <= passingTime + bufferTime)
- {
- passingTime -= currentCar.Length;
- carsCounter++;
- }
- else
- {
- carCrashed = true;
- if(passingTime < 0)
- {
- passingTime = 0;
- }
- crashStart = passingTime + bufferTime;
- theCarThatCrashed = currentCar;
- break;
- }
- }
- }
- if(carCrashed)
- {
- break;
- }
- /*if(currentCar.Length > greenLight + freeWindow)
- {
- for (int i = 0; i < passingTime; i++)
- {
- currentCar = cars.Dequeue();
- }
- }*/
- }
- else
- {
- string car = input;
- cars.Enqueue(car);
- }
- }
- if(carCrashed)
- {
- Console.Write("A crash happened!\n{0} was hit at {1}.", theCarThatCrashed, theCarThatCrashed[crashStart]);
- }
- else
- {
- Console.Write("Everyone is safe.\n{0} total cars passed the crossroads.", carsCounter);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement