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