bastien.monsarrat 6 gadi atpakaļ
vecāks
revīzija
a6c09a4049
5 mainītis faili ar 314 papildinājumiem un 4 dzēšanām
  1. 10 4
      Adv2018.sln
  2. 9 0
      D24.1/D24.1.csproj
  3. 264 0
      D24.1/Program.cs
  4. 8 0
      D24.1/Properties/launchSettings.json
  5. 23 0
      D24.1/input.txt

+ 10 - 4
Adv2018.sln

@@ -87,13 +87,15 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D20.1and2", "D20.1\D20.1and
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D21.1and2", "D21.1\D21.1and2.csproj", "{47F415F2-1D92-4A6F-89D3-6B3A958A0BDC}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D22.1", "D22.1\D22.1.csproj", "{E3050FCD-AA84-418E-9257-24E0C174AE82}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D22.1", "D22.1\D22.1.csproj", "{E3050FCD-AA84-418E-9257-24E0C174AE82}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D22.2", "D22.2\D22.2.csproj", "{1B878AC4-1487-4FC0-87BB-89D69C9A8428}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D22.2", "D22.2\D22.2.csproj", "{1B878AC4-1487-4FC0-87BB-89D69C9A8428}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D23.1", "D23.1\D23.1.csproj", "{107344C3-3FFF-45D1-B709-BF382C1F7E9A}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D23.1", "D23.1\D23.1.csproj", "{107344C3-3FFF-45D1-B709-BF382C1F7E9A}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D23.2", "D23.2\D23.2.csproj", "{8B805697-82CB-435D-8329-BC67ACA04B5D}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D23.2", "D23.2\D23.2.csproj", "{8B805697-82CB-435D-8329-BC67ACA04B5D}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D24.1", "D24.1\D24.1.csproj", "{11697B71-AD28-40ED-995B-8506C7A1B785}"
 EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -285,6 +287,10 @@ Global
 		{8B805697-82CB-435D-8329-BC67ACA04B5D}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{8B805697-82CB-435D-8329-BC67ACA04B5D}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{8B805697-82CB-435D-8329-BC67ACA04B5D}.Release|Any CPU.Build.0 = Release|Any CPU
+		{11697B71-AD28-40ED-995B-8506C7A1B785}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{11697B71-AD28-40ED-995B-8506C7A1B785}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{11697B71-AD28-40ED-995B-8506C7A1B785}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{11697B71-AD28-40ED-995B-8506C7A1B785}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

+ 9 - 0
D24.1/D24.1.csproj

@@ -0,0 +1,9 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <TargetFramework>netcoreapp2.1</TargetFramework>
+    <RootNamespace>D24._1</RootNamespace>
+  </PropertyGroup>
+
+</Project>

+ 264 - 0
D24.1/Program.cs

@@ -0,0 +1,264 @@
+//#define PRINT
+
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text.RegularExpressions;
+
+namespace D24._1
+{
+    public class Group
+    {
+        public int Id { get; set; }
+        public bool Healthy = true;
+        public string Team { get; set; }
+        public int Number { get; set; }
+        public int Initiative { get; set; }
+        public int HitPoints { get; set; }
+        public int Atk { get; set; }
+        public string AtkType { get; set; }
+        public HashSet<string> Weaknesses = new HashSet<string>();
+        public HashSet<string> Immunities = new HashSet<string>();
+        public int EffectivePower => Number * Atk;
+        public Group Target { get; set; }
+        public bool Targeted { get; set; }
+
+        private int _computeDamage(Group target)
+        {
+            int damage = 0;
+            if (target.Weaknesses.Contains(AtkType))
+                damage = 2 * EffectivePower;
+            else if (target.Immunities.Contains(AtkType) == false)
+                damage = EffectivePower;
+            return damage;
+        }
+
+        public int ComputeDamage(Group target)
+        {
+            int damage = _computeDamage(target);
+#if PRINT
+            Console.WriteLine($"{Team} group {Id} would deal defending group {target.Id} {damage} damage");
+#endif
+            return damage;
+        }
+
+        public void DealDamage(Group target)
+        {
+            var kill = target.ReceiveDamage(this);
+#if PRINT
+            Console.WriteLine($"{Team} group {Id} attacks defending group {target.Id}, killing {kill} units");
+#endif
+        }
+
+        int ReceiveDamage(Group source)
+        {
+            var damage = source._computeDamage(this);
+
+            int kill = damage / HitPoints;
+            kill = Math.Min(kill, Number);
+
+            Number -= kill;
+            if (Number == 0) Healthy = false;
+
+            return kill;
+        }
+    }
+
+    class Program
+    {
+        static void Main(string[] args)
+        {
+            if (args.Length < 1) return;
+            if (File.Exists(args[0]) == false) return;
+
+            List<Group> all;
+            Dictionary<string, int> teams;
+            ParseFile(args, out all, out teams);
+
+            while (teams.All(t => t.Value > 0))
+            {
+#if PRINT
+                PrintGroups(all, teams);
+                Console.WriteLine("");
+#endif
+
+                TargetSelectionPhase(all);
+
+#if PRINT
+                Console.WriteLine("");
+#endif
+
+                AttackPhase(all, teams);
+
+#if PRINT
+                Console.WriteLine("");
+#endif
+            }
+
+
+            int answer = all.Sum(a => a.Number);
+            Console.WriteLine($"The answer is : {answer}");
+        }
+
+        private static void PrintGroups(List<Group> all, Dictionary<string, int> teams)
+        {
+            foreach (var team in teams)
+            {
+                int printed = 0;
+                Console.WriteLine($"{team.Key}:");
+                foreach (var group in all.Where(a => a.Team == team.Key))
+                {
+                    if (group.Healthy == false) continue;
+                    Console.WriteLine($"Group {group.Id} contains {group.Number} units");
+                    printed++;
+                }
+                if (printed == 0) Console.WriteLine($"No groups remain.");
+            }
+        }
+
+        private static void ParseFile(string[] args, out List<Group> all, out Dictionary<string, int> teams)
+        {
+            var file = File.OpenText(args[0]);
+
+            var reg = new Regex(@"(?<n>\d+) units each with (?<hp>\d+) hit points( \(((?<att1>immune|weak) to (?<att1s>[\w\s,]+);? ?)((?<att2>immune|weak) to (?<att2s>[\w\s,]+))?\))? with an attack that does (?<atk>\d+) (?<atkt>\w+) damage at initiative (?<ini>\d+)");
+
+            string rgroup = string.Empty;
+
+            all = new List<Group>();
+            teams = new Dictionary<string, int>();
+            do
+            {
+                var line = file.ReadLine();
+                if (line == null) break;
+
+                if (line == string.Empty)
+                {
+                    rgroup = string.Empty;
+                    continue;
+                }
+
+                if (rgroup == string.Empty)
+                {
+                    rgroup = line.Substring(0, line.Length - 1);
+                    teams.Add(rgroup, 0);
+                    continue;
+                }
+
+                var res = reg.Match(line);
+
+                var g = new Group();
+
+                g.Team = rgroup;
+                teams[rgroup] = teams[rgroup] + 1;
+
+                g.Id = teams[rgroup];
+                g.Number = int.Parse(res.Groups["n"].Value);
+                g.HitPoints = int.Parse(res.Groups["hp"].Value);
+                g.Atk = int.Parse(res.Groups["atk"].Value);
+                g.AtkType = res.Groups["atkt"].Value;
+                g.Initiative = int.Parse(res.Groups["ini"].Value);
+
+                for (var i = 1; i <= 2; ++i)
+                {
+                    if (res.Groups[$"att{i}"].Success == false) break;
+
+                    var type = res.Groups[$"att{i}"].Value;
+                    foreach (var att in res.Groups[$"att{i}s"].Value.Split(", "))
+                    {
+                        if (type.Equals("weak")) g.Weaknesses.Add(att);
+                        else if (type.Equals("immune")) g.Immunities.Add(att);
+                    }
+                }
+
+                all.Add(g);
+            } while (true);
+
+            file.Close();
+        }
+
+        private static void AttackPhase(List<Group> all, Dictionary<string, int> teams)
+        {
+            var attackingOrder = all
+                                .OrderByDescending(s => s.Initiative);
+
+            foreach (var group in attackingOrder)
+            {
+                if (group.Target == null) continue;
+                if (group.Healthy == false) continue;
+
+                AttackTarget(teams, group);
+            }
+        }
+
+        private static void AttackTarget(Dictionary<string, int> teams, Group group)
+        {
+            var target = group.Target;
+            group.Target = null;
+            target.Targeted = false;
+
+            group.DealDamage(target);
+            if (target.Healthy == false)
+            {
+                if (target.Target != null)
+                {
+                    target.Target.Targeted = false;
+                    target.Target = null;
+                }
+                teams[target.Team] = teams[target.Team] - 1;
+            }
+        }
+
+        private static void TargetSelectionPhase(List<Group> all)
+        {
+            var targetingOrder = all
+                                .OrderByDescending(a => a.EffectivePower)
+                                .ThenBy(a => a.Initiative);
+
+            foreach (var group in targetingOrder)
+            {
+                if (group.Healthy == false) continue;
+                Group max = PickTarget(all, group);
+
+                if (max == null) continue;
+
+                group.Target = max;
+                max.Targeted = true;
+            }
+        }
+
+        private static Group PickTarget(List<Group> all, Group group)
+        {
+            Group max = null;
+            int maxDamage = 0;
+            foreach (var target in all)
+            {
+                if (target.Team == group.Team) continue;
+                if (target.Targeted == true) continue;
+                if (target.Healthy == false) continue;
+
+                int damage = group.ComputeDamage(target);
+
+                if (damage == 0) continue;
+
+                if (damage > maxDamage)
+                {
+                    maxDamage = damage;
+                    max = target;
+                }
+                else if (damage == maxDamage)
+                {
+                    if (target.EffectivePower > max.EffectivePower)
+                        max = target;
+                    else if (target.EffectivePower == max.EffectivePower)
+                    {
+                        if (target.Initiative > max.Initiative)
+                            max = target;
+                    }
+                }
+            }
+
+            return max;
+        }
+    }
+}

+ 8 - 0
D24.1/Properties/launchSettings.json

@@ -0,0 +1,8 @@
+{
+  "profiles": {
+    "D24.1": {
+      "commandName": "Project",
+      "commandLineArgs": "\"..\\..\\..\\..\\D24.1\\input.txt\" "
+    }
+  }
+}

+ 23 - 0
D24.1/input.txt

@@ -0,0 +1,23 @@
+Immune System:
+4592 units each with 2061 hit points (immune to slashing, radiation; weak to cold) with an attack that does 4 fire damage at initiative 9
+1383 units each with 3687 hit points with an attack that does 26 radiation damage at initiative 15
+2736 units each with 6429 hit points (immune to slashing) with an attack that does 20 slashing damage at initiative 2
+777 units each with 3708 hit points (immune to radiation, cold; weak to slashing, fire) with an attack that does 39 cold damage at initiative 4
+6761 units each with 2792 hit points (immune to bludgeoning, fire, cold, slashing) with an attack that does 3 radiation damage at initiative 17
+6028 units each with 5537 hit points (immune to slashing) with an attack that does 7 radiation damage at initiative 6
+2412 units each with 2787 hit points with an attack that does 9 bludgeoning damage at initiative 20
+6042 units each with 7747 hit points (immune to radiation) with an attack that does 12 slashing damage at initiative 12
+1734 units each with 7697 hit points (weak to radiation, cold) with an attack that does 38 cold damage at initiative 10
+4391 units each with 3250 hit points with an attack that does 7 cold damage at initiative 19
+
+Infection:
+820 units each with 46229 hit points (immune to cold, bludgeoning) with an attack that does 106 slashing damage at initiative 18
+723 units each with 30757 hit points (weak to bludgeoning) with an attack that does 80 fire damage at initiative 3
+2907 units each with 51667 hit points (weak to slashing; immune to bludgeoning) with an attack that does 32 fire damage at initiative 1
+2755 units each with 49292 hit points (weak to bludgeoning) with an attack that does 34 fire damage at initiative 5
+5824 units each with 24708 hit points (immune to bludgeoning, cold, radiation, slashing) with an attack that does 7 bludgeoning damage at initiative 11
+7501 units each with 6943 hit points (weak to cold; immune to slashing) with an attack that does 1 radiation damage at initiative 8
+573 units each with 10367 hit points (weak to slashing, cold) with an attack that does 30 radiation damage at initiative 16
+84 units each with 31020 hit points (weak to cold) with an attack that does 639 slashing damage at initiative 14
+2063 units each with 31223 hit points (immune to bludgeoning; weak to radiation) with an attack that does 25 cold damage at initiative 13
+214 units each with 31088 hit points (weak to fire) with an attack that does 271 slashing damage at initiative 7