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.Globalization;
- using System.Text.RegularExpressions;
- namespace lab4.Trainlands
- {
- class Program
- {
- static void Main(string[] args)
- {
- string input = Console.ReadLine();
- var saveTrains = new Dictionary<string, Dictionary<string, int>>();
- while (input != "It's Training Men!")
- {
- string[] tokens = input.Split(new char[] { ' ', ':', '-', '>', '=' }, StringSplitOptions.RemoveEmptyEntries);
- if (tokens.Length == 3)
- {
- string trainName = tokens[0];
- string wagonName = tokens[1];
- int wagonPower = int.Parse(tokens[2], NumberFormatInfo.InvariantInfo);
- if (!saveTrains.ContainsKey(trainName))
- {
- saveTrains.Add(trainName, new Dictionary<string, int>());
- }
- saveTrains[trainName].Add(wagonName, wagonPower);
- }
- else
- {
- Regex removeTrain = new Regex(@" \-> ");
- Regex copyWagons = new Regex(@" = ");
- if (removeTrain.IsMatch(input))
- {
- string trainToPutWagons = tokens[0];
- string trainToRemove = tokens[1];
- if (!saveTrains.ContainsKey(trainToPutWagons))
- {
- saveTrains.Add(trainToPutWagons , new Dictionary<string, int>());
- }
- foreach (var wagon in saveTrains[trainToRemove])
- {
- string wago = wagon.Key;
- int pow = wagon.Value;
- saveTrains[trainToPutWagons].Add(wago, pow);
- }
- saveTrains = saveTrains
- .Where(t => t.Key != trainToRemove)
- .ToDictionary(k => k.Key, val => val.Value);
- }
- else if (copyWagons.IsMatch(input))
- {
- string trainToReplaceWagons = tokens[0];
- string trainCopyWagons = tokens[1];
- if(!saveTrains.ContainsKey(trainToReplaceWagons))
- {
- saveTrains.Add(trainToReplaceWagons, new Dictionary<string, int>());
- }
- saveTrains[trainToReplaceWagons].Clear();
- foreach (var wagons in saveTrains[trainCopyWagons])
- {
- string wagon = wagons.Key;
- int power = wagons.Value;
- saveTrains[trainToReplaceWagons].Add(wagon, power);
- }
- }
- }
- input = Console.ReadLine();
- }
- foreach (var train in saveTrains
- .OrderByDescending(x => x.Value.Values.Sum())
- .ThenBy(x => x.Value.Count()))
- {
- string name = train.Key;
- Dictionary<string, int> wagons = train.Value;
- Console.WriteLine("Train: {0}",name);
- foreach (var wagon in wagons.OrderByDescending(x => x.Value))
- {
- string wagonName = wagon.Key;
- int power = wagon.Value;
- Console.WriteLine("###{0} - {1}",wagonName,power);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement