kolioi

Nether Realms w/o regex

Jul 8th, 2017
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.28 KB | None | 0 0
  1. // Nether Realms w/o using regex, just parsing the string
  2. // Programming Fundamentals Exam - Part 2 - 23 October 2016
  3. // https://judge.softuni.bg/Contests/Practice/Index/349#0
  4. // 100 / 100
  5. // Memory: 9.30 MB
  6. // Time: 0.015 s
  7.  
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12.  
  13. namespace NetherRealms
  14. {
  15.     class Program
  16.     {
  17.         static void Main()
  18.         {
  19.             string[] separators = { " ", "," };
  20.             List<string> demons = Console.ReadLine()
  21.                                   .Split(separators, StringSplitOptions.RemoveEmptyEntries)
  22.                                   .ToList();
  23.  
  24.             demons.Sort();
  25.  
  26.             foreach (string demon in demons)
  27.                 ParseDemonInfo(demon);
  28.         }
  29.  
  30.         private static void ParseDemonInfo(string s)
  31.         {
  32.             int health = 0,
  33.                 multCount = 0,
  34.                 divCount = 0;
  35.  
  36.             StringBuilder sb = new StringBuilder();
  37.  
  38.             foreach (char ch in s)
  39.                 switch (ch)
  40.                 {
  41.                     case '*':
  42.                         multCount++;
  43.                         sb.Append(' ');
  44.                         break;
  45.                     case '/':
  46.                         divCount++;
  47.                         sb.Append(' ');
  48.                         break;
  49.                     case '+':
  50.                     case '-':
  51.                         sb.Append(' ');
  52.                         sb.Append(ch);
  53.                         break;
  54.                     default:
  55.                         if (char.IsDigit(ch) || ch.Equals('.'))
  56.                             sb.Append(ch);
  57.                         else
  58.                         {
  59.                             health += ch;
  60.                             sb.Append(' ');
  61.                         }
  62.                         break;
  63.                 }
  64.  
  65.             double damage = sb.ToString()
  66.                             .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  67.                             .Select(double.Parse)
  68.                             .ToArray()
  69.                             .Sum();
  70.  
  71.             damage *= Math.Pow(2, multCount - divCount);
  72.  
  73.             Console.WriteLine($"{s} - {health} health, {damage:F2} damage");
  74.         }
  75.  
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment