Advertisement
NelIfandieva

ExamRetake_24April2018_Pr01_Crossroads

Sep 20th, 2018
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.23 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace ExamRetake_24April2018
  5. {
  6.     class Crossroads
  7.     {
  8.         static void Main()
  9.         {
  10.             int greenLight = int.Parse(Console.ReadLine());
  11.             int freeWindow = int.Parse(Console.ReadLine());
  12.             Queue<string> cars = new Queue<string>();
  13.             bool carCrashed = false;
  14.             string theCarThatCrashed = "";
  15.             int crashStart = 0;
  16.             int carsCounter = 0;
  17.             string input = "";
  18.             while(input != "END")
  19.             {
  20.                 input = Console.ReadLine();
  21.  
  22.                 if(input == "END")
  23.                 {
  24.                     break;
  25.                 }
  26.                 if(input == "green")
  27.                 {
  28.                     int passingTime = greenLight;
  29.                     int bufferTime = freeWindow;
  30.                     string currentCar = "";
  31.                    
  32.                     while(passingTime > 0 && cars.Count > 0)
  33.                     {
  34.                         if(passingTime <= 0 || cars.Count <= 0)
  35.                         {
  36.                             break;
  37.                         }
  38.                         currentCar = cars.Dequeue();
  39.                        
  40.                         if (currentCar.Length <= passingTime)
  41.                         {
  42.                             passingTime -= currentCar.Length;
  43.                             carsCounter++;
  44.                         }
  45.                         else
  46.                         {
  47.                             if(currentCar.Length <= passingTime + bufferTime)
  48.                             {
  49.                                 passingTime -= currentCar.Length;
  50.                                 carsCounter++;
  51.                             }
  52.                             else
  53.                             {
  54.                                 carCrashed = true;
  55.                                 if(passingTime < 0)
  56.                                 {
  57.                                     passingTime = 0;
  58.                                 }
  59.                                 crashStart = passingTime + bufferTime;
  60.                                 theCarThatCrashed = currentCar;
  61.                                 break;
  62.                             }
  63.                         }
  64.                     }
  65.                     if(carCrashed)
  66.                     {
  67.                         break;
  68.                     }
  69.                     /*if(currentCar.Length > greenLight + freeWindow)
  70.                     {
  71.                         for (int i = 0; i < passingTime; i++)
  72.                         {
  73.                             currentCar = cars.Dequeue();
  74.                         }
  75.                     }*/
  76.                 }
  77.                 else
  78.                 {
  79.                     string car = input;
  80.                     cars.Enqueue(car);
  81.                 }
  82.             }
  83.             if(carCrashed)
  84.             {
  85.                 Console.Write("A crash happened!\n{0} was hit at {1}.", theCarThatCrashed, theCarThatCrashed[crashStart]);
  86.             }
  87.             else
  88.             {
  89.                 Console.Write("Everyone is safe.\n{0} total cars passed the crossroads.", carsCounter);
  90.             }
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement