Ver código fonte

Day 11: expanding the universe

Lukas Angerer 2 anos atrás
pai
commit
e472357065
6 arquivos alterados com 127 adições e 2 exclusões
  1. 22 0
      Day11/Galaxy.cs
  2. 9 0
      Day11/GridPoint.cs
  3. 16 2
      Day11/Parser.cs
  4. 7 0
      Day11/Program.cs
  5. 63 0
      Day11/Universe.cs
  6. 10 0
      Day11/inputs/sample1.txt

+ 22 - 0
Day11/Galaxy.cs

@@ -0,0 +1,22 @@
+using Day10;
+
+namespace Day11;
+
+public class Galaxy
+{
+    private static int _galaxyCounter = 0;
+    
+    public int Id { get; }
+    public GridPoint Position { get; private set; }
+
+    public Galaxy(GridPoint position)
+    {
+        Id = _galaxyCounter++;
+        Position = position;
+    }
+
+    public void Move(GridPoint delta)
+    {
+        Position = Position.Add(delta);
+    }
+}

+ 9 - 0
Day11/GridPoint.cs

@@ -0,0 +1,9 @@
+namespace Day10;
+
+public record GridPoint(int X, int Y)
+{
+    public GridPoint Add(GridPoint other)
+    {
+        return new GridPoint(X + other.X, Y + other.Y);
+    }
+}

+ 16 - 2
Day11/Parser.cs

@@ -1,5 +1,7 @@
 using System.Text.RegularExpressions;
 
+using Day10;
+
 namespace Day11;
 
 public partial class Parser
@@ -7,13 +9,25 @@ public partial class Parser
     [GeneratedRegex(@".*")]
     private partial Regex LineMatch();
     
-    public void Parse(string inputFile)
+    public Universe Parse(string inputFile)
     {
         using var reader = File.OpenText(inputFile);
-        
+
+        var universe = new Universe();
+        var row = 0;
         while (!reader.EndOfStream)
         {
             var line = reader.ReadLine()!;
+            for (var col = 0; col < line.Length; col++)
+            {
+                if (line[col] == '#')
+                {
+                    universe.Add(new Galaxy(new GridPoint(col, row)));
+                }
+            }
+            row++;
         }
+
+        return universe;
     }
 }

+ 7 - 0
Day11/Program.cs

@@ -16,5 +16,12 @@ if (args.Length < 1)
 
 var inputFile = args[0];
 var parser = new Parser();
+var universe = parser.Parse(inputFile);
+universe.Expand();
+
+foreach (var g in universe.Galaxies)
+{
+    Console.WriteLine($"{g.Id}: {g.Position.X} / {g.Position.Y}");
+}
 
 return 0;

+ 63 - 0
Day11/Universe.cs

@@ -0,0 +1,63 @@
+using System.Collections.ObjectModel;
+
+using Day10;
+
+namespace Day11;
+
+public class Universe
+{
+    public IList<Galaxy> Galaxies { get; } = new List<Galaxy>();
+
+    public void Add(Galaxy galaxy)
+    {
+        Galaxies.Add(galaxy);
+    }
+
+    public void Expand()
+    {
+        var xMap = new Dictionary<int, IList<Galaxy>>();
+        var yMap = new Dictionary<int, IList<Galaxy>>();
+
+        foreach (var g in Galaxies)
+        {
+            if (!xMap.ContainsKey(g.Position.X))
+            {
+                xMap.Add(g.Position.X, new List<Galaxy>());
+            }
+            xMap[g.Position.X].Add(g);
+            if (!yMap.ContainsKey(g.Position.Y))
+            {
+                yMap.Add(g.Position.Y, new List<Galaxy>());
+            }
+            yMap[g.Position.Y].Add(g);
+        }
+
+        var xPrev = -1;
+        var xExpand = 0;
+        foreach (var x in xMap.Keys.Order())
+        {
+            xExpand += x - xPrev - 1;
+            xPrev = x;
+            
+            foreach (var g in xMap[x])
+            {
+                var delta = new GridPoint(xExpand, 0);
+                g.Move(delta);
+            }
+        }
+        
+        var yPrev = -1;
+        var yExpand = 0;
+        foreach (var y in yMap.Keys.Order())
+        {
+            yExpand += y - yPrev - 1;
+            yPrev = y;
+            
+            foreach (var g in yMap[y])
+            {
+                var delta = new GridPoint(0, yExpand);
+                g.Move(delta);
+            }
+        }
+    }
+}

+ 10 - 0
Day11/inputs/sample1.txt

@@ -0,0 +1,10 @@
+...#......
+.......#..
+#.........
+..........
+......#...
+.#........
+.........#
+..........
+.......#..
+#...#.....