Advertisement
kalitarix

Iron girder

Dec 30th, 2018
331
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 IronGirder
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Dictionary<string, int> passengers = new Dictionary<string, int>();
  12.             Dictionary<string, int> times = new Dictionary<string, int>();
  13.  
  14.             string input = Console.ReadLine();
  15.  
  16.             while (input != "Slide rule")
  17.             {
  18.                 string[] data = input.Split(":");
  19.  
  20.                 string town = data[0];
  21.                 string[] info = data[1].Split("->");
  22.  
  23.                 int numberOfPassengers = int.Parse(info[1]);
  24.  
  25.                 if (info[0] == "ambush")
  26.                 {
  27.                     if (times.ContainsKey(town))
  28.                     {
  29.                         times[town] = 0;
  30.                         passengers[town] -= numberOfPassengers;
  31.                     }
  32.                 }
  33.                 else
  34.                 {
  35.                     int currentTime = int.Parse(info[0]);
  36.  
  37.                     if (times.ContainsKey(town) == false)
  38.                     {
  39.                         times.Add(town, currentTime);
  40.                         passengers.Add(town, numberOfPassengers);
  41.                     }
  42.                     else
  43.                     {
  44.                         passengers[town] += numberOfPassengers;
  45.  
  46.                         if (times[town] > currentTime || times[town] == 0)
  47.                         {
  48.                             times[town] = currentTime;
  49.                         }
  50.                     }
  51.                 }
  52.  
  53.                 input = Console.ReadLine();
  54.             }
  55.  
  56.             foreach (var pair in times.OrderBy(p => p.Value).ThenBy(p => p.Key))
  57.             {
  58.                 string town = pair.Key;
  59.  
  60.                 if (times[town] == 0 || passengers[town] == 0)
  61.                 {
  62.                     continue;
  63.                 }
  64.  
  65.                 Console.WriteLine($"{town} -> Time: {pair.Value} -> Passengers: {passengers[town]}");
  66.             }
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement