Advertisement
Militsa

04. Trainlands

Dec 8th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.02 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _04.Trainlands
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.              Dictionary<string, Dictionary<string, long>> allTrains
  14.                 = new Dictionary<string, Dictionary<string, long>>();
  15.  
  16.             string inputLine = Console.ReadLine();
  17.  
  18.             while (inputLine != "It's Training Men!")
  19.             {
  20.                 string[] inputParameters = inputLine
  21.                     .Split(new[] { " -> ", " : ", " = " }, StringSplitOptions.RemoveEmptyEntries);
  22.  
  23.                 string currentTrainName = inputParameters[0];
  24.  
  25.                 if (inputParameters.Length == 3)
  26.                 {
  27.                     string currentWagon = inputParameters[1];
  28.                     long currentPower = long.Parse(inputParameters[2]);
  29.  
  30.                     if (!allTrains.ContainsKey(currentTrainName))
  31.                     {
  32.                         allTrains[currentTrainName] = new Dictionary<string, long>();
  33.                     }
  34.  
  35.                     allTrains[currentTrainName].Add(currentWagon, currentPower);
  36.                 }
  37.                 else
  38.                 {
  39.                     string otherTrainName = inputParameters[1];
  40.  
  41.                     if (inputLine.Contains(" = "))
  42.                     {
  43.                         if (!allTrains.ContainsKey(currentTrainName))
  44.                         {
  45.                             allTrains[currentTrainName] = new Dictionary<string, long>();
  46.                         }
  47.  
  48.                         allTrains[currentTrainName].Clear();
  49.  
  50.                         foreach (var wagons in allTrains[otherTrainName])
  51.                         {
  52.                             allTrains[currentTrainName].Add(wagons.Key, wagons.Value);
  53.                         }
  54.                     }
  55.                     else
  56.                     {
  57.                         if (!allTrains.ContainsKey(currentTrainName))
  58.                         {
  59.                             allTrains[currentTrainName] = new Dictionary<string, long>();
  60.                         }
  61.  
  62.                         foreach (var wagons in allTrains[otherTrainName])
  63.                         {
  64.                             allTrains[currentTrainName].Add(wagons.Key, wagons.Value);
  65.                         }
  66.  
  67.                         allTrains.Remove(otherTrainName);
  68.                     }
  69.                 }
  70.  
  71.  
  72.                 inputLine = Console.ReadLine();
  73.             }
  74.  
  75.  
  76.             foreach (var train in allTrains
  77.                 .OrderByDescending(t => t.Value.Values.Sum())
  78.                 .ThenBy(t => t.Value.Count()))
  79.             {
  80.                 Console.WriteLine("Train: {0}", train.Key);
  81.  
  82.                 foreach (var wagon in allTrains[train.Key]
  83.                     .OrderByDescending(w => w.Value))
  84.                 {
  85.                     Console.WriteLine("###{0} - {1}",wagon.Key,wagon.Value);
  86.                 }
  87.             }
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement