Lukas Angerer vor 2 Jahren
Ursprung
Commit
ab56462d26
8 geänderte Dateien mit 354 neuen und 7 gelöschten Zeilen
  1. 96 0
      Day21/Map.cs
  2. 11 7
      Day21/Parser.cs
  3. 5 0
      Day21/Program.cs
  4. 9 0
      Day21/ScreenSpace.cs
  5. 29 0
      Day21/Tile.cs
  6. 62 0
      Day21/Vec.cs
  7. 131 0
      Day21/inputs/input-lar.txt
  8. 11 0
      Day21/inputs/sample1.txt

+ 96 - 0
Day21/Map.cs

@@ -0,0 +1,96 @@
+namespace Day21;
+
+public class Map
+{
+    private readonly int _width;
+    private readonly int _height;
+    private readonly Tile[,] _map;
+    private readonly Tile? _start;
+
+    public Map(int width, int height, IEnumerable<Tile> tiles)
+    {
+        _width = width;
+        _height = height;
+        _map = new Tile[width, height];
+        var i = 0;
+        foreach (var t in tiles)
+        {
+            if (t.IsStart)
+            {
+                _start = t;
+            }
+            t.Position = new Vec(i % width, i / width);
+            _map[t.Position.X, t.Position.Y] = t;
+            i++;
+        }
+
+        if (_start == null)
+        {
+            throw new Exception("No start tile found");
+        }
+    }
+
+    public void Print(ISet<Vec>? mark = null)
+    {
+        for (int y = 0; y < _height; y++)
+        {
+            for (int x = 0; x < _width; x++)
+            {
+                if (mark != null && mark.Contains(new Vec(x, y)))
+                {
+                    Console.Write('O');
+                }
+                else
+                {
+                    Console.Write(_map[x, y]);
+                }
+            }
+
+            Console.WriteLine();
+        }
+
+        Console.WriteLine();
+    }
+
+    public long ReachableTiles(int steps)
+    {
+        var reachable = new HashSet<Vec>();
+        reachable.Add(_start!.Position);
+        
+        for (int i = 0; i < steps; i++)
+        {
+            var next = new HashSet<Vec>();
+            foreach (var p in reachable)
+            {
+                foreach (var neighbor in Surrounding(p).Where(n => At(n)?.IsWalkable ?? false))
+                {
+                    next.Add(neighbor);
+                }
+            }
+
+            reachable = next;
+        }
+        
+        Print(reachable);
+
+        return reachable.Count;
+    }
+
+    private IEnumerable<Vec> Surrounding(Vec middle)
+    {
+        yield return middle.Add(ScreenSpace.Up);
+        yield return middle.Add(ScreenSpace.Right);
+        yield return middle.Add(ScreenSpace.Down);
+        yield return middle.Add(ScreenSpace.Left);
+    }
+
+    private Tile? At(Vec p)
+    {
+        if (p.X < 0 || p.X >= _width || p.Y < 0 || p.Y >= _height)
+        {
+            return null;
+        }
+
+        return _map[p.X, p.Y];
+    }
+}

+ 11 - 7
Day21/Parser.cs

@@ -7,13 +7,17 @@ public partial class Parser
     [GeneratedRegex(@".*")]
     private partial Regex LineMatch();
     
-    public void Parse(string inputFile)
+    public Map Parse(string inputFile)
     {
-        using var reader = File.OpenText(inputFile);
-        
-        while (!reader.EndOfStream)
-        {
-            var line = reader.ReadLine()!;
-        }
+         var lines = File.ReadAllLines(inputFile);
+         var width = lines.First().Length;
+         var height = lines.Length;
+
+         return new Map(width, height, TileFactory(lines));
+    }
+
+    private IEnumerable<Tile> TileFactory(string[] lines)
+    {
+        return lines.SelectMany(l => l).Select(c => new Tile(c));
     }
 }

+ 5 - 0
Day21/Program.cs

@@ -16,5 +16,10 @@ if (args.Length < 1)
 
 var inputFile = args[0];
 var parser = new Parser();
+var map = parser.Parse(inputFile);
+
+var reachable = map.ReachableTiles(64);
+Console.WriteLine();
+Console.WriteLine($"Reachable: {reachable}");
 
 return 0;

+ 9 - 0
Day21/ScreenSpace.cs

@@ -0,0 +1,9 @@
+namespace Day21;
+
+public static class ScreenSpace
+{
+    public static Vec Up { get; } = new Vec(0, -1);
+    public static Vec Down { get; } = new Vec(0, 1);
+    public static Vec Left { get; } = new Vec(-1, 0);
+    public static Vec Right { get; } = new Vec(1, 0);
+}

+ 29 - 0
Day21/Tile.cs

@@ -0,0 +1,29 @@
+namespace Day21;
+
+public class Tile
+{
+    private readonly char _type;
+    
+    public bool IsStart { get; }
+    public Vec Position { get; set; }
+
+    public bool IsWalkable => _type != '#';
+
+    public Tile(char type)
+    {
+        if (type == 'S')
+        {
+            _type = '.';
+            IsStart = true;
+        }
+        else
+        {
+            _type = type;
+        }
+    }
+
+    public override string ToString()
+    {
+        return _type.ToString();
+    }
+}

+ 62 - 0
Day21/Vec.cs

@@ -0,0 +1,62 @@
+namespace Day21;
+
+public record struct Vec
+{
+    public static Vec Zero { get; } = new Vec(0, 0);
+    
+    public long X { get; }
+    public long Y { get; }
+
+    public bool IsHorizontal => Y == 0;
+    public bool IsVertical => X == 0;
+
+    public Vec(long x, long y)
+    {
+        X = x;
+        Y = y;
+    }
+
+    public Vec RotatePositive()
+    {
+        // clockwise
+        return new Vec(-1 * Y, X);
+    }
+    
+    public Vec RotateNegative()
+    {
+        // counter-clockwise
+        return new Vec(Y, -1 * X);
+    }
+
+    public Vec Add(Vec other)
+    {
+        return new Vec(X + other.X, Y + other.Y);
+    }
+
+    public Vec Multiply(int f)
+    {
+        return new Vec(f * X, f * Y);
+    }
+
+    public long Cross(Vec other)
+    {
+        return X * other.Y - other.X * Y;
+    }
+
+    public static Vec Direction(char c)
+    {
+        switch (c)
+        {
+            case 'R':
+                return new Vec(1, 0);
+            case 'L':
+                return new Vec(-1, 0);
+            case 'U':
+                return new Vec(0, -1);
+            case 'D':
+                return new Vec(0, 1);
+        }
+
+        return default;
+    }
+}

+ 131 - 0
Day21/inputs/input-lar.txt

@@ -0,0 +1,131 @@
+...................................................................................................................................
+.........#.............#..#.................................#..........#......##.............#.#..#.#......................##......
+...#.................#.....#........#.#............#...#...#................##............#...#.............#.....#.......#........
+...........#.#.#...........#................#....#....................................#........#..#...............#................
+...#.......#...#...#...........#.#....##...........#...#...............................#..................#..#.#..#....#.#.#.......
+..##.#.................#........#...........................................#...#...................................#..............
+......#......................##....##......#.......................................#.........#........#...........#.#..............
+...#.....#.......#...............#....##...#......#..#.......................#...#.#....#.........#.#.......#......................
+..................................#...#.............#.............#.#..............#.......#.....................#.#...............
+..#........#...............#....#....##.....#.....#..............................#.....#...#...#..#..............#.................
+.#................................#.............##................#...#...................#............#.#....#...#.#.....#..#.....
+..............#......#......................#.............#..#.........................#......##....#...........#.........#...###..
+...#..............#.............#.....##.........#...................#...#...........#.....#..........#...............#.........#..
+...#........#......................#.#....#...#...............#....................................#.#..#..........................
+..........#.#.........#...#...#..........#..#...............#.........#....#..................................##.#.................
+..#...........#...............#.........#...#.........##...#....#....................#....#..#.....#.....###.#.....#...............
+...........................................#................#..............##............#.....#...#...............#...##.....##...
+.......#...........#....#.....#.......................#.##..........#...#.................#.......#...................##...........
+...........#.....#............#........#............#..........#.........##..#...................#..........................#......
+.......#....#..##..........#..........#..............#.##.............................................#.....#.....#...#.#....#.....
+..#...#.............#......#.#....#....#.........#.....#............#....#.......#.......#..#.#.....#...........#.............#.#..
+...#.......#.....#...#.......##....#............................#...........#.............................#........#........#....#.
+.............#.....##.#............#.#............................................#........#........#...#........#........##.#..#..
+.....#..#..#......#.............................#.......#.........#..#.............#................##..#.........#........#.......
+....#.......#...............#......#............................#.......#.#.............................................#..#.......
+.....#.........#..............#..............#...............#..........#.........................#.#...#..#.......#........#...#..
+.........................#.............................#...#.......#................................#.....#.#.....#...........#....
+..............#......#......................##....#................#....#...........##.................#.........#.....##..........
+..........#.....................#.........#...........#.....#........#...................................#.........................
+..#...#..##..............#..............#.......#...............#.#.......#...........#.#.................##......#.........#.#....
+.....##........#....##......#..................#...#...................#...........##.##..#...........#............#...............
+...#............#..#...#.................#................................#.....#..#.....#...........#..#......#.##...#..#.....#...
+......#.#...#...#......................#..#....................#......................................#......#...........#..#...#..
+..............................................#..##...............#.......#..#...#.......##.#.#.........................#...#......
+..#......#...#...........#..........#.....................#..........##...#....#.#......#..#.......................................
+....#.......##..............................................#........##.....#......#.#..............................#........#.....
+..................##..#.................................#........................................................#............#....
+......#....#.........................#.#......#.....##......#........#.#........#.............#...........#..................#.....
+.##....................#.............#.....#..........#...#.........##..........#............##.............#........#.............
+..............#.....#............#.......................#.....##.........##..#........................................#...........
+..............#...................................#.#.#......#..#....#............##.#..#.................................#........
+....#..#............................#..#...#.......................................................#...........................#...
+........#......#...........#.#.........##.#................#.##.....................#...............................#..............
+...........................#......#.........................#..#..#...........#.................#.............................#..#.
+......#...................#...#.........#.....#.........#.......#....#.......#......#...#.................................#........
+....#.#..#...#...........#....#...........#..........#.........#........................#.#...............#.............#..........
+.#............#.......................................#......#.#............###...................##.....#.............#...........
+.......#....................................#.#..........#.#...#.........#.....#................#.....#...#........................
+...........#.......................##.....#..#........##..............#.............#...........................................#..
+........#.##.........#..........#........#.................#..............#......###....#........#.....................#.#.....#...
+.....##.#...................#.............#........................#..................#..................###.............#.........
+......................#.............#.#.....#..#............#......#...#.#.................#....#........#...................#.....
+..#..................#.#..#............#.......#..............................................................#.#................#.
+..#...#.................#..#.........#........#...#.#.#......#....#.#...#............#...........#.......#.........................
+..#........................##...............................#.......#.......##.........#....#........#......#...............#......
+...............#.#...........#...........#................#.#..#.........#.........#.........#..................#..#..........#....
+.............#......#......#..#.#......#...#.#.................................#........#..#......#................#...........##..
+............#.........................#..................................#......#...........#........#.............................
+...#.......#.................#............#........................#.#...........#.#.............#.................................
+.............#.......#.........#..............#..#..#.......#..............##.........#....#........##.#...#.......................
+.....................#.#.........#.....#..#.........#................##.....#.#....................#........#......................
+........#.........#......#...#.#............................#.......#..............#....#............#..###....#....#.#............
+..................#.##..##.....#....#........................#..#....................#..#.....#..#..............#..................
+..........#..##..........#.......##.#..#...........#..............#..#..........................#....#........#........#...........
+.............#...#..#......#.#.....................................#..#.............#.......#....#............#....................
+.................................................................S.................................................................
+.................##.#...#.......#.....................#.#..............#......#..........#.##.........#.........##.................
+............................#..#..................#............##................#.......#.....##........................##........
+..................#......................#............#....#.#..#.....#..........#.#........................#.......#....#.........
+........##.....#.#.........................#..#.....#...#.....#....#...#...#............#...............#.#........................
+..............#....#.#...............#......#...............................#......#......#..............#.........................
+.#.............##..#...................#.......#...........#...#................#.......#.........#.#...#..........#.#..#..........
+......................#....................#.........................#.#........#........#............#....#......#.............#..
+...#...............#......##......#.......#.....#..#.....#.................#..#.#.........#.....#........#....#....................
+......................#....#...............#.....................................#.................#.........................#.#...
+.....#.............#.....................#................#.......##...............................................................
+...#...........#..................#............#........#...............#.............#......................................#.#...
+.....#...............#.#......#.#...#..#...#..#........#.............................#.............#.........................#.....
+.#.................#.....................#.......#..........#..........#....#.......#............#...#...#.........................
+.........#..............##...............#.............#..#...#..............##....#..#.#.#..#...#.....#...#.....................#.
+.#.......#.#...................#.#...............#.....#...#............................#....#......#..............................
+....................#...#.....#................#..........#.....................................#....#.#.....#.....................
+.........................................#.....#...............#.......#.........#.#.#......#...#.........................#...##...
+..#.##...........................#.#.................................................#.......#.#....................#.....#..##....
+..#.........#..#........##................#.......#..................#.#.........#.#.......#................................#......
+....#....#................#..........#.#..........#...........#...#............#.....#................#..............#..#....#.....
+..#.............#........#.....#...#..........#.........#...........#.......#............................#..................#.##...
+....##...........##.............#...........#......#..........#.....#........................#.###................#.......#.....#..
+........#..#.......................................................#.......#..........##.#..........#.#.............#.....#.#.#.#..
+.#.#......#.###.................#....................................#...#..#..............#....#.#..##............#..#............
+...........#....#...............##.#......#....#...#............................................#....#.........................#...
+....#.............#................................#.......#...#..........##..................#...............................#....
+........#.#.#..#.....................#.....##.#..#.......#..........#............................#........................#........
+...........#.....................##.........#...#..........#....................##....................................##....#...#..
+.....#.#......................................##........#..................#................................#.........#............
+...#....#......#.....#....................#.............#...#......#.#...#............#..##...........................#........#...
+..#....................#.............................#..........#.................##..##.#...#...................#....#.....##.....
+...........................................##................#..#...........................#.............#.....#.............#.#..
+.....................#......#.........#......#.......#.............................#...............................#........###....
+............#........#......#.#..............##................#...#...................................................#...........
+.................#...........#....................#..#............#.........#......................#..#.....................#......
+.#..............#............##..................#..#...#....#....##.......#..#.#........#.........#.......#..#...#................
+............#...#.................................#.#........................###....#..........................#.................#.
+.......#.....#.........#....................#.............#..........#............#............................................##..
+............#..#..............#....#............#.......#...#.....#........#........#...........#..#...#.....................#.#...
+....#.#...........................#..............#...........#.......#................#...........#.##.....#............#..#.......
+..........#...........#...#......#...............#.............#.......#......................#..........................#.........
+..#.......#.#............#.......#...............#.......#................................................#.......#................
+............#..............#........#..........#...#.....................#....#.............#.#.###.......#....#...................
+....#..........#.#.....#.............#.................#......##.........#.......#..........#..................#........#..........
+..#.#.#.........##....#.........#..........................###............................#......##...#.......#........#.......#...
+....#....#.....................#..................#.#.#...............#........#..........................#.....#..............##..
+.......#...#......#.......#..............#..................#.##........#.#.................................#......#..........#....
+...#.....#...........##..##....#.....#.................#.....................................#......#....##...#.............#......
+..........#.........................#...........................#.#........#.........#..#.....#.........#....#..##...#.#......#....
+............................#.....#.#..#...#..................#.....................#........##..#............#....................
+......#.............#........................................#.........#........................#..#...........#...#..#.....#......
+...........#.....#................#.#..#........#...........#.....##...............................#...............#..........##...
+......................#.........................#.........#....#...............................##.......#..#....#...#..............
+..#.....#.......#....................##...........#...........#.................#.......#..........................................
+....#....#..#............#......#..#..#.#..#......#...................#.........#...............#.#..#....................#........
+........##..............................#.#.................##......................#...............#..#......#......#..........#..
+.....#..#...........#.#.......#...............#.................................#......#...........................................
+....##.##............#..............#..#..............#.........#...................................#.......#......................
+.....#....#.....#........#......#..............................#...........#.........##......#...#............##...................
+....#..........#..#............##..#....#.#....#..#.............#...........#.#............#......#........#.......#.#.............
+........#..##.....#......##...............#......#.#.............................#..#...#........#....#..............#........#....
+..#.................#....#...#........##.....#.............................#..#.#......#.....#...#................#................
+...........#.#....................#.......##.........................................#..............#..#.............#.......#.....
+..#.....#......#.....................................#......................................#................#.#........#..........
+...................................................................................................................................

+ 11 - 0
Day21/inputs/sample1.txt

@@ -0,0 +1,11 @@
+...........
+.....###.#.
+.###.##..#.
+..#.#...#..
+....#.#....
+.##..S####.
+.##..#...#.
+.......##..
+.##.#.####.
+.##..##.##.
+...........