bastien.monsarrat 6 年之前
父节点
当前提交
a666b0c63d
共有 5 个文件被更改,包括 119 次插入17 次删除
  1. 7 1
      Adv2018.sln
  2. 25 16
      D24.1/Program.cs
  3. 13 0
      D24.2/D24.2.csproj
  4. 66 0
      D24.2/Program.cs
  5. 8 0
      D24.2/Properties/launchSettings.json

+ 7 - 1
Adv2018.sln

@@ -95,7 +95,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D23.1", "D23.1\D23.1.csproj
 EndProject
 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}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D24.1", "D24.1\D24.1.csproj", "{11697B71-AD28-40ED-995B-8506C7A1B785}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D24.2", "D24.2\D24.2.csproj", "{9DD991C4-F621-42A0-AA99-714718FD1119}"
 EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -291,6 +293,10 @@ Global
 		{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
+		{9DD991C4-F621-42A0-AA99-714718FD1119}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{9DD991C4-F621-42A0-AA99-714718FD1119}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{9DD991C4-F621-42A0-AA99-714718FD1119}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{9DD991C4-F621-42A0-AA99-714718FD1119}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

+ 25 - 16
D24.1/Program.cs

@@ -14,23 +14,25 @@ namespace D24._1
         public bool Healthy = true;
         public string Team { get; set; }
         public int Number { get; set; }
+        public int OrigNumber { get; set; }
         public int Initiative { get; set; }
         public int HitPoints { get; set; }
         public int Atk { get; set; }
+        public int AtkBonus { 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 int EffectivePower => Number * (Atk + AtkBonus);
         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;
+            if (target.Immunities.Contains(AtkType)) return 0;
+
+            int damage = EffectivePower;
+            if (target.Weaknesses.Contains(AtkType)) damage *= 2;
+
             return damage;
         }
 
@@ -43,12 +45,13 @@ namespace D24._1
             return damage;
         }
 
-        public void DealDamage(Group target)
+        public int DealDamage(Group target)
         {
             var kill = target.ReceiveDamage(this);
 #if PRINT
             Console.WriteLine($"{Team} group {Id} attacks defending group {target.Id}, killing {kill} units");
 #endif
+            return kill;
         }
 
         int ReceiveDamage(Group source)
@@ -59,13 +62,13 @@ namespace D24._1
             kill = Math.Min(kill, Number);
 
             Number -= kill;
-            if (Number == 0) Healthy = false;
+            if (Number <= 0) Healthy = false;
 
             return kill;
         }
     }
 
-    class Program
+    public class Program
     {
         static void Main(string[] args)
         {
@@ -117,7 +120,7 @@ namespace D24._1
             }
         }
 
-        private static void ParseFile(string[] args, out List<Group> all, out Dictionary<string, int> teams)
+        public static void ParseFile(string[] args, out List<Group> all, out Dictionary<string, int> teams)
         {
             var file = File.OpenText(args[0]);
 
@@ -154,6 +157,7 @@ namespace D24._1
 
                 g.Id = teams[rgroup];
                 g.Number = int.Parse(res.Groups["n"].Value);
+                g.OrigNumber = g.Number;
                 g.HitPoints = int.Parse(res.Groups["hp"].Value);
                 g.Atk = int.Parse(res.Groups["atk"].Value);
                 g.AtkType = res.Groups["atkt"].Value;
@@ -177,27 +181,31 @@ namespace D24._1
             file.Close();
         }
 
-        private static void AttackPhase(List<Group> all, Dictionary<string, int> teams)
+        public static bool AttackPhase(List<Group> all, Dictionary<string, int> teams)
         {
             var attackingOrder = all
                                 .OrderByDescending(s => s.Initiative);
 
+            int attacks = 0;
             foreach (var group in attackingOrder)
             {
                 if (group.Target == null) continue;
                 if (group.Healthy == false) continue;
 
-                AttackTarget(teams, group);
+                var kill = AttackTarget(teams, group);
+                if (kill > 0) attacks++;
             }
+
+            return attacks > 0;
         }
 
-        private static void AttackTarget(Dictionary<string, int> teams, Group group)
+        private static int AttackTarget(Dictionary<string, int> teams, Group group)
         {
             var target = group.Target;
             group.Target = null;
             target.Targeted = false;
 
-            group.DealDamage(target);
+            var kill = group.DealDamage(target);
             if (target.Healthy == false)
             {
                 if (target.Target != null)
@@ -207,13 +215,14 @@ namespace D24._1
                 }
                 teams[target.Team] = teams[target.Team] - 1;
             }
+            return kill;
         }
 
-        private static void TargetSelectionPhase(List<Group> all)
+        public static void TargetSelectionPhase(List<Group> all)
         {
             var targetingOrder = all
                                 .OrderByDescending(a => a.EffectivePower)
-                                .ThenBy(a => a.Initiative);
+                                .ThenByDescending(a => a.Initiative);
 
             foreach (var group in targetingOrder)
             {

+ 13 - 0
D24.2/D24.2.csproj

@@ -0,0 +1,13 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <TargetFramework>netcoreapp2.1</TargetFramework>
+    <RootNamespace>D24._2</RootNamespace>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <ProjectReference Include="..\D24.1\D24.1.csproj" />
+  </ItemGroup>
+
+</Project>

+ 66 - 0
D24.2/Program.cs

@@ -0,0 +1,66 @@
+//#define PRINT
+
+using D24._1;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+
+namespace D24._2
+{
+    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;
+            Dictionary<string, int> origTeams = new Dictionary<string, int>();
+            _1.Program.ParseFile(args, out all, out teams);
+            foreach (var team in teams) origTeams.Add(team.Key, team.Value);
+
+            int bonus = 2;
+            string bonusTeam = "Immune System";
+            do
+            {
+#if PRINT == false
+                Console.Write($"Trying with a boost of : {bonus}\r");
+#endif
+                SystemCleanup(all, teams, origTeams, bonus, bonusTeam);
+
+                while (teams.All(t => t.Value > 0))
+                {
+
+                    _1.Program.TargetSelectionPhase(all);
+
+                    bool deadlock = _1.Program.AttackPhase(all, teams) == false;
+                    if (deadlock) break;
+                }
+
+                if (teams.Where(t => t.Key != bonusTeam).Sum(t => t.Value) > 0) bonus++; else break;
+
+            } while (true);
+
+
+            int answer = all.Sum(a => a.Number);
+            Console.WriteLine($"\nThe answer is : {answer}");
+        }
+
+        private static void SystemCleanup(List<Group> all, Dictionary<string, int> teams, Dictionary<string, int> origTeams, int bonus, string bonusTeam)
+        {
+            foreach (var team in origTeams)
+                teams[team.Key] = origTeams[team.Key];
+
+            foreach (var group in all)
+            {
+                group.Number = group.OrigNumber;
+                group.Healthy = true;
+
+                if (group.Team != bonusTeam) continue;
+                group.AtkBonus = bonus;
+            }
+        }
+    }
+}

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

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