Ejemplo n.º 1
0
 private static void CreateTrain(string name, Wagon wagon)
 {
     if (trains.ContainsKey(name))
     {
         trains[name].Add(wagon);
     }
     else
     {
         trains.Add(name, new List <Wagon> {
             wagon
         });
     }
 }
Ejemplo n.º 2
0
        private static bool TakeLine()
        {
            string input = Console.ReadLine();

            if (input == "It's Training Men!")
            {
                Print();
                return(false);
            }

            string createPattern = @"^((?<trainName>[^\s\-\:\>\=]+) -> (?<wagonName>[^\s\-\:\>\=]+) : (?<wagonPower>[0-9]+))$";
            string addPattern    = @"^((?<first>[^\s\-\:\>\=]+) -> (?<second>[^\s\-\:\>\=]+))$";
            string copyPattern   = @"^((?<first>[^\s\-\:\>\=]+) = (?<second>[^\s\-\:\>\=]+))$";

            if (Regex.IsMatch(input, createPattern))
            {
                Match  match      = Regex.Match(input, createPattern);
                string trainName  = match.Groups["trainName"].Value;
                string wagonName  = match.Groups["wagonName"].Value;
                long   wagonPower = long.Parse(match.Groups["wagonPower"].Value);

                Wagon wagon = new Wagon(wagonName, wagonPower);

                CreateTrain(trainName, wagon);
            }
            else if (Regex.IsMatch(input, addPattern))
            {
                Match  match  = Regex.Match(input, addPattern);
                string first  = match.Groups["first"].Value;
                string second = match.Groups["second"].Value;
                AddAll(first, second);
            }
            else if (Regex.IsMatch(input, copyPattern))
            {
                Match  match  = Regex.Match(input, copyPattern);
                string first  = match.Groups["first"].Value;
                string second = match.Groups["second"].Value;
                Copy(first, second);
            }

            return(true);
        }
Ejemplo n.º 3
0
        static void Main()
        {
            Dictionary <string, List <Wagon> > trains = new Dictionary <string, List <Wagon> >();
            Dictionary <string, long>          power  = new Dictionary <string, long>();

            string input = string.Empty;

            while ((input = Console.ReadLine()) != "It's Training Men!")
            {
                string[] args = input.Split(new[] { ' ', '>', '-', '=', ':' }, StringSplitOptions.RemoveEmptyEntries);

                if (args.Length == 3)
                {
                    string trainName = args[0];
                    string wagName   = args[1];
                    int    warPow    = int.Parse(args[2]);

                    if (!trains.ContainsKey(trainName))
                    {
                        trains.Add(trainName, new List <Wagon>());
                    }
                    Wagon wag = new Wagon()
                    {
                        Name = wagName, Power = warPow
                    };
                    trains[trainName].Add(wag);
                }
                else if (args.Length == 2)
                {
                    string[] command = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                    if (command[1] == "->")
                    {
                        string firstTrain = args[0];
                        string secTrain   = args[1];

                        var sec    = trains.First(x => x.Key == secTrain);
                        var secWag = sec.Value.ToList();
                        if (trains.ContainsKey(firstTrain))
                        {
                            trains[firstTrain].AddRange(secWag);
                        }
                        else
                        {
                            trains.Add(firstTrain, secWag);
                        }
                        trains.Remove(secTrain);
                    }
                    else if (command[1] == "=")
                    {
                        string firstTrain = args[0];
                        string secTrain   = args[1];

                        var sec    = trains.First(x => x.Key == secTrain);
                        var secWag = sec.Value.ToList();

                        trains[firstTrain] = new List <Wagon>();
                        trains[firstTrain].AddRange(secWag);
                    }
                }
            }
            foreach (var train in trains)
            {
                power.Add(train.Key, 0);

                foreach (var wag in train.Value)
                {
                    power[train.Key] += wag.Power;
                }
            }
            var ordered = trains
                          .OrderByDescending(x => power[x.Key])
                          .ThenBy(x => x.Value.Count)
                          .ToDictionary(x => x.Key, y => y.Value);

            foreach (var train in ordered)
            {
                Console.WriteLine($"Train: {train.Key}");

                foreach (var wag in train.Value.OrderByDescending(x => x.Power))
                {
                    Console.WriteLine($"###{wag.Name} - {wag.Power}");
                }
            }
        }