Lukas Angerer 2 éve
szülő
commit
1ed91bc458
6 módosított fájl, 70 hozzáadás és 13 törlés
  1. 12 11
      Day17/Map.cs
  2. 9 2
      Day17/Path.cs
  3. 6 0
      Day17/Program.cs
  4. 5 0
      Day17/Tile.cs
  5. 33 0
      Day17/UltraPath.cs
  6. 5 0
      Day17/inputs/sample2.txt

+ 12 - 11
Day17/Map.cs

@@ -5,12 +5,14 @@ public class Map
     private readonly Tile[,] _tiles;
     private readonly int _width;
     private readonly int _height;
+    private Func<Path?, Tile, Vec, Path> _pathFactory;
 
     public Map(Tile[,] tiles, int width, int height)
     {
         _tiles = tiles;
         _width = width;
         _height = height;
+        _pathFactory = (path, tile, vRun) => new Path(path, tile, vRun);
     }
 
     public void Print()
@@ -20,6 +22,7 @@ public class Map
             for (var x = 0; x < _width; x++)
             {
                 Console.Write(_tiles[x, y].Display);
+                _tiles[x, y].ClearDisplay();
             }
 
             Console.WriteLine();
@@ -30,12 +33,11 @@ public class Map
 
     public int ShortestPath()
     {
-        var n = 0;
         var goal = _tiles[_width - 1, _height - 1];
         var visited = new HashSet<string>();
         var queue = new PriorityQueue<Path, int>();
-        queue.Enqueue(new Path(null, _tiles[0, 0], new Vec(1, 0)), 0);
-        queue.Enqueue(new Path(null, _tiles[0, 0], new Vec(0, 1)), 0);
+        queue.Enqueue(_pathFactory(null, _tiles[0, 0], new Vec(1, 0)), 0);
+        queue.Enqueue(_pathFactory(null, _tiles[0, 0], new Vec(0, 1)), 0);
 
         while (queue.Count > 0)
         {
@@ -51,8 +53,8 @@ public class Map
                 var pos = current.Tile.Position.Add(v);
                 if (IsValid(pos))
                 {
-                    var next = new Path(current, Tile(pos), v);
-                    if (next.Tile == goal)
+                    var next = _pathFactory(current, Tile(pos), v);
+                    if (next.Tile == goal && next.CanStop())
                     {
                         var cost = Backtrack(next);
                         return cost;
@@ -60,17 +62,16 @@ public class Map
                     queue.Enqueue(next, next.Cost);
                 }
             }
-
-            if (n % 100 == 0)
-            {
-                Console.WriteLine($"Queue: {queue.Count}");
-            }
-            n++;
         }
 
         return -1;
     }
 
+    public void SetUltraPathMode()
+    {
+        _pathFactory = (path, tile, vRun) => new UltraPath(path, tile, vRun);
+    }
+
     private int Backtrack(Path path)
     {
         var sum = 0;

+ 9 - 2
Day17/Path.cs

@@ -14,12 +14,14 @@ public class Path
         Tile = tile;
         
         VRun = vRun;
-        LRun = vRun == Prev?.VRun ? Prev!.LRun + 1 : 1;
+        LRun = prev == null
+            ? 0
+            : vRun == Prev?.VRun ? Prev!.LRun + 1 : 1;
 
         Cost = (prev?.Cost ?? 0) + tile.Cost;
     }
 
-    public IEnumerable<Vec> Next()
+    public virtual IEnumerable<Vec> Next()
     {
         yield return VRun.RotatePositive();
         if (LRun < 3)
@@ -29,6 +31,11 @@ public class Path
         yield return VRun.RotateNegative();
     }
 
+    public virtual bool CanStop()
+    {
+        return true;
+    }
+
     public string GetKey()
     {
         return $"{Tile.Position.X}/{Tile.Position.Y},{VRun.X}/{VRun.Y},{LRun}";

+ 6 - 0
Day17/Program.cs

@@ -22,4 +22,10 @@ var cost = map.ShortestPath();
 map.Print();
 Console.WriteLine($"Cost: {cost}");
 
+Console.WriteLine();
+map.SetUltraPathMode();
+cost = map.ShortestPath();
+map.Print();
+Console.WriteLine($"Cost: {cost}");
+
 return 0;

+ 5 - 0
Day17/Tile.cs

@@ -17,4 +17,9 @@ public class Tile
     {
         Display = display;
     }
+    
+    public void ClearDisplay()
+    {
+        Display = Cost.ToString()[0];
+    }
 }

+ 33 - 0
Day17/UltraPath.cs

@@ -0,0 +1,33 @@
+namespace Day17;
+
+public class UltraPath : Path
+{
+    public UltraPath(Path? prev, Tile tile, Vec vRun)
+        : base(prev, tile, vRun)
+    {
+    }
+    
+    public override IEnumerable<Vec> Next()
+    {
+        if (LRun < 4)
+        {
+            yield return VRun;
+        }
+        else if (LRun < 10)
+        {
+            yield return VRun.RotatePositive();
+            yield return VRun;
+            yield return VRun.RotateNegative();
+        }
+        else
+        {
+            yield return VRun.RotatePositive();
+            yield return VRun.RotateNegative();
+        }
+    }
+    
+    public override bool CanStop()
+    {
+        return LRun >= 4;
+    }
+}

+ 5 - 0
Day17/inputs/sample2.txt

@@ -0,0 +1,5 @@
+111111111111
+999999999991
+999999999991
+999999999991
+999999999991