simeon3000

4

Aug 25th, 2017
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.30 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. class Trainlands
  7. {
  8.     const string TerminatingString = "It's Training Men!";
  9.  
  10.     static Dictionary<string, Dictionary<string, long>> trains = new Dictionary<string, Dictionary<string, long>>();
  11.  
  12.     static void Main()
  13.     {
  14.         var wagonPattern = new Regex(@"^([^ \-:>=]+) (->|=) ([^ \-:>=]+) : (\d+)$");
  15.         var trainPattern = new Regex(@"^([^ \-:>=]+) (->|=) ([^ \-:>=]+)$");
  16.         string input = Console.ReadLine();
  17.  
  18.         while (input != TerminatingString)
  19.         {
  20.             var wagonMatch = wagonPattern.Match(input);
  21.             var trainMatch = trainPattern.Match(input);
  22.  
  23.             if (trainMatch.Success)
  24.             {
  25.                 string firstTrainName = trainMatch.Groups[1].Value;
  26.                 string sign = trainMatch.Groups[2].Value;
  27.                 string otherTrainName = trainMatch.Groups[3].Value;
  28.  
  29.                 if (sign == "->")
  30.                 {
  31.                     if (!trains.ContainsKey(firstTrainName))
  32.                     {
  33.                         trains.Add(firstTrainName, new Dictionary<string, long>());
  34.                     }
  35.  
  36.                     foreach (var wagon in trains[otherTrainName])
  37.                     {
  38.                         trains[firstTrainName].Add(wagon.Key, wagon.Value);
  39.                     }
  40.  
  41.                     trains.Remove(otherTrainName);
  42.                 }
  43.                 else
  44.                 {
  45.                     if (!trains.ContainsKey(firstTrainName))
  46.                     {
  47.                         trains.Add(firstTrainName, new Dictionary<string, long>());
  48.                     }
  49.                     //поправка
  50.                     trains[firstTrainName].Clear();
  51.                    
  52.                     foreach (var train in trains[otherTrainName])
  53.                             {
  54.                                 trains[firstTrainName].Add(train.Key, train.Value);
  55.                             }
  56.                 }
  57.             }
  58.             else if (wagonMatch.Success)
  59.             {
  60.                 string trainName = wagonMatch.Groups[1].Value;
  61.                 string wagonName = wagonMatch.Groups[3].Value;
  62.                 int wagonPower = int.Parse(wagonMatch.Groups[4].Value);
  63.  
  64.                 if (!trains.ContainsKey(trainName))
  65.                 {
  66.                     trains.Add(trainName, new Dictionary<string, long>());
  67.                 }
  68.                 //поправка
  69.                 if (!trains[trainName].ContainsKey(wagonName))
  70.                 {
  71.                 trains[trainName].Add(wagonName, 0);
  72.                 }
  73.                 trains[trainName][wagonName] += wagonPower;
  74.             }
  75.  
  76.             input = Console.ReadLine();
  77.         }
  78.  
  79.         var sortedTrains = trains
  80.             .OrderByDescending(t => t.Value.Values.Sum())
  81.             .ThenBy(t => t.Value.Count)
  82.             .ToDictionary(t => t.Key, np => np.Value);
  83.  
  84.         foreach (var train in sortedTrains.Keys)
  85.         {
  86.             Console.WriteLine($"Train: {train}");
  87.  
  88.             var sortedWagons = sortedTrains[train]
  89.                 .OrderByDescending(np => np.Value)
  90.                 .ToDictionary(np => np.Key, np => np.Value);
  91.  
  92.             foreach (var wagon in sortedWagons)
  93.             {
  94.                 Console.WriteLine($"###{wagon.Key} - {wagon.Value}");
  95.             }
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment