Advertisement
StoyanGrigorov

03. Nether Realms

Oct 23rd, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.77 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Text.RegularExpressions;
  7. //03. Nether Realms
  8. namespace NetherRealms
  9. {
  10.     class NetherRealms
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             Dictionary<string, Dictionary<long, decimal>> demons = new Dictionary<string, Dictionary<long, decimal>>();
  15.  
  16.             string input = Console.ReadLine();
  17.  
  18.             string patternInput = @"([^,\s][^\,]*[^,\s]*)";
  19.             Regex rgxInput = new Regex(patternInput);
  20.  
  21.             MatchCollection matches = rgxInput.Matches(input);
  22.  
  23.  
  24.             foreach (Match match in matches)
  25.             {
  26.                 string name = match.Groups[1].Value;
  27.                 long hp = GetHp(name);
  28.                 decimal dmg = GetDmg(name);
  29.  
  30.                 if (!demons.ContainsKey(name))
  31.                 {
  32.                     demons.Add(name, new Dictionary<long, decimal> { { hp, dmg } });
  33.                 }
  34.                 else
  35.                 {
  36.                     demons[name] = new Dictionary<long, decimal> { { hp, dmg } };
  37.                 }
  38.             }
  39.             foreach (var dem in demons.OrderBy(x => x.Key))
  40.             {
  41.                 Console.Write($"{dem.Key} - ");
  42.                 foreach (var stats in dem.Value)
  43.                 {
  44.                     Console.WriteLine($"{stats.Key} health, {stats.Value:f2} damage");
  45.                 }
  46.             }
  47.         }
  48.  
  49.         private static decimal GetDmg(string name)
  50.         {
  51.             string patternNumbers = @"([+-]?[0-9]+(?:\.[0-9]+)?)";
  52.             Regex rgxNumbers = new Regex(patternNumbers);
  53.  
  54.             MatchCollection matches = rgxNumbers.Matches(name);
  55.  
  56.             decimal dmg = 0;
  57.  
  58.             foreach (Match match in matches)
  59.             {
  60.                 decimal currentValue = decimal.Parse(match.Groups[1].Value.TrimStart('+'));
  61.  
  62.                 dmg += currentValue;
  63.             }
  64.  
  65.             for (int i = 0; i < name.Length; i++)
  66.             {
  67.                 if (name[i] == '*')
  68.                 {
  69.                     dmg *= 2;
  70.                 }
  71.                 else if( name[i] == '/')
  72.                 {
  73.                     dmg /= 2;
  74.                 }
  75.             }
  76.  
  77.             return dmg;
  78.         }
  79.  
  80.         private static long GetHp(string name)
  81.         {
  82.             string patternChars = @"([^0-9\+\-\*\/\.])";
  83.             Regex rgxChars = new Regex(patternChars);
  84.  
  85.             MatchCollection filteredName = rgxChars.Matches(name);
  86.             long sum = 0;
  87.             foreach (Match chara in filteredName)
  88.             {
  89.                 char c = char.Parse(chara.Groups[1].Value);
  90.                 sum += c;
  91.             }
  92.  
  93.  
  94.             return sum;
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement