Advertisement
North_Point

04. Trainlands

Aug 21st, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.06 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.Test
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             var dic = new Dictionary<string, Dictionary<string, long>>();
  14.  
  15.             while (true)
  16.             {
  17.                 var input = Console.ReadLine();
  18.                 if (input == "It's Training Men!")
  19.                 {
  20.                     break;
  21.                 }
  22.                 if (input.Contains(':'))
  23.                 {
  24.                     var tokens = input.Split(new string[] { " -> " }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToArray();
  25.                     var trainName = tokens[0];
  26.                     var wagons = tokens[1].Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToArray();
  27.                     var wagonName = wagons[0];
  28.                     var wagonPower = long.Parse(wagons[1].Trim());
  29.  
  30.                     if (!dic.ContainsKey(trainName))
  31.                     {
  32.                         dic.Add(trainName, new Dictionary<string, long>());
  33.                     }
  34.                     if (!dic[trainName].ContainsKey(wagonName))
  35.                     {
  36.                         dic[trainName].Add(wagonName, 0);
  37.                     }
  38.                     dic[trainName][wagonName] = wagonPower;
  39.                 }
  40.                 else if (input.Contains('='))
  41.                 {
  42.                     var tokens = input.Split(new string[] { " = " }, StringSplitOptions.RemoveEmptyEntries);
  43.                     var first = tokens[0];
  44.                     var other = tokens[1];
  45.                     dic[first].Clear();
  46.  
  47.                     foreach (var item in dic[other])
  48.                     {
  49.                         if (!dic.ContainsKey(item.Key))
  50.                         {
  51.                             dic[first][item.Key] = item.Value;
  52.                         }
  53.                     }
  54.                 }
  55.                 else
  56.                 {
  57.                     var tokens = input.Split(new string[] { " -> " }, StringSplitOptions.RemoveEmptyEntries);
  58.                     var first = tokens[0];
  59.                     var second = tokens[1];
  60.                     foreach (var item in dic[second])
  61.                     {
  62.                         if (!dic.ContainsKey(first))
  63.                         {
  64.                             dic.Add(first, new Dictionary<string, long>());
  65.                         }
  66.                         dic[first].Add(item.Key, item.Value);
  67.                     }
  68.                     dic.Remove(second);
  69.                 }
  70.             }
  71.  
  72.             foreach (var item in dic.OrderByDescending(x => x.Value.Values.Sum()).ThenBy(y => y.Value.Count()))
  73.             {
  74.                 Console.WriteLine("Train: {0}", item.Key);
  75.                 foreach (var kvp in item.Value.OrderByDescending(x => x.Value))
  76.                 {
  77.                     Console.WriteLine("###{0} - {1}", kvp.Key, kvp.Value);
  78.                 }
  79.             }
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement