Advertisement
paykova

Trainlands

Apr 15th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _04.Trainlands
  6. {
  7. class Trainlands
  8. {
  9. static void Main()
  10. {
  11. string input = Console.ReadLine();
  12. Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>();
  13.  
  14.  
  15. while (input != "It's Training Men!")
  16. {
  17. string[] inputLine = input.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
  18.  
  19. if (inputLine.Length == 5)
  20. {
  21. string name = inputLine[0];
  22. string wagon = inputLine[2] + " - " + inputLine[4];
  23. if (!dict.ContainsKey(name))
  24. {
  25. dict.Add(name, new List<string>());
  26. }
  27. dict[name].Add(wagon);
  28. }
  29. if (inputLine.Length == 3 && inputLine[1] == "->")
  30. {
  31. string trainName = inputLine[0];
  32. string otherTrainName = inputLine[2];
  33.  
  34. if (dict.ContainsKey(trainName))
  35. {
  36. dict[trainName].AddRange(dict[otherTrainName]);
  37. dict.Remove(otherTrainName);
  38. }
  39. else
  40. {
  41. dict.Add(trainName, dict[otherTrainName]);
  42. dict.Remove(otherTrainName);
  43. }
  44. }
  45. if (inputLine.Length == 3 && inputLine[1] == "=")
  46. {
  47. string trainName = inputLine[0];
  48. string otherTrainName = inputLine[2];
  49.  
  50. if (dict.ContainsKey(trainName))
  51. {
  52. dict[trainName] = dict[otherTrainName];
  53. }
  54. else
  55. {
  56. dict.Add(trainName, dict[otherTrainName]);
  57. }
  58. }
  59. input = Console.ReadLine();
  60. }
  61.  
  62. foreach (var item in dict)
  63. {
  64. Console.WriteLine($"Train: {item.Key}");
  65. foreach (var it in item.Value.OrderByDescending(x => int.Parse(x.Split(new char[]
  66. { ' ', '-'}, StringSplitOptions.RemoveEmptyEntries).Last())))
  67. {
  68. Console.WriteLine("###" + it);
  69. }
  70. }
  71. }
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement