Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.RegularExpressions;
- class Trainlands
- {
- const string TerminatingString = "It's Training Men!";
- static Dictionary<string, Dictionary<string, long>> trains = new Dictionary<string, Dictionary<string, long>>();
- static void Main()
- {
- var wagonPattern = new Regex(@"^([^ \-:>=]+) (->|=) ([^ \-:>=]+) : (\d+)$");
- var trainPattern = new Regex(@"^([^ \-:>=]+) (->|=) ([^ \-:>=]+)$");
- string input = Console.ReadLine();
- while (input != TerminatingString)
- {
- var wagonMatch = wagonPattern.Match(input);
- var trainMatch = trainPattern.Match(input);
- if (trainMatch.Success)
- {
- string firstTrainName = trainMatch.Groups[1].Value;
- string sign = trainMatch.Groups[2].Value;
- string otherTrainName = trainMatch.Groups[3].Value;
- if (sign == "->")
- {
- if (!trains.ContainsKey(firstTrainName))
- {
- trains.Add(firstTrainName, new Dictionary<string, long>());
- }
- foreach (var wagon in trains[otherTrainName])
- {
- trains[firstTrainName].Add(wagon.Key, wagon.Value);
- }
- trains.Remove(otherTrainName);
- }
- else
- {
- if (!trains.ContainsKey(firstTrainName))
- {
- trains.Add(firstTrainName, new Dictionary<string, long>());
- }
- //поправка
- trains[firstTrainName].Clear();
- foreach (var train in trains[otherTrainName])
- {
- trains[firstTrainName].Add(train.Key, train.Value);
- }
- }
- }
- else if (wagonMatch.Success)
- {
- string trainName = wagonMatch.Groups[1].Value;
- string wagonName = wagonMatch.Groups[3].Value;
- int wagonPower = int.Parse(wagonMatch.Groups[4].Value);
- if (!trains.ContainsKey(trainName))
- {
- trains.Add(trainName, new Dictionary<string, long>());
- }
- //поправка
- if (!trains[trainName].ContainsKey(wagonName))
- {
- trains[trainName].Add(wagonName, 0);
- }
- trains[trainName][wagonName] += wagonPower;
- }
- input = Console.ReadLine();
- }
- var sortedTrains = trains
- .OrderByDescending(t => t.Value.Values.Sum())
- .ThenBy(t => t.Value.Count)
- .ToDictionary(t => t.Key, np => np.Value);
- foreach (var train in sortedTrains.Keys)
- {
- Console.WriteLine($"Train: {train}");
- var sortedWagons = sortedTrains[train]
- .OrderByDescending(np => np.Value)
- .ToDictionary(np => np.Key, np => np.Value);
- foreach (var wagon in sortedWagons)
- {
- Console.WriteLine($"###{wagon.Key} - {wagon.Value}");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment