private static List <Legion> Reader() { List <Legion> legionsList = new List <Legion>(); int n = int.Parse(Console.ReadLine()); while (n-- > 0) { string[] cmdArgs = Console.ReadLine() .Split(new[] { " = ", " -> ", ":" }, StringSplitOptions.RemoveEmptyEntries); long lastActivity = long.Parse(cmdArgs[0]); string legionName = cmdArgs[1]; string soldier = cmdArgs[2]; long soldierCount = long.Parse(cmdArgs[3]); if (!legionsList.Any(l => l.LegionName.Contains(legionName))) { Legion legion = new Legion(lastActivity, legionName, soldier, soldierCount); legionsList.Add(legion); } else { Legion legion = legionsList.FirstOrDefault(l => l.LegionName.Equals(legionName)); if (legion.Activity < lastActivity) { legion.Activity = lastActivity; } if (legionsList.Any(l => l.LegionName.Equals(legionName) && l.Soldier.Equals(soldier))) { legion = legionsList.FirstOrDefault(l => l.LegionName.Equals(legionName) && l.Soldier.Equals(soldier)); legion.SoldierCount += soldierCount; } else if (legionsList.Any(l => l.Activity >= lastActivity && l.LegionName.Equals(legionName))) { long highestActivityForCurrentLegion = legion.Activity; legion = new Legion(highestActivityForCurrentLegion, legionName, soldier, soldierCount); legionsList.Add(legion); } } } return(legionsList); }
private static void CalculateLegions(Dictionary <string, Legion> dictLegion) { string[] input = Console.ReadLine() .Split(new string[] { "=", "->", ":", " " }, StringSplitOptions.RemoveEmptyEntries); long legionActivity = long.Parse(input[0]); string legionName = input[1]; string soldierTipe = input[2]; long soldierCount = long.Parse(input[3]); Legion newLegion = new Legion(); newLegion.LastActivity = legionActivity; newLegion.SoldierTipeSoldieName = new Dictionary <string, long>(); newLegion.SoldierTipeSoldieName.Add(soldierTipe, soldierCount); if (!dictLegion.ContainsKey(legionName)) { dictLegion.Add(legionName, newLegion); } else if (!dictLegion[legionName].SoldierTipeSoldieName.ContainsKey(soldierTipe)) { dictLegion[legionName].SoldierTipeSoldieName.Add(soldierTipe, soldierCount); if (dictLegion[legionName].LastActivity < legionActivity) { dictLegion[legionName].LastActivity = legionActivity; } } else { dictLegion[legionName].SoldierTipeSoldieName[soldierTipe] += soldierCount; if (dictLegion[legionName].LastActivity < legionActivity) { dictLegion[legionName].LastActivity = legionActivity; } } }
public static void Main() { var n = int.Parse(Console.ReadLine()); var separators = new char[] { '=', '-', '>', ':', ' ', '\t', '\n' }; var legionsSummary = new Dictionary <string, Legion>(); for (int i = 0; i < n; i++) { var input = Console.ReadLine() .Split(separators, StringSplitOptions.RemoveEmptyEntries) .ToList(); var currLastActivity = int.Parse(input[0]); var currLegionName = input[1]; var currSoldierType = input[2]; var currSoldierCount = long.Parse(input[3]); if (!legionsSummary.ContainsKey(currLegionName)) { var currentLegion = new Legion() { LastActivity = currLastActivity, Name = currLegionName, Soldiers = new Dictionary <string, long>() }; currentLegion.Soldiers.Add(currSoldierType, currSoldierCount); legionsSummary.Add(currLegionName, currentLegion); } else { if (currLastActivity > legionsSummary[currLegionName].LastActivity) { legionsSummary[currLegionName].LastActivity = currLastActivity; } if (legionsSummary[currLegionName].Soldiers.ContainsKey(currSoldierType)) { legionsSummary[currLegionName].Soldiers[currSoldierType] += currSoldierCount; } else { legionsSummary[currLegionName].Soldiers.Add(currSoldierType, currSoldierCount); } } } var printCommand = Console.ReadLine().Split('\\').ToList(); if (printCommand.Count == 2) { var lastActivity = int.Parse(printCommand[0]); var soldierType = printCommand[1]; var legionsToPrint = new Dictionary <string, long>(); foreach (var legion in legionsSummary.Values) { if (legion.LastActivity < lastActivity && legion.Soldiers.ContainsKey(soldierType)) { legionsToPrint.Add(legion.Name, legion.Soldiers[soldierType]); } } foreach (var kvp in legionsToPrint.OrderByDescending(x => x.Value)) { Console.WriteLine($"{kvp.Key} -> {kvp.Value}"); } } else if (printCommand.Count == 1) { var soldierType = printCommand[0]; foreach (var legion in legionsSummary.Values.OrderByDescending(x => x.LastActivity)) { if (legion.Soldiers.ContainsKey(soldierType)) { Console.WriteLine($"{legion.LastActivity} : {legion.Name}"); } } } }
static void Main(string[] args) { var legions = new List <Legion>(); var n = int.Parse(Console.ReadLine()); var pattern = @"(.+) = (.+) -> (.+):(.+)"; for (int i = 0; i < n; i++) { var line = Console.ReadLine(); MatchCollection matches = Regex.Matches(line, pattern); var activity = long.Parse(matches[0].Groups[1].Value); var legionName = matches[0].Groups[2].Value; var solderType = matches[0].Groups[3].Value; var solderCount = long.Parse(matches[0].Groups[4].Value); if (legions.Any(x => x.Name == legionName) == false) { var currentLegion = new Legion(); currentLegion.Name = legionName; currentLegion.Activity = activity; var currentEmptySolders = new List <Soldier>(); var currentEmptySolder = new Soldier(); currentEmptySolder.Type = solderType; currentEmptySolder.Count = solderCount; currentEmptySolders.Add(currentEmptySolder); currentLegion.Soldiers = currentEmptySolders; legions.Add(currentLegion); } else { var legionFirst = legions.First(x => x.Name == legionName); if (legionFirst.Activity < activity) { legionFirst.Activity = activity; } if (legionFirst.Soldiers.Any(x => x.Type == solderType) == false) { var currentEmptySolder = new Soldier(); currentEmptySolder.Type = solderType; currentEmptySolder.Count = solderCount; legionFirst.Soldiers.Add(currentEmptySolder); } else { var currentSolderTypeFirst = legionFirst.Soldiers.First(x => x.Type == solderType); currentSolderTypeFirst.Count += solderCount; } } } var outputCommant = Console.ReadLine(); if (Regex.IsMatch(outputCommant, @"(\d+)\\(.+)")) { MatchCollection matchesCommand = Regex.Matches(outputCommant, @"(\d+)\\(.+)"); var activityCommand = long.Parse(matchesCommand[0].Groups[1].Value); var soldierTypeCommand = matchesCommand[0].Groups[2].Value; var countSoldersByType = new Dictionary <string, long>(); foreach (var legion in legions.Where(x => x.Activity < activityCommand)) {// 0 solders if (legion.Soldiers.Any(x => x.Type == soldierTypeCommand)) { countSoldersByType.Add(legion.Name, legion.Soldiers.First(x => x.Type == soldierTypeCommand).Count); } //else //{ // countSoldersByType.Add(legion.Name, 0L); //} } foreach (var pair in countSoldersByType.OrderByDescending(x => x.Value)) { Console.WriteLine($"{pair.Key} -> {pair.Value}"); } } else { var legionWithActivity = new Dictionary <string, long>(); foreach (var legion in legions.Where(x => x.Soldiers.Any(z => z.Type == outputCommant))) { legionWithActivity.Add(legion.Name, legion.Activity); } foreach (var pair in legionWithActivity.OrderByDescending(x => x.Value)) { Console.WriteLine($"{pair.Value} : {pair.Key}"); } } }
public static void Main() { var n = decimal.Parse(Console.ReadLine()); var legions = new List <Legion>(); for (decimal i = 0; i < n; i++) { string[] input = Console.ReadLine() .Split(new char[] { ' ', '=', '-', ':', '>' }, StringSplitOptions.RemoveEmptyEntries); var legionActivity = decimal.Parse(input[0]); var legionName = input[1]; var soldierType = input[2]; var soldCount = decimal.Parse(input[3]); var TypeAndCount = new Dictionary <string, decimal> { { soldierType, soldCount } }; Legion legion = new Legion(); legion.LastActivity = legionActivity; legion.Name = legionName; legion.SoldierTypeAndCount = TypeAndCount; bool legionExist = false; foreach (var l in legions) { if (l.Name == legionName) { legionExist = true; if (l.SoldierTypeAndCount.ContainsKey(soldierType)) { l.SoldierTypeAndCount[soldierType] += soldCount; if (l.LastActivity < legionActivity) { l.LastActivity = legionActivity; } } else { l.SoldierTypeAndCount.Add(soldierType, soldCount); if (l.LastActivity < legionActivity) { l.LastActivity = legionActivity; } } } } if (legionExist == false) { legions.Add(legion); } } string[] printLine = Console.ReadLine().Split('\\'); if (printLine.Length > 1) { var printActivity = decimal.Parse(printLine[0]); var printSoldiers = printLine[1]; var result = legions.OrderByDescending(x => x.SoldierTypeAndCount.Values); foreach (var l in legions .Where(a => a.LastActivity < printActivity) .Where(x => x.SoldierTypeAndCount.ContainsKey(printSoldiers)) .OrderByDescending(s => s.SoldierTypeAndCount[printSoldiers])) { Console.WriteLine($"{l.Name} -> {l.SoldierTypeAndCount[printSoldiers]}"); } } foreach (var l in legions .Where(t => t.SoldierTypeAndCount.ContainsKey(printLine[0])) .OrderByDescending(a => a.LastActivity)) { Console.WriteLine($"{l.LastActivity} : {l.Name}"); } }
public static void Main() { var legionList = new List <Legion>(); var linesCount = long.Parse(Console.ReadLine()); for (int i = 0; i < linesCount; i++) { var tokens = Console.ReadLine().Split(); var soldierTokens = tokens[4].Split(':'); var lastActivity = int.Parse(tokens[0]); var legionName = tokens[2]; var soldierType = soldierTokens[0]; var soldierCount = long.Parse(soldierTokens[1]); var matchLegion = legionList .FindIndex(l => l.LegionName == legionName); var matchLegionAndSoldierType = legionList .FindIndex(x => x.LegionName == legionName & x.SoldierTypeAndCount.ContainsKey(soldierType)); if (matchLegionAndSoldierType >= 0) { legionList[matchLegionAndSoldierType].SoldierTypeAndCount[soldierType] += soldierCount; if (legionList[matchLegionAndSoldierType].LastActivity < lastActivity) { legionList[matchLegionAndSoldierType].LastActivity = lastActivity; } } else if (matchLegion >= 0) { legionList[matchLegion].SoldierTypeAndCount.Add(soldierType, soldierCount); if (legionList[matchLegion].LastActivity < lastActivity) { legionList[matchLegion].LastActivity = lastActivity; } } else { var soldierTypeAndCountDict = new Dictionary <string, long>(); soldierTypeAndCountDict.Add(soldierType, soldierCount); var legionAddInfo = new Legion(lastActivity, legionName, soldierTypeAndCountDict); legionList.Add(legionAddInfo); } } var printInput = Console.ReadLine().Split('\\'); if (printInput.Length > 1) { var lastActivity = int.Parse(printInput[0]); var soldierType = printInput[1]; var sorted = legionList .Where(l => l.LastActivity < lastActivity) .Where(s => s.SoldierTypeAndCount.ContainsKey(soldierType)) .OrderByDescending(l => l.SoldierTypeAndCount[soldierType]); foreach (var legion in sorted) { Console.WriteLine($"{legion.LegionName} -> {legion.SoldierTypeAndCount[soldierType]}"); } } else { var soldierType = printInput[0]; var sorted = legionList .Where(s => s.SoldierTypeAndCount.ContainsKey(soldierType)) .OrderByDescending(a => a.LastActivity); foreach (var legion in sorted) { Console.WriteLine($"{legion.LastActivity} : {legion.LegionName}"); } } }