Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _04.Trainlands
- {
- class Trainlands
- {
- static void Main()
- {
- string input = Console.ReadLine();
- Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>();
- while (input != "It's Training Men!")
- {
- string[] inputLine = input.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
- if (inputLine.Length == 5)
- {
- string name = inputLine[0];
- string wagon = inputLine[2] + " - " + inputLine[4];
- if (!dict.ContainsKey(name))
- {
- dict.Add(name, new List<string>());
- }
- dict[name].Add(wagon);
- }
- if (inputLine.Length == 3 && inputLine[1] == "->")
- {
- string trainName = inputLine[0];
- string otherTrainName = inputLine[2];
- if (dict.ContainsKey(trainName))
- {
- dict[trainName].AddRange(dict[otherTrainName]);
- dict.Remove(otherTrainName);
- }
- else
- {
- dict.Add(trainName, dict[otherTrainName]);
- dict.Remove(otherTrainName);
- }
- }
- if (inputLine.Length == 3 && inputLine[1] == "=")
- {
- string trainName = inputLine[0];
- string otherTrainName = inputLine[2];
- if (dict.ContainsKey(trainName))
- {
- dict[trainName] = dict[otherTrainName];
- }
- else
- {
- dict.Add(trainName, dict[otherTrainName]);
- }
- }
- input = Console.ReadLine();
- }
- foreach (var item in dict)
- {
- Console.WriteLine($"Train: {item.Key}");
- foreach (var it in item.Value.OrderByDescending(x => int.Parse(x.Split(new char[]
- { ' ', '-'}, StringSplitOptions.RemoveEmptyEntries).Last())))
- {
- Console.WriteLine("###" + it);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement