Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.14 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Problem_04
  6. {
  7.     public class Program
  8.     {
  9.         public static void Main()
  10.         {
  11.             var dict = new Dictionary<string, Dictionary<string, int>>();
  12.  
  13.             string inputline = Console.ReadLine();
  14.  
  15.             while (inputline != "It's Training Men!")
  16.             {
  17.                 if (inputline.Contains("->") && inputline.Contains(":"))
  18.                 {
  19.                     string[] tokens = inputline.Split(new string[] { " -> ", " : " }, StringSplitOptions.RemoveEmptyEntries);
  20.  
  21.                     string trainName = tokens[0];
  22.                     string wagonName = tokens[1];
  23.                     int wagonPower = int.Parse(tokens[2]);
  24.  
  25.                     if (!dict.ContainsKey(trainName))
  26.                     {
  27.                         dict[trainName] = new Dictionary<string, int>();
  28.                     }
  29.                     if (!dict[trainName].ContainsKey(wagonName))
  30.                     {
  31.                         dict[trainName][wagonName] = 0;
  32.                     }
  33.                     dict[trainName][wagonName] = wagonPower;
  34.                 }
  35.                 else if (inputline.Contains("->"))
  36.                 {
  37.                     string[] tokens = inputline.Split(new string[] { " -> " }, StringSplitOptions.RemoveEmptyEntries);
  38.  
  39.                     string trainName = tokens[0];
  40.                     string otherTrainName = tokens[1];
  41.  
  42.                     if (!dict.ContainsKey(trainName))
  43.                     {
  44.                         dict[trainName] = new Dictionary<string, int>();
  45.                     }
  46.                     var selectOtherTrains = dict
  47.                                            .Where(x => x.Key == otherTrainName)
  48.                                            .SelectMany(x => x.Value);
  49.  
  50.                     foreach (var wagon in selectOtherTrains)
  51.                     {
  52.                         dict[trainName].Add(wagon.Key, wagon.Value);
  53.                     }
  54.                     dict.Remove(otherTrainName);
  55.                 }
  56.                 else
  57.                 {
  58.                     string[] tokens = inputline.Split(new string[] { " = " }, StringSplitOptions.RemoveEmptyEntries);
  59.                     string trainName = tokens[0];
  60.                     string otherTrainName = tokens[1];
  61.  
  62.                     if (!dict.ContainsKey(trainName))
  63.                     {
  64.                         dict[trainName] = new Dictionary<string, int>();
  65.                     }
  66.                     dict[trainName] = dict[otherTrainName];
  67.  
  68.                 }
  69.                 inputline = Console.ReadLine();
  70.             }
  71.             var sortKeys = dict
  72.                           .OrderByDescending(x => x.Value.Values.Sum())
  73.                           .ThenBy(x => x.Value.Count);
  74.  
  75.             foreach (var train in sortKeys)
  76.             {
  77.                 Console.WriteLine($"Train: {train.Key}");
  78.                 foreach (var wagon in train.Value.OrderByDescending(x => x.Value))
  79.                 {
  80.                     Console.WriteLine($"###{wagon.Key} - {wagon.Value}");
  81.                 }
  82.             }
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement