Advertisement
gabi11

Stacks and Queues - 7. Traffic Jam

May 10th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.04 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5.  
  6. namespace Advanced
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             int number = int.Parse(Console.ReadLine());
  13.             string input = Console.ReadLine();
  14.             var cars = new Queue<string>();
  15.  
  16.             int totalPassed = 0;
  17.  
  18.             while (input != "end")
  19.             {
  20.                 if (input != "green")
  21.                 {
  22.                     cars.Enqueue(input);
  23.                 }
  24.  
  25.                 else
  26.                 {
  27.                     var carToPass = Math.Min(number, cars.Count);
  28.  
  29.                     for (int i = 0; i < carToPass; i++)
  30.                     {
  31.                         Console.WriteLine($"{cars.Dequeue()} passed!");
  32.                         totalPassed++;
  33.                     }
  34.                 }
  35.                 input = Console.ReadLine();
  36.             }
  37.             Console.WriteLine($"{totalPassed} cars passed the crossroads.");
  38.         }
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement