Пример #1
0
        public static Demon Parse(string demonStr)
        {
            var name        = demonStr;
            var damageRegex = new Regex(@"-?\d+(?:\.\d+)?"); //regex за числата
            var healthRegex = new Regex(@"[^\d+\-*\/\.]");   //regex за буквите

            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 *= (decimal)Math.Pow(2, multiplyCount);
            damage /= (decimal)Math.Pow(2, divideCount);

            var demon = new Demon()
            {
                Name   = name,
                Health = health,
                Damage = damage
            };

            return(demon);
        }
Пример #2
0
        public static void Main()
        {
            var numbersRegex = new Regex(@"[+-]?[\d]+\.?[\d]*");
            var symbolsRegex = new Regex(@"[*/]");
            var lettersRegex = new Regex(@"[^0-9+\-*/.]");
            var demons       = new SortedDictionary <string, Demon>();

            foreach (var demon in Console.ReadLine()
                     .Split(new[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries))
            {
                int health = lettersRegex.Matches(demon)
                             .Cast <Match>()
                             .Aggregate(0, (current, match) => current + char.Parse(match.Value));
                double damage = numbersRegex.Matches(demon)
                                .Cast <Match>()
                                .Sum(match => double.Parse(match.Value));
                damage = symbolsRegex.Matches(demon)
                         .Cast <Match>()
                         .Aggregate(damage, (current, match) => current * (match.Value == "*" ? 2 : 0.5));
                demons[demon] = new Demon(health, damage);
            }
            foreach (var demon in demons)
            {
                Console.WriteLine($"{demon.Key} - {demon.Value.Health} health, {demon.Value.Damage:f2} damage");
            }
        }
Пример #3
0
        static void Main(string[] args)
        {
            var healthPattern         = @"(?![0-9]|([.\-+\/*]))(?<characters>.)";
            var damagePattern         = @"[-+]?[0-9]+\.?[0-9]*";
            var multiplicationPattern = @"[\*\/]";

            var demonNames = Regex.Split(Console.ReadLine(), @"[,\s]+");

            Regex healthRegex = new Regex(healthPattern);
            Regex damageRegex = new Regex(damagePattern);

            var demons = new List <Demon>();

            foreach (var name in demonNames)
            {
                Demon demon = new Demon
                {
                    Name   = name,
                    Health = GetHealth(healthRegex, name),
                    Damage = GetDamage(damageRegex, name, multiplicationPattern)
                };

                demons.Add(demon);
            }

            PrintResults(demons);
        }
Пример #4
0
        static void Main(string[] args)
        {
            string[] demonsList = Console.ReadLine()
                                  .Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries)
                                  .ToArray();

            string pattern = @"(?<number>(([+-])*)(([0-9]+([.])*([0-9]+)*)))";
            SortedDictionary <string, Demon> participants = new SortedDictionary <string, Demon>();

            foreach (var demon in demonsList)
            {
                long            health  = 0;
                decimal         dmg     = 0.0m;
                MatchCollection match   = Regex.Matches(demon, pattern);
                char[]          currDem = demon.ToCharArray();
                for (int i = 0; i < currDem.Length; i++)
                {
                    if (!char.IsDigit(currDem[i]) &&
                        (currDem[i] != '.' &&
                         currDem[i] != '+' &&
                         currDem[i] != '-' &&
                         currDem[i] != '*' &&
                         currDem[i] != '/'))
                    {
                        health += currDem[i];
                    }
                }
                string action = string.Empty;
                foreach (Match m in match)
                {
                    string currMatch = m.ToString();

                    decimal currNum = decimal.Parse(m.Groups["number"].Value.ToString());
                    dmg += currNum;
                }
                for (int i = 0; i < demon.Length; i++)
                {
                    if (demon[i] == '*')
                    {
                        dmg *= 2;
                    }
                    else if (demon[i] == '/')
                    {
                        dmg /= 2;
                    }
                }
                if (!participants.ContainsKey(demon))
                {
                    participants[demon]        = new Demon();
                    participants[demon].Health = health;
                    participants[demon].Damage = dmg;
                }
            }
            foreach (var currDemon in participants)
            {
                Console.WriteLine($"{currDemon.Key} - {currDemon.Value.Health} health, {currDemon.Value.Damage:F2} damage");
            }
        }
