Advertisement
Guest User

NetherRealmsRegex

a guest
Oct 24th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.80 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.Text.RegularExpressions;
  5.  
  6. public class NetherRealms
  7. {
  8.     public static void Main()
  9.     {
  10.         var demonNames = Console.ReadLine().Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);
  11.         var demonsList = new List<Demon>();
  12.  
  13.         foreach (var name in demonNames)
  14.         {
  15.             var currentDemon = new Demon(name);
  16.             currentDemon.Damage = CalcDamage(name);
  17.             currentDemon.Health = CalcHealth(name);
  18.             demonsList.Add(currentDemon);
  19.         }
  20.  
  21.         foreach (var demon in demonsList.OrderBy(d => d.Name))
  22.         {
  23.             Console.WriteLine($"{demon.Name} - {demon.Health} health, {demon.Damage:f2} damage");
  24.         }
  25.     }
  26.  
  27.     static double CalcDamage(string name)
  28.     {
  29.         Regex regexNumbers = new Regex(@"([\+|-]*\d+\.?\d*)");
  30.         Regex regexOperators = new Regex(@"([\/\*])");
  31.  
  32.         MatchCollection matchesNums = regexNumbers.Matches(name);
  33.         MatchCollection matchesOps = regexOperators.Matches(name);
  34.  
  35.         double damage = 0d;
  36.  
  37.         foreach (Match match in matchesNums)
  38.             damage += double.Parse(match.Groups[0].ToString());
  39.  
  40.         foreach (Match match in matchesOps)
  41.         {
  42.             switch (match.Groups[0].ToString())
  43.             {
  44.                 case "*": damage *= 2.0; break;
  45.                 case "/": damage /= 2.0; break;
  46.                 default:
  47.                     break;
  48.             }
  49.         }
  50.  
  51.         return damage;
  52.     }
  53.  
  54.     static int CalcHealth(string name)
  55.     {
  56.         Regex regex = new Regex(@"([^0-9\+\-\*\/\.])");
  57.         MatchCollection matches = regex.Matches(name);
  58.  
  59.         int health = 0;
  60.  
  61.         foreach (Match match in matches)
  62.         {
  63.             char currentChar = match.Groups[0].ToString()[0];
  64.             health += (int)currentChar;
  65.         }
  66.  
  67.         return health;
  68.     }
  69. }
  70.  
  71. public class Demon
  72. {
  73.     public string Name { get; set; }
  74.     public double Damage { get; set; }
  75.     public int Health { get; set; }
  76.  
  77.     public Demon(string name)
  78.     {
  79.         Name = name;
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement