Advertisement
nikiman8888

Trainslands ExamTest

May 23rd, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Globalization;
  5. using System.Text.RegularExpressions;
  6.  
  7. namespace lab4.Trainlands
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string input = Console.ReadLine();
  14.             var saveTrains = new Dictionary<string, Dictionary<string, int>>();
  15.  
  16.             while (input != "It's Training Men!")
  17.             {
  18.                 string[] tokens = input.Split(new char[] { ' ', ':', '-', '>', '=' }, StringSplitOptions.RemoveEmptyEntries);
  19.  
  20.                 if (tokens.Length == 3)
  21.                 {
  22.                     string trainName = tokens[0];
  23.                     string wagonName = tokens[1];
  24.                     int wagonPower = int.Parse(tokens[2], NumberFormatInfo.InvariantInfo);
  25.  
  26.                     if (!saveTrains.ContainsKey(trainName))
  27.                     {
  28.                         saveTrains.Add(trainName, new Dictionary<string, int>());
  29.                     }
  30.                     saveTrains[trainName].Add(wagonName, wagonPower);
  31.                 }
  32.                 else
  33.                 {
  34.                     Regex removeTrain = new Regex(@" \-> ");
  35.                     Regex copyWagons = new Regex(@" = ");
  36.  
  37.                     if (removeTrain.IsMatch(input))
  38.                     {
  39.                         string trainToPutWagons = tokens[0];
  40.                         string trainToRemove = tokens[1];
  41.  
  42.                         if (!saveTrains.ContainsKey(trainToPutWagons))
  43.                         {
  44.                             saveTrains.Add(trainToPutWagons , new Dictionary<string, int>());
  45.                         }
  46.                                                                      
  47.                             foreach (var wagon   in saveTrains[trainToRemove])
  48.                             {
  49.                                 string wago = wagon.Key;
  50.  
  51.                                 int pow = wagon.Value;
  52.  
  53.                                 saveTrains[trainToPutWagons].Add(wago, pow);
  54.                             }
  55.                         saveTrains = saveTrains
  56.                             .Where(t => t.Key != trainToRemove)
  57.                             .ToDictionary(k => k.Key, val => val.Value);
  58.                        
  59.                     }
  60.                     else if (copyWagons.IsMatch(input))
  61.                     {
  62.                         string trainToReplaceWagons = tokens[0];
  63.                         string trainCopyWagons = tokens[1];
  64.  
  65.                         if(!saveTrains.ContainsKey(trainToReplaceWagons))
  66.                         {
  67.                             saveTrains.Add(trainToReplaceWagons, new Dictionary<string, int>());
  68.                         }
  69.                      
  70.                         saveTrains[trainToReplaceWagons].Clear();
  71.                         foreach (var wagons in saveTrains[trainCopyWagons])
  72.                         {
  73.                             string wagon = wagons.Key;
  74.                             int power = wagons.Value;
  75.                             saveTrains[trainToReplaceWagons].Add(wagon, power);
  76.                         }
  77.  
  78.                     }
  79.                 }
  80.                 input = Console.ReadLine();
  81.             }
  82.             foreach (var train in saveTrains
  83.                 .OrderByDescending(x => x.Value.Values.Sum())
  84.                 .ThenBy(x => x.Value.Count()))
  85.             {
  86.                 string name = train.Key;
  87.                 Dictionary<string, int> wagons = train.Value;
  88.                 Console.WriteLine("Train: {0}",name);
  89.                 foreach (var wagon in wagons.OrderByDescending(x => x.Value))
  90.                 {
  91.                     string wagonName = wagon.Key;
  92.                     int power = wagon.Value;
  93.                     Console.WriteLine("###{0} - {1}",wagonName,power);
  94.                 }
  95.             }
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement