Advertisement
Rayk

Untitled

Nov 1st, 2017
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace _03_Nether_Realms
  7. {
  8. public class NetherRealms
  9. {
  10. public static void Main()
  11. {
  12. var inputDemons = Console.ReadLine().Split(',').Select(d => d.Trim()).ToArray();
  13.  
  14. var damageRegex = new Regex(@"(?<damage>[\+\-]?\d+\.?\d*)");
  15. var healthRegex = new Regex(@"(?<health>[^\d\+\-\*\.\/])");
  16.  
  17. List<Demon> demons = new List<Demon>();
  18. foreach (var inputDemon in inputDemons)
  19. {
  20. decimal damage = damageRegex.Matches(inputDemon)
  21. .Cast<Match>()
  22. .Select(d => d.Value)
  23. .Select(decimal.Parse)
  24. .Sum();
  25.  
  26. var health = healthRegex.Matches(inputDemon)
  27. .Cast<Match>()
  28. .Select(d => char.Parse(d.Value))
  29. .Select(ch => (int)ch)
  30. .Sum();
  31.  
  32. var multiplyCnt = inputDemon.ToCharArray().Count(ch => ch == '*');
  33. var divideCnt = inputDemon.ToCharArray().Count(ch => ch == '/');
  34.  
  35. damage *= (decimal)Math.Pow(2, multiplyCnt);
  36. damage /= (decimal)Math.Pow(2, divideCnt);
  37.  
  38. var demon = new Demon(inputDemon, damage, health);
  39. demons.Add(demon);
  40. }
  41.  
  42. foreach (var demon in demons.OrderBy(d => d.DemonName))
  43. {
  44. Console.WriteLine($"{demon.DemonName} - {demon.Health} health, {demon.Damage:f2} damage");
  45. }
  46. }
  47. }
  48.  
  49. public class Demon
  50. {
  51. public Demon(string demonName, decimal damage, decimal health)
  52. {
  53. DemonName = demonName;
  54. Damage = damage;
  55. Health = health;
  56. }
  57.  
  58. public string DemonName { get; set; }
  59.  
  60. public decimal Damage { get; set; }
  61.  
  62. public decimal Health { get; set; }
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement