MiroslavKisov

NetherRealms

Dec 9th, 2017
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.57 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace _03.NetherRealms
  7. {
  8.     public class StartUp
  9.     {
  10.         public static void Main()
  11.         {
  12.             List<string> result = new List<string>();
  13.             string healthPattern = @"[^0-9\.\-\+\*\/]";
  14.             string damagePattern = @"((\+|\-)?(\d+)(\.\d+)?)";
  15.             var demons = Console.ReadLine()
  16.                 .Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries)
  17.                 .ToList();
  18.             Regex regexHealth = new Regex(healthPattern);
  19.             Regex regexDamage = new Regex(damagePattern);
  20.             for (int i = 0; i < demons.Count; i++)
  21.             {
  22.                 int totalHealth = 0;
  23.                 double totalDamage = 0.0d;
  24.                 MatchCollection healthMatches = Regex.Matches(demons[i], healthPattern);
  25.                 MatchCollection damageMatches = Regex.Matches(demons[i], damagePattern);
  26.                 int mult = demons[i].Where(c => c == '*').Count();
  27.                 int dev = demons[i].Where(c => c == '/').Count();
  28.                 totalHealth = GetHealth(totalHealth, healthMatches);
  29.                 totalDamage = GetDamage(totalDamage, damageMatches, mult, dev);
  30.                 result.Add(demons[i] + " " + "-" + " " + totalHealth + " health, " + totalDamage.ToString(string.Format("F2")) + " damage");
  31.             }
  32.             foreach (var demon in result.OrderBy(x => x))
  33.             {
  34.                 Console.WriteLine(demon);
  35.             }
  36.         }
  37.  
  38.         private static int GetHealth(int totalHealth, MatchCollection healthMatches)
  39.         {
  40.             foreach (Match match in healthMatches)
  41.             {
  42.                 string m = match.Value;
  43.                 totalHealth += Convert.ToChar(m);
  44.             }
  45.  
  46.             return totalHealth;
  47.         }
  48.  
  49.         private static double GetDamage(double totalDamage, MatchCollection damageMatches, int mult, int dev)
  50.         {
  51.             foreach (Match match in damageMatches)
  52.             {
  53.                 double damage = double.Parse(match.Value);
  54.                 totalDamage += damage;
  55.             }
  56.             if (mult > 0)
  57.             {
  58.                 for (int j = 0; j < mult; j++)
  59.                 {
  60.                     totalDamage *= 2;
  61.                 }
  62.             }
  63.             if (dev > 0)
  64.             {
  65.                 for (int j = 0; j < dev; j++)
  66.                 {
  67.                     totalDamage /= 2;
  68.                 }
  69.             }
  70.  
  71.             return totalDamage;
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment