VelizarAvramov

04. Trainlands

May 6th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.91 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _04._Trainlands
  6. {
  7.     class Trainlands
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             string command = Console.ReadLine();
  12.             Dictionary<string, Dictionary<string, int>> trains = new Dictionary<string, Dictionary<string, int>>();
  13.             while (command != "It’s Training Men!")
  14.             {
  15.                 string[] info = command.Split(' ');
  16.                 if (info.Length == 5)
  17.                 {
  18.                     string name = info[0];
  19.                     string wagon = info[2];
  20.                     int power = int.Parse(info[4]);
  21.                     if (!trains.ContainsKey(name))
  22.                     {
  23.                         Dictionary<string, int> current = new Dictionary<string, int>();
  24.                         current.Add(wagon, power);
  25.                         trains.Add(name, current);
  26.                     }
  27.                     else
  28.                     {
  29.                         if (!trains[name].ContainsKey(wagon))
  30.                         {
  31.                             trains[name].Add(wagon, power);
  32.                         }
  33.                     }
  34.                 }
  35.                 else if (info[1] == "->")
  36.                 {
  37.                     string first = info[0];
  38.                     string other = info[2];
  39.                     if (!trains.ContainsKey(first))
  40.                     {
  41.                         Dictionary<string, int> current = new Dictionary<string, int>();
  42.                         trains.Add(first, current);
  43.                     }
  44.                     foreach (var pair in trains[other])
  45.                     {
  46.                         trains[first].Add(pair.Key, pair.Value);
  47.                     }
  48.                     trains.Remove(other);
  49.                 }
  50.                 else
  51.                 {
  52.                     string first = info[0];
  53.                     string other = info[2];
  54.                     if (!trains.ContainsKey(first))
  55.                     {
  56.                         Dictionary<string, int> current = new Dictionary<string, int>();
  57.                         trains.Add(first, current);
  58.                     }
  59.                     trains[first].Clear();
  60.                     foreach (var pair in trains[other])
  61.                     {
  62.                         trains[first].Add(pair.Key, pair.Value);
  63.                     }
  64.                 }
  65.                 command = Console.ReadLine();
  66.             }
  67.             foreach (var train in trains.OrderByDescending
  68.                 (x => x.Value.Values.Sum()).
  69.                 ThenBy(x=>x.Value.Values.Count))
  70.             {
  71.                 Console.WriteLine($"Train: {train.Key}");
  72.                 foreach (var item in train.Value.OrderByDescending(x=>x.Value))
  73.                 {
  74.                     Console.WriteLine($"###{item.Key} - {item.Value}");
  75.                 }
  76.             }
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment