static void Main() { string patternForNumbers = @"([-+]?(\d+\.)?\d+)"; string patternForLetters = @"[^\d+\-*/.]"; List <Demon> resultList = new List <Demon>(); string[] input = Console.ReadLine().Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToArray(); for (int i = 0; i < input.Length; i++) { input[i] = input[i].Trim(); double damage = 0; double health = 0; CalculateHealthAndDamage(patternForNumbers, patternForLetters, input, i, ref damage, ref health); Demon newDemon = new Demon() { Name = input[i], Health = health, Damage = damage }; resultList.Add(newDemon); } foreach (var demon in resultList.OrderBy(x => x.Name)) { Console.WriteLine($"{demon.Name} - {demon.Health} health, {demon.Damage:f2} damage"); } }
internal static Demon Parse(string demonStr) { var name = demonStr; var damageRegex = new Regex(@"-?\d+(?:\.\d+)?"); var healthRegex = new Regex(@"[^\d\-*\/\.]"); var health = healthRegex .Matches(demonStr) .Cast <Match>() .Select(a => (int)char.Parse(a.Value)) .Sum(); var damage = damageRegex.Matches(demonStr) .Cast <Match>() .Select(a => decimal.Parse(a.Value)) .Sum(); var multiplyCount = demonStr.Count(a => a == '*'); var divideCount = demonStr.Count(a => a == '/'); damage *= (int)Math.Pow(2, multiplyCount); damage /= (int)Math.Pow(2, divideCount); var demon = new Demon { Name = name, Damage = damage, Health = health }; return(demon); }
public static void Main() { var dragonsNames = Console.ReadLine() .Split(new char[] { ',', ' ' } , StringSplitOptions.RemoveEmptyEntries); var healthRegex = new Regex(@"[^0-9\+\-\*\.\/]"); var damageRegex = new Regex(@"-?\d+\.?\d*"); var arithmeticSymbols = new Regex(@"[\*\/]"); var demons = new List <Demon>(); foreach (var dragonName in dragonsNames) { var health = 0; var healthSymbolsMatches = healthRegex.Matches(dragonName); foreach (Match symbol in healthSymbolsMatches) { health += symbol.Value.ToCharArray().First(); } double damage = 0; var damageSymbolsMatches = damageRegex.Matches(dragonName); foreach (Match symbol in damageSymbolsMatches) { damage += double.Parse(symbol.Value); } var arithmeticSymbolsMatches = arithmeticSymbols.Matches(dragonName); foreach (Match symbol in arithmeticSymbolsMatches) { if (symbol.Value == "*") { damage *= 2; } else { damage /= 2; } } var currentDemon = new Demon { Name = dragonName, Health = health, Damage = damage }; demons.Add(currentDemon); } foreach (var demon in demons.OrderBy(n => n.Name)) { Console.WriteLine($"{demon.Name} - {demon.Health} health, {demon.Damage:f2} damage"); } }
public static Demon Parse(string demonName) { var demon = new Demon(); demon.Name = demonName; demon.Health = CalculateHealth(demonName); demon.Damage = CalculateDamage(demonName); return(demon); }
static void Main(string[] args) { List <string> demons = Console.ReadLine().Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries) .Select(x => x.Trim()).ToList(); foreach (var item in demons.OrderBy(x => x)) { Demon demon = new Demon(); demon.Name = item; demon.Health = 0; demon.Damage = 0; int multiplication = 0; int division = 0; for (int i = 0; i < demon.Name.Length; i++) { double value = 0; if (demon.Name[i] >= 48 && demon.Name[i] <= 57) { string potentialNumber = string.Empty; while ((demon.Name[i] >= 48 && demon.Name[i] <= 57) || demon.Name[i] == 46) { potentialNumber += demon.Name[i]; i++; if (i >= demon.Name.Length) { break; } } value = double.Parse(potentialNumber); demon.Damage += value; i--; } else { switch (demon.Name[i]) { case '*': multiplication++; break; case '/': division++; break; case '+': string potentialNumber = string.Empty; i++; if (i >= demon.Name.Length) { break; } while ((demon.Name[i] >= 48 && demon.Name[i] <= 57) || demon.Name[i] == 46) { potentialNumber += demon.Name[i]; i++; if (i >= demon.Name.Length) { break; } } value = double.Parse(potentialNumber); demon.Damage += value; i--; break; case '-': string potentialNegativeNumber = string.Empty; i++; if (i >= demon.Name.Length) { break; } while ((demon.Name[i] >= 48 && demon.Name[i] <= 57) || demon.Name[i] == 46) { potentialNegativeNumber += demon.Name[i]; i++; if (i >= demon.Name.Length) { break; } } value = double.Parse(potentialNegativeNumber); demon.Damage -= value; i--; break; default: demon.Health += demon.Name[i]; break; } } } for (int i = 0; i < multiplication; i++) { demon.Damage *= 2; } for (int i = 0; i < division; i++) { demon.Damage /= 2; } Console.WriteLine($"{demon.Name} - {demon.Health} health, {demon.Damage:f2} damage"); } }