Advertisement
Guest User

Untitled

a guest
May 19th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.51 KB | None | 0 0
  1. namespace _10Crossroads
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.  
  6.     class StartUp
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             Queue<string> queueOfCars = new Queue<string>();
  11.  
  12.             int greenLightDuration = int.Parse(Console.ReadLine());
  13.             int freeWindowDuration = int.Parse(Console.ReadLine());
  14.  
  15.             string input = string.Empty;
  16.  
  17.             bool isHitted = false;
  18.             string hittedCarName = string.Empty;
  19.             char hittedSymbol = '\0';
  20.             int totalCarsCounter = 0;
  21.  
  22.             while ((input = Console.ReadLine()) != "END")
  23.             {
  24.                 if ((input = Console.ReadLine()) != "green")
  25.                 {
  26.                     queueOfCars.Enqueue(input);
  27.                     continue;
  28.                 }
  29.  
  30.                 int currentGreenLightDuration = greenLightDuration;
  31.                 while (currentGreenLightDuration > 0 && queueOfCars.Count > 0)
  32.                 {
  33.                     string carName = queueOfCars.Dequeue();
  34.                     int carLenght = carName.Length;
  35.  
  36.                     if (currentGreenLightDuration - carLenght >= 0)
  37.                     {
  38.                         currentGreenLightDuration -= carLenght;
  39.                         totalCarsCounter++;
  40.                     }
  41.                     else
  42.                     {
  43.                         currentGreenLightDuration += freeWindowDuration;
  44.  
  45.                         if (currentGreenLightDuration - carLenght >= 0)
  46.                         {
  47.                             totalCarsCounter++;
  48.                             break;
  49.                         }
  50.                         else
  51.                         {
  52.                             isHitted = true;
  53.                             hittedCarName = carName;
  54.                             hittedSymbol = carName[currentGreenLightDuration];
  55.                             break;
  56.                         }
  57.                     }
  58.  
  59.                     if (isHitted)
  60.                     {
  61.                         break;
  62.                     }
  63.                 }
  64.             }
  65.  
  66.             if (isHitted)
  67.             {
  68.                 Console.WriteLine("A crash happened!");
  69.                 Console.WriteLine($"{hittedCarName} was hit at {hittedSymbol}.");
  70.             }
  71.             else
  72.             {
  73.                 Console.WriteLine("Everyone is safe.");
  74.                 Console.WriteLine($"{totalCarsCounter} total cars passed the crossroads.");
  75.             }
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement