public int DamageGivenTo(GroupTwo target) { if (target.immuneSystem == immuneSystem) { return(0); } else if (target.immuneTo.Contains(attackType)) { return(0); } else if (target.weakTo.Contains(attackType)) { return(effectivePower * 2); } else { return(effectivePower); } }
public List <GroupTwo> Parse(string input) { var lines = input.Split(new string[] { "\r\n" }, StringSplitOptions.None); var immuneSystem = false; var res = new List <GroupTwo>(); foreach (var line in lines) { if (line == "Immune System:") { immuneSystem = true; } else if (line == "Infection:") { immuneSystem = false; } else if (line != "") { //643 units each with 9928 hit points (immune to fire; weak to slashing, bludgeoning) with an attack that does 149 fire damage at initiative 14 var rx = @"(\d+) units each with (\d+) hit points(.*)with an attack that does (\d+)(.*)damage at initiative (\d+)"; var m = Regex.Match(line, rx); if (m.Success) { GroupTwo g = new GroupTwo(); g.immuneSystem = immuneSystem; g.units = int.Parse(m.Groups[1].Value); g.hp = int.Parse(m.Groups[2].Value); g.damage = int.Parse(m.Groups[4].Value); g.attackType = m.Groups[5].Value.Trim(); g.initiative = int.Parse(m.Groups[6].Value); var st = m.Groups[3].Value.Trim(); if (st != "") { st = st.Substring(1, st.Length - 2); foreach (var part in st.Split(';')) { var k = part.Split(new string[] { " to " }, StringSplitOptions.None); var set = new HashSet <string>(k[1].Split(new string[] { ", " }, StringSplitOptions.None)); var w = k[0].Trim(); if (w == "immune") { g.immuneTo = set; } else if (w == "weak") { g.weakTo = set; } else { throw new Exception(); } } } res.Add(g); } else { throw new Exception(); } } } return(res); }