Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Problem_04
- {
- public class Program
- {
- public static void Main()
- {
- var dict = new Dictionary<string, Dictionary<string, int>>();
- string inputline = Console.ReadLine();
- while (inputline != "It's Training Men!")
- {
- if (inputline.Contains("->") && inputline.Contains(":"))
- {
- string[] tokens = inputline.Split(new string[] { " -> ", " : " }, StringSplitOptions.RemoveEmptyEntries);
- string trainName = tokens[0];
- string wagonName = tokens[1];
- int wagonPower = int.Parse(tokens[2]);
- if (!dict.ContainsKey(trainName))
- {
- dict[trainName] = new Dictionary<string, int>();
- }
- if (!dict[trainName].ContainsKey(wagonName))
- {
- dict[trainName][wagonName] = 0;
- }
- dict[trainName][wagonName] = wagonPower;
- }
- else if (inputline.Contains("->"))
- {
- string[] tokens = inputline.Split(new string[] { " -> " }, StringSplitOptions.RemoveEmptyEntries);
- string trainName = tokens[0];
- string otherTrainName = tokens[1];
- if (!dict.ContainsKey(trainName))
- {
- dict[trainName] = new Dictionary<string, int>();
- }
- var selectOtherTrains = dict
- .Where(x => x.Key == otherTrainName)
- .SelectMany(x => x.Value);
- foreach (var wagon in selectOtherTrains)
- {
- dict[trainName].Add(wagon.Key, wagon.Value);
- }
- dict.Remove(otherTrainName);
- }
- else
- {
- string[] tokens = inputline.Split(new string[] { " = " }, StringSplitOptions.RemoveEmptyEntries);
- string trainName = tokens[0];
- string otherTrainName = tokens[1];
- if (!dict.ContainsKey(trainName))
- {
- dict[trainName] = new Dictionary<string, int>();
- }
- dict[trainName] = dict[otherTrainName];
- }
- inputline = Console.ReadLine();
- }
- var sortKeys = dict
- .OrderByDescending(x => x.Value.Values.Sum())
- .ThenBy(x => x.Value.Count);
- foreach (var train in sortKeys)
- {
- Console.WriteLine($"Train: {train.Key}");
- foreach (var wagon in train.Value.OrderByDescending(x => x.Value))
- {
- Console.WriteLine($"###{wagon.Key} - {wagon.Value}");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement