Advertisement
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;
- using System.Threading.Tasks;
- namespace _04_trainlands
- {
- class Wagon
- {
- public string WagonName { get; set; }
- public int WagonPower { get; set; }
- public Wagon(string name, int power)
- {
- WagonName = name;
- WagonPower = power;
- }
- }
- class trainlands
- {
- static void Main(string[] args)
- {
- Dictionary<string, List<Wagon>> trains = new Dictionary<string, List<Wagon>>();
- string input = Console.ReadLine();
- string trainName = "";
- while (input != "It's Training Men!")
- {
- string[] inputTokkens = input.Split().ToArray();
- if (inputTokkens.Length > 3)
- {
- trainName = inputTokkens[0];
- string wagonName = inputTokkens[2];
- int wagonPower = int.Parse(inputTokkens[4]);
- if (!trains.ContainsKey(trainName))
- {
- trains.Add(trainName, new List<Wagon>());
- trains[trainName].Add(new Wagon(wagonName, wagonPower));
- }
- else
- {
- trains[trainName].Add(new Wagon(wagonName, wagonPower));
- }
- }
- else if (inputTokkens[1] == "->" && inputTokkens.Length == 3)
- {
- trainName = inputTokkens[0];
- string otherTrainName = inputTokkens[2];
- if (!trains.ContainsKey(trainName))
- {
- trains.Add(trainName, new List<Wagon>());
- trains[trainName].AddRange(trains[otherTrainName]);
- trains.Remove(otherTrainName);
- }
- else
- {
- trains[trainName].AddRange(trains[otherTrainName]);
- trains.Remove(otherTrainName);
- }
- }
- else if (inputTokkens[1] == "=" && inputTokkens.Length == 3)
- {
- trainName = inputTokkens[0];
- string otherTrainName = inputTokkens[2];
- if (!trains.ContainsKey(trainName))
- {
- trains.Add(trainName, new List<Wagon>());
- trains[trainName].AddRange(trains[otherTrainName]);
- }
- else
- {
- trains[trainName].Clear();
- trains[trainName].AddRange(trains[otherTrainName]);
- }
- }
- input = Console.ReadLine();
- }
- var orderedTrains = trains
- .OrderByDescending(t => t.Value.Sum(x => x.WagonPower))
- .ThenBy(t => t.Value.Count);
- foreach (var train in orderedTrains)
- {
- Console.WriteLine("Train: {0}", train.Key);
- foreach (var wagon in train.Value.OrderByDescending(t => t.WagonPower))
- {
- Console.WriteLine("###{0} - {1}",wagon.WagonName, wagon.WagonPower);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement