Skip to content

Instantly share code, notes, and snippets.

@Tangrila-BG
Last active November 6, 2016 18:18
Show Gist options
  • Save Tangrila-BG/1bfe5347d9fa71be49c7fca4e81cccb5 to your computer and use it in GitHub Desktop.
Save Tangrila-BG/1bfe5347d9fa71be49c7fca4e81cccb5 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace random
{
class Program
{
static void Main(string[] args)
{
// SortedList <string DEMON NAME, Dictionary<int HEALTH, double DAMAGE> >
SortedList<string, Dictionary<string, double?>> demonBook =
new SortedList<string, Dictionary<string, double?>>();
string dragonPattern = @",\s*"; // used in dragon extraction
Regex extractNumbers = new Regex(@"(\-?\+?\d+\.\d+|\-?\+?\d+|\d+)"); // used in number extraction
Regex extractMultAndDiv = new Regex(@"[*\/]"); // used in symbol extraction
string[] input = Regex.Split(Console.ReadLine(), dragonPattern);
foreach (string dragon in input)
{
int health = dragon.Where(Char.IsLetter) // extracts the letters from input
.Select(c => c - '0' + '0') // converts each letter to its numeric value
.Sum(); // sums the values
// extracts all numbers (with + or - sign and double or int)
MatchCollection damageMatches = extractNumbers.Matches(dragon);
double damage = 0d;
foreach (Match match in damageMatches)
{
damage += double.Parse(match.Value);
}
// extracts all * and / symbols
MatchCollection symbolMatches = extractMultAndDiv.Matches(dragon);
foreach (Match match in symbolMatches)
{
if (match.Value == "*")
damage *= 2;
else
damage /= 2;
}
demonBook.Add(dragon, new Dictionary<string, double?>());
demonBook[dragon].Add("health", health);
demonBook[dragon].Add("damage", damage);
}
//
// Output
//
foreach (var dragonValuePair in demonBook)
{
Console.Write(dragonValuePair.Key + " - ");
foreach (var typeValuePair in dragonValuePair.Value) // iterates the nested dictionary
{
if (typeValuePair.Key == "damage")
Console.Write($"{typeValuePair.Value:F2} {typeValuePair.Key}");
else
Console.Write($"{typeValuePair.Value} {typeValuePair.Key}, ");
}
Console.WriteLine();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment