TonyTroev

NetherRealms

Mar 1st, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.77 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Text.RegularExpressions;
  4.  
  5. namespace _02_03_NetherRealms
  6. {
  7.     class NetherRealms
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             // 100% from the very first try!
  12.             Regex healthPattern = new Regex(@"[^0-9-.,\+*\/]");
  13.             Regex damagePattern = new Regex(@"-?\d+(\.\d+)?");
  14.             Regex multiplyPattern = new Regex(@"[*\/]");
  15.             string[] input = Console.ReadLine()
  16.                 .Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries)
  17.                 .OrderBy(x => x)
  18.                 .ToArray();
  19.  
  20.             foreach (string word in input)
  21.             {
  22.                 MatchCollection letters = healthPattern.Matches(word);
  23.                 long health = 0L;
  24.                 foreach (Match match in letters)
  25.                 {
  26.                     health += (int)char.Parse(match.Value);
  27.                 }
  28.  
  29.                 MatchCollection numbers = damagePattern.Matches(word);
  30.                 double damage = 0.0;
  31.                 foreach (Match match in numbers)
  32.                 {
  33.                     damage += double.Parse(match.Value);
  34.                 }
  35.  
  36.                 MatchCollection multipliers = multiplyPattern.Matches(word);
  37.                 foreach (Match match in multipliers)
  38.                 {
  39.                     switch (match.Value)
  40.                     {
  41.                         case "*":
  42.                             damage *= 2;
  43.                             break;
  44.                         case "/":
  45.                             damage /= 2.0;
  46.                             break;
  47.                     }
  48.                 }
  49.  
  50.                 Console.WriteLine($"{word} - {health} health, {damage:F2} damage");
  51.             }
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment