static void Main() { var input = Regex.Split(Console.ReadLine(), @",\s*") .Select(x => x.Trim()) .Where(x => !x.Contains(" ") & !x.Contains(",")) .ToArray(); var deamons = new Dictionary <string, Deamon>(); foreach (var match in input) { decimal health = Regex.Matches(match, @"[^\d\-*\/\.]") .Cast <Match>() .Select(x => char.Parse(x.Value)) .Select(x => (int)x) .Sum(); var damage = Regex.Matches(match, @"-?\d+(?:\.\d+)?") .Cast <Match>() .Select(x => decimal.Parse(x.Value)) .Sum(); var specialSymbols = match .Where(x => x == '*' || x == '/') .ToArray(); damage = DivideOrMultiply(damage, specialSymbols); var healthAndDamage = Deamon.Read(health, damage); if (!deamons.ContainsKey(match)) { deamons[match] = new Deamon(); } deamons[match] = healthAndDamage; } deamons .OrderBy(x => x.Key) .ToList() .ForEach(x => Console.WriteLine($"{x.Key} - {x.Value.Health} health, {x.Value.Damage:f2} damage")); }
static void Main(string[] args) { string[] deamons = Console.ReadLine() .Split(new[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries); SortedDictionary <string, Deamon> deamonData = new SortedDictionary <string, Deamon>(); foreach (var deamon in deamons) { var healthSymbols = deamon .Where(s => !char.IsDigit(s) && s != '+' && s != '-' && s != '*' && s != '/' && s != '.'); int health = 0; foreach (var healthSymbol in healthSymbols) { health += healthSymbol; } Regex regex = new Regex(@"-?\d+\.?\d*"); MatchCollection matches = regex.Matches(deamon); double damage = 0.0; foreach (Match match in matches) { double currentNumber = double.Parse(match.Value); damage += currentNumber; } char[] modifiers = deamon .Where(s => s == '*' || s == '/').ToArray(); foreach (var modifier in modifiers) { if (modifier == '*') { damage *= 2; } else if (modifier == '/') { damage /= 2; } } deamonData.Add(deamon, new Deamon { Name = deamon, Health = health, Damage = damage }); } foreach (var demonEntry in deamonData) { Deamon demon = demonEntry.Value; Console.WriteLine("{0} - {1} health, {2:F2} damage", demon.Name, demon.Health, demon.Damage); } }