private static void ReadLegionInfoAndPutToDic(Dictionary <string, Legion> legions) { string inputPattern = @"(\d+)\s\=\s(.+)\s\-\>\s(.+)\:(\d+)"; Regex inputRegex = new Regex(inputPattern); Match inputMatch = inputRegex.Match(Console.ReadLine()); int lastActivity = int.Parse(inputMatch.Groups[1].Value); string legionName = inputMatch.Groups[2].Value; string soldierType = inputMatch.Groups[3].Value; int soldierCount = int.Parse(inputMatch.Groups[4].Value); // Create new Legion if (!legions.ContainsKey(legionName)) { var newLegion = new Legion(); var newSoldier = new Soldier(); newLegion.LastActivity = lastActivity; newSoldier.Type = soldierType; newSoldier.Count = soldierCount; newLegion.SoldierList.Add(newSoldier); legions.Add(legionName, newLegion); } else { // Update last activity if (legions[legionName].LastActivity < lastActivity) { legions[legionName].LastActivity = lastActivity; } // Add new Soldier Type if (!legions[legionName].SoldierList.Any(s => s.Type == soldierType)) { var newSoldier = new Soldier(); newSoldier.Type = soldierType; newSoldier.Count = soldierCount; legions[legionName].SoldierList.Add(newSoldier); } // Update Soldier Count if (legions[legionName].SoldierList.Any(s => s.Type == soldierType)) { var existingSoldier = legions[legionName].SoldierList.Where(s => s.Type == soldierType).First(); existingSoldier.Count += soldierCount; } } }
static void Main(string[] args) { var n = int.Parse(Console.ReadLine()); var legions = new List <Legion>(); var isInList = false; for (int i = 0; i < n; i++) { var input = Console.ReadLine().Split(new[] { " = ", " -> ", ":" }, StringSplitOptions.RemoveEmptyEntries).ToArray(); foreach (var item in legions) { if (item.Name == input[1]) { isInList = true; if (item.LastActivity < long.Parse(input[0])) { item.LastActivity = long.Parse(input[0]); } if (!item.SoldierType.Contains(input[2])) { item.SoldierType.Add(input[2]); item.SoldierCount += long.Parse(input[3]); } else { item.SoldierCount += long.Parse(input[3]); } } } if (!isInList) { var legion = new Legion(); legion.LastActivity = long.Parse(input[0]); legion.Name = input[1]; legion.SoldierType = new List <string>(); legion.SoldierType.Add(input[2]); legion.SoldierCount = long.Parse(input[3]); legions.Add(legion); } isInList = false; } var command = Console.ReadLine().Split('\\').ToArray(); if (command.Length > 1) { var activity = int.Parse(command[0]); var soldierType = command[1]; foreach (var item in legions.Where(l => l.LastActivity < activity).OrderByDescending(sc => sc.SoldierCount)) { if (item.SoldierType.Contains(soldierType)) { Console.WriteLine($"{item.Name} -> {item.SoldierCount}"); } } } else { var soldierType = command[0]; foreach (var item in legions.OrderByDescending(a => a.LastActivity)) { if (item.SoldierType.Contains(soldierType)) { Console.WriteLine($"{item.LastActivity} : {item.Name}"); } } } }
public static void Main(string[] args) { List <Legion> legions = new List <Legion>(); int numberOfLines = int.Parse(Console.ReadLine()); for (int i = 0; i < numberOfLines; i++) { string[] token = Console.ReadLine() .Split(new string[] { " = ", " -> ", ":" }, StringSplitOptions.RemoveEmptyEntries); int lastActivity = int.Parse(token[0]); string legionName = token[1]; string soldierType = token[2]; long soldierCount = long.Parse(token[3]); if (!legions.Any(x => x.Name == legionName)) { legions.Add(new Legion(legionName)); } Legion currentLegion = legions.Find(x => x.Name == legionName); if (!currentLegion.Soldiers.ContainsKey(soldierType)) { currentLegion.Soldiers.Add(soldierType, 0); } currentLegion.Soldiers[soldierType] += soldierCount; if (currentLegion.Activity < lastActivity) { currentLegion.Activity = lastActivity; } } string[] input = Console.ReadLine().Split('\\'); if (input.Length == 1) { string soldierType = input[0]; foreach (Legion legion in legions.OrderByDescending(x => x.Activity)) { if (legion.Soldiers.ContainsKey(soldierType)) { Console.WriteLine($"{legion.Activity} : {legion.Name}"); } } } else { int activity = int.Parse(input[0]); string soldierType = input[1]; foreach (Legion legion in legions .Where(x => x.Soldiers.ContainsKey(soldierType)) .OrderByDescending(x => x.Soldiers[soldierType])) { if (legion.Activity < activity) { Console.WriteLine($"{legion.Name} -> {legion.Soldiers[soldierType]}"); } } } }