bastien.monsarrat 6 anos atrás
pai
commit
c2564167ae
1 arquivos alterados com 17 adições e 14 exclusões
  1. 17 14
      D15.1/Program.cs

+ 17 - 14
D15.1/Program.cs

@@ -2,6 +2,7 @@
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;
+using System.Threading.Tasks;
 
 namespace D15._1
 {
@@ -37,6 +38,8 @@ namespace D15._1
         static readonly (int mx, int my)[] MoveSequence = new [] { (0, -1), (-1, 0), (1, 0), (0, 1) };
         static int round = 0;
 
+        static bool PRINT = false;
+
         static void Main(string[] args)
         {
             if (args.Length < 1) return;
@@ -48,25 +51,25 @@ namespace D15._1
             var units = new Units();
 
             FillMap(file, map, strMap, units);
-            PrintMap(map, strMap, units);
+            if (PRINT) PrintMap(map, strMap, units, 0);
 
             bool continueCombat = true;
             do
             {
-                Console.WriteLine($"Playing round {round + 1}");
-
                 continueCombat = Tick(map, units);
                 if (continueCombat) round++;
 
-                PrintMap(map, strMap, units);
+                if (PRINT) PrintMap(map, strMap, units, round);
 
             } while (continueCombat);
 
             TheAnswerIs(units, round);
         }
 
-        private static void PrintMap(Map map, List<string> strMap, Units units)
+        private static void PrintMap(Map map, List<string> strMap, Units units, int round)
         {
+            if (round > 0) Console.WriteLine($"Playing round {round + 1}");
+
             for (int y = 0; y < strMap.Count; ++y)
             {
                 var line = strMap[y];
@@ -127,12 +130,9 @@ namespace D15._1
                 if (filtered == null || filtered.Count == 0) return false;
 
                 var inRange = new Map();
-                var inRangeActions = new Dictionary<(int x, int y), List<(int x, int y)>>();
 
                 var unitNeedsToMove = GetTilesInRange(map, inRange, unit, filtered);
-
-                if (unitNeedsToMove)
-                    MoveUnit(map, units, unit, inRange, inRangeActions);
+                if (unitNeedsToMove) MoveUnit(map, units, unit, inRange);
 
                 var targetHasDied = AttackNearestTarget(unit, filtered);
 
@@ -181,10 +181,14 @@ namespace D15._1
             return minHpTarget;
         }
 
-        private static void MoveUnit(Map map, Units units, Unit unit, Map inRange, Dictionary<(int x, int y), List<(int x, int y)>> inRangeActions)
+        private static void MoveUnit(Map map, Units units, Unit unit, Map inRange)
         {
-            foreach (var r in inRange)
+            var inRangeActions = new Dictionary<(int x, int y), List<(int x, int y)>>();
+
+            Parallel.ForEach(inRange, (r) =>
+            {
                 GetBreadthFirstSearch(unit, map, units, inRangeActions, r);
+            });
 
             if (inRangeActions.Count == 0)
                 return;
@@ -253,7 +257,7 @@ namespace D15._1
 
             actionList.Reverse();
 
-            inRangeActions.Add(root, actionList);
+            lock (inRangeActions) inRangeActions.Add(root, actionList);
         }
 
         private static bool GetTilesInRange(Map map, Map inRange, Unit unit, List<Unit> filtered)
@@ -268,8 +272,7 @@ namespace D15._1
                     if (nc.x == unit.Coord.X && nc.y == unit.Coord.Y)
                         return false;
 
-                    if (map.Contains(nc))
-                        inRange.Add(nc);
+                    if (map.Contains(nc)) inRange.Add(nc);
                 }
             }