Пример #5
0
 static void Main(string[] args)
 {
     string[] demons = Console.ReadLine().Split(',').Select(x => x.Trim()).OrderBy(x => x).ToArray();
     for (int i = 0; i < demons.Length; i++)
     {
         Demon demon = new Demon(demons[i]);
         Console.WriteLine("{0} - {1} health, {2:f2} damage", demon.Name, demon.Healt, demon.Dmg);
     }
 }
Пример #6
0
        public static void Main()
        {
            var listOfDemons = new List <Demon>();

            var pattern = @"\s*,\s*";

            var demonsName = Regex.Split(Console.ReadLine(), pattern)
                             .Select(d => d.Trim())
                             .Where(d => d.Length > 0)
                             .ToList();

            for (int i = 0; i < demonsName.Count; i++)
            {
                var demon = new Demon()
                {
                    Name   = demonsName[i],
                    Health = 0,
                    Damage = 0.0
                };

                if (demonsName[i].ToCharArray().Any(char.IsDigit))
                {
                    demon.Damage = Regex.Matches(demonsName[i], @"(-?\d+(?:\.\d+)?)")
                                   .Cast <Match>()
                                   .Select(n => n.Value.Trim())
                                   .Select(double.Parse)
                                   .Sum();
                }

                demon.Health = Regex.Matches(demonsName[i], @"[^\d+\-\/\.\*]")
                               .Cast <Match>()
                               .Select(n => n.Value.Trim())
                               .Select(char.Parse)
                               .Sum(x => x);


                for (int j = 0; j < demonsName[i].Length; j++)
                {
                    if (demonsName[i][j].Equals('*'))
                    {
                        demon.Damage *= 2.0;
                    }
                    else if (demonsName[i][j].Equals('/'))
                    {
                        demon.Damage /= 2.0;
                    }
                }

                listOfDemons.Add(demon);
            }

            foreach (var demon in listOfDemons.OrderBy(x => x.Name))
            {
                Console.WriteLine($"{demon.Name} - {demon.Health} health, " +
                                  $"{demon.Damage:f2} damage");
            }
        }
Пример #7
0
        public static Demon Parse(string demonName)
        {
            var demon = new Demon();

            demon.Name   = demonName;
            demon.Health = CalculateHealth(demonName);
            demon.Damage = CalculateDamage(demonName);

            return(demon);
        }
Пример #8
0
        static void Main(string[] args)
        {
            var input = Regex.Split(Console.ReadLine(), @"\s*,\s*");

            var list = new List <Demon>();

            foreach (var demon in input)
            {
                var health = Regex.Matches(demon, @"[^\d\+\-\*\/\.]")
                             .Cast <Match>()
                             .Select(a => (int)char.Parse(a.Value))
                             .Sum();

                var damage = Regex.Matches(demon, @"-*\d+(\.\d+)*")
                             .Cast <Match>()
                             .Select(a => decimal.Parse(a.Value))
                             .Sum();

                var damageSigns = demon
                                  .Where(a => a == '*' || a == '/')
                                  .ToArray();

                foreach (var sign in damageSigns)
                {
                    if (sign == '*')
                    {
                        damage *= 2;
                    }
                    else if (sign == '/')
                    {
                        damage /= 2;
                    }
                }

                var classDemon = new Demon
                {
                    Name   = demon,
                    Health = health,
                    Damage = damage
                };

                list.Add(classDemon);
            }

            foreach (var demon in list.OrderBy(a => a.Name))
            {
                Console.WriteLine($"{demon.Name} - {demon.Health:F0} health, {demon.Damage:F2} damage");
            }
        }
Пример #9
0
        public static void Main()
        {
            var demon = new List <Demon>();

            var input = Console.ReadLine().Split(new string[] { ",", " " }, StringSplitOptions.RemoveEmptyEntries);

            foreach (var data in input)
            {
                var demonName   = GetDemonName(data);
                var demonDamage = GetDemonDamage(data);
                var demonHealth = GetDemonHealth(demonName);

                var demonInfo = new Demon(data, demonHealth, demonDamage);
                demon.Add(demonInfo);
            }

            PrintResult(demon);
        }