Advertisement
yanass

Crossroads - Stacks & Queues

Sep 26th, 2019
1,261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.04 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Crossroads
  6. {
  7.     class Program
  8.     {
  9.         static void Main()
  10.         {
  11.             int greenLightSeconds = int.Parse(Console.ReadLine());
  12.             int secondsPassCrossroad = int.Parse(Console.ReadLine());
  13.  
  14.             Queue<string> carsQueue = new Queue<string>();
  15.  
  16.             int counter = 0;
  17.  
  18.             while (true)
  19.             {
  20.                 string car = Console.ReadLine();
  21.  
  22.                 int greenLights = greenLightSeconds;
  23.                 int passSeconds = secondsPassCrossroad;
  24.  
  25.                 if (car == "END")
  26.                 {
  27.                     Console.WriteLine($"Everyone is safe.{Environment.NewLine}" +
  28.                         $"{counter} total cars passed the crossroads.");
  29.                     return;
  30.                 }
  31.  
  32.                 if (car == "green")
  33.                 {
  34.                     while (greenLights > 0 && carsQueue.Count != 0)
  35.                     {
  36.  
  37.                         string firstInQueue = carsQueue.Dequeue();
  38.                         greenLights -= firstInQueue.Count();
  39.                         if (greenLights >= 0)
  40.                         {
  41.                             counter++;
  42.                         }
  43.                        
  44.                         else
  45.                         {
  46.                             passSeconds += greenLights;
  47.                             if (passSeconds < 0)
  48.                             {
  49.                                 Console.WriteLine($"A crash happened!{Environment.NewLine}" +
  50.                                     $"{firstInQueue} was hit at" +
  51.                                     $" {firstInQueue[firstInQueue.Length + passSeconds]}.");
  52.                                 return;
  53.                             }
  54.                             counter++;
  55.                         }
  56.                     }
  57.                 }
  58.  
  59.                 else
  60.                 {
  61.                     carsQueue.Enqueue(car);
  62.                 }
  63.             }
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement