Advertisement
YavorGrancharov

Trainlands(examTask04)

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