Advertisement
valkata

netherRealms

Aug 16th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7.  
  8. namespace _03_netherRealms
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             string[] names = Console.ReadLine()
  15.                 .Split(new string[] { ",", " " }, StringSplitOptions.RemoveEmptyEntries)
  16.                 .ToArray();
  17.             names = CheckNames(names);
  18.             Dictionary<string, Dictionary<double, double>> fighters = new Dictionary<string, Dictionary<double, double>>();
  19.             string numberPatter = @"([-+ ]?[\d]+[\.]?[\d]+)|\d";
  20.            
  21.             foreach (var name in names)
  22.             {
  23.                 Match currNameMAtch = Regex.Match(name, @"[^\+\-\*\/0-9\.\s\,]");
  24.                 if (currNameMAtch.Success)
  25.                 {
  26.                     if (!fighters.ContainsKey(name))
  27.                     {
  28.                         fighters.Add(name, new Dictionary<double, double>());
  29.                     }
  30.  
  31.                     long health = FighterHealth(name);
  32.                     double power = FighterPower(name, numberPatter);
  33.  
  34.                     fighters[name][health] = power;
  35.                 }
  36.                
  37.             }
  38.             var orderedFighters = fighters.OrderBy(f => f.Key);
  39.             foreach (var fighter in orderedFighters)
  40.             {
  41.                 Console.Write("{0} - ",fighter.Key);
  42.  
  43.                 var stats = fighter.Value;
  44.                 foreach (var stat in stats)
  45.                 {
  46.                     Console.WriteLine($"{stat.Key} health, {stat.Value:F2} damage");
  47.                 }
  48.             }
  49.         }
  50.  
  51.         private static string[] CheckNames(string[] names)
  52.         {
  53.             List<string> newArray = new List<string>();
  54.             foreach (var name in names)
  55.             {
  56.                 if (!name.Contains(" "))
  57.                 {
  58.                     if (!name.Contains(","))
  59.                     {
  60.                         newArray.Add(name);
  61.                     }
  62.                 }
  63.             }
  64.  
  65.             return newArray.ToArray();
  66.         }
  67.  
  68.         private static double FighterPower(string name, string numberPatter)
  69.         {
  70.             double power = Regex.Matches(name, numberPatter)
  71.                 .Cast<Match>()
  72.                 .Select(n => n.Value)
  73.                 .Select(double.Parse)
  74.                 .ToArray()
  75.                 .Sum();
  76.                        
  77.  
  78.             string pattern = @"[*\/]?";
  79.             string[] maths = Regex.Matches(name, pattern)
  80.                 .Cast<Match>()
  81.                 .Select(m => m.Value)
  82.                 .ToArray();
  83.  
  84.             foreach (var math in maths)
  85.             {
  86.                 if(math == "*")
  87.                 {
  88.                     power *= 2;
  89.                 }
  90.                 else if(math == "/")
  91.                 {
  92.                     power /= 2;
  93.                 }
  94.             }
  95.  
  96.             return power;
  97.         }
  98.  
  99.         private static long FighterHealth(string name)
  100.         {
  101.             string[] currMatch = Regex.Matches(name, @"[^\+\-\*\/0-9\.\s\,]")
  102.                 .Cast<Match>()
  103.                 .Select(m => m.Value)
  104.                 .ToArray();
  105.  
  106.             long health = 0;
  107.  
  108.             for (int i = 0; i < currMatch.Length; i++)
  109.             {
  110.                 health += char.Parse(currMatch[i]);
  111.             }
  112.  
  113.             return health;
  114.         }
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement