Advertisement
NelIfandieva

ProgFund_Exam_27Aug2018_Pr04_IronGirder

Oct 27th, 2018
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5. using System.Numerics;
  6. namespace Exam_Pr03
  7. {
  8.     class Program
  9.     {
  10.         static void Main()
  11.         {
  12.             Dictionary<string, int> townToTimes = new Dictionary<string, int>();
  13.             Dictionary<string, ulong> townToPassengers = new Dictionary<string, ulong>();
  14.             while (true)
  15.             {
  16.                 var input = Console.ReadLine();
  17.                 if(input == "Slide rule")
  18.                 {
  19.                     break;
  20.                 }
  21.                 string[] arrForTown = input.Split(new[] { ':'}, StringSplitOptions.RemoveEmptyEntries);
  22.                 string town = arrForTown[0];
  23.                 string[] arrForTime = input.Split(new[] { ':', '>'}, StringSplitOptions.RemoveEmptyEntries);
  24.                 string rawTime = arrForTime[1];
  25.                 rawTime = rawTime.Trim('-');
  26.                 int time;
  27.                 string ambush = "";
  28.                 ulong passengers = ulong.Parse(arrForTime[2]);
  29.                 if (int.TryParse(rawTime, out time))
  30.                 {
  31.                     time = int.Parse(rawTime);
  32.                 }
  33.                 else
  34.                 {
  35.                     ambush = rawTime;
  36.                 }
  37.                 if (ambush != "")
  38.                 {
  39.                     if(townToTimes.ContainsKey(town))
  40.                     {
  41.                         townToTimes[town] = 0;
  42.                         townToPassengers[town] -= passengers;
  43.                     }
  44.                 }
  45.                 else
  46.                 {
  47.                     if(!townToTimes.ContainsKey(town))
  48.                     {
  49.                         townToTimes.Add(town, time);
  50.                         townToPassengers.Add(town, passengers);
  51.                     }
  52.                     else
  53.                     {
  54.                         if(townToTimes[town] == 0 || townToTimes[town] > time)
  55.                         {
  56.                             townToTimes[town] = time;
  57.                         }
  58.                         townToPassengers[town] += passengers;
  59.                     }
  60.                 }
  61.             }
  62.             foreach(var entry in townToTimes.OrderBy(kvp => kvp.Value).ThenBy(kvp => kvp.Key))
  63.             {
  64.                 string town = entry.Key;
  65.                 int time = entry.Value;
  66.                 ulong passengersCount = townToPassengers[town];
  67.                 if(time < 1 || passengersCount <= 1)
  68.                 {
  69.                     continue;
  70.                 }
  71.                 Console.WriteLine($"{town} -> Time: {time} -> Passengers: {passengersCount}");
  72.             }
  73.         }
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement