bullit3189

Nether Realms-String and Regex

Mar 29th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.Text.RegularExpressions;
  5. using System.Text;
  6.  
  7. public class Program
  8. {
  9. public static void Main()
  10. {
  11. Dictionary<string,Dictionary<int,double>> nameHpDmg = new Dictionary<string,Dictionary<int,double>>();
  12.  
  13. string[] names = Console.ReadLine().Split(new[] {',',' '},StringSplitOptions.RemoveEmptyEntries);
  14.  
  15. foreach (string name in names)
  16. {
  17. int health =0;
  18.  
  19. for (int i=0; i<name.Length; i++)
  20. {
  21. char curr = name[i];
  22.  
  23. if (char.IsDigit(curr) || curr=='+' || curr=='-' || curr == '*' || curr=='/' || curr=='.')
  24. {
  25. continue;
  26. }
  27. else
  28. {
  29. health += (int) curr;
  30. }
  31. }
  32.  
  33. string numsPattern = @"(\+|-)?(\d+(\.\d+)?)";
  34.  
  35. double dmg = 0;
  36.  
  37. if (Regex.IsMatch(name,numsPattern))
  38. {
  39. MatchCollection matches = Regex.Matches(name,numsPattern);
  40.  
  41. foreach (Match match in matches)
  42. {
  43. double currMatch = double.Parse(match.Value);
  44. dmg+=currMatch;
  45. }
  46. }
  47.  
  48. foreach (char symbol in name)
  49. {
  50. if (symbol == '*')
  51. {
  52. dmg*=2;
  53. }
  54. else if (symbol == '/')
  55. {
  56. dmg/=2.0;
  57. }
  58. }
  59.  
  60. nameHpDmg.Add(name,new Dictionary<int,double>());
  61. nameHpDmg[name].Add(health,dmg);
  62. }
  63.  
  64. foreach (var kvp in nameHpDmg.OrderBy(x=>x.Key))
  65. {
  66. string name = kvp.Key;
  67.  
  68. foreach (var kvpValue in kvp.Value)
  69. {
  70. Console.WriteLine("{0} - {1} health, {2:f2} damage",name,kvpValue.Key,kvpValue.Value);
  71. }
  72. }
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment