Bläddra i källkod

Day 16 part 1 - ray tracing

Lukas Angerer 2 år sedan
förälder
incheckning
dc03b51143
7 ändrade filer med 317 tillägg och 5 borttagningar
  1. 51 0
      Day16/IRay.cs
  2. 64 0
      Day16/Panel.cs
  3. 16 5
      Day16/Parser.cs
  4. 5 0
      Day16/Program.cs
  5. 61 0
      Day16/Tile.cs
  6. 110 0
      Day16/inputs/input-lar.txt
  7. 10 0
      Day16/inputs/sample1.txt

+ 51 - 0
Day16/IRay.cs

@@ -0,0 +1,51 @@
+namespace Day16;
+
+public interface IRay
+{
+    bool IsHorizontal { get; }
+    bool IsVertical { get; }
+    (int X, int Y) Apply(Tile t);
+    IRay Rotate();
+    IRay AntiRotate();
+}
+
+public static class Rays
+{
+    public static IRay North { get; } = new Ray(0, -1);
+    public static IRay East { get; } = new Ray(1, 0);
+    public static IRay South { get; } = new Ray(0, 1);
+    public static IRay West { get; } = new Ray(-1, 0);
+    private static readonly IList<IRay> _rays = new List<IRay> { North, West, South, East };
+    
+    private class Ray : IRay
+    {
+        private readonly int _dX;
+        private readonly int _dY;
+
+        public bool IsHorizontal => _dX != 0;
+        public bool IsVertical => _dY != 0;
+
+        public Ray(int dX, int dY)
+        {
+            _dX = dX;
+            _dY = dY;
+        }
+        
+        public (int X, int Y) Apply(Tile t)
+        {
+            return (t.X + _dX, t.Y + _dY);
+        }
+
+        public IRay Rotate()
+        {
+            var index = _rays.IndexOf(this);
+            return _rays[(index + 1) % _rays.Count];
+        }
+
+        public IRay AntiRotate()
+        {
+            var index = _rays.IndexOf(this);
+            return _rays[(index + _rays.Count - 1) % _rays.Count];
+        }
+    }
+}

+ 64 - 0
Day16/Panel.cs

@@ -0,0 +1,64 @@
+namespace Day16;
+
+public class Panel
+{
+    private readonly Tile[,] _tiles;
+    private readonly int _width;
+    private readonly int _height;
+    
+    public Panel(Tile[,] tiles)
+    {
+        _tiles = tiles;
+        _width = tiles.GetLength(0);
+        _height = tiles.GetLength(1);
+    }
+
+    public int Print()
+    {
+        var energized = 0;
+        Console.WriteLine($"{_width} x {_height}");
+        for (var y = 0; y < _height; y++)
+        {
+            for (var x = 0; x < _width; x++)
+            {
+                energized += _tiles[x, y].IsEnergized ? 1 : 0;
+                Console.Write(_tiles[x, y].IsEnergized ? '#' : _tiles[x, y].Value);
+            }
+            Console.WriteLine();
+        }
+        Console.WriteLine();
+        return energized;
+    }
+
+    public void Energize(IRay start, int x, int y)
+    {
+        var tile = _tiles[x, y];
+        var stack = new Stack<(IRay Ray, Tile Tile)>();
+        stack.Push((start, tile));
+
+        while (stack.Count > 0)
+        {
+            var item = stack.Pop();
+            foreach (var r in item.Tile.Process(item.Ray))
+            {
+                var next = Next(r, item.Tile);
+                if (next != null)
+                {
+                    stack.Push((r, next));
+                }
+            }
+
+            Console.WriteLine($"Stack: {stack.Count}");
+        }
+
+        Console.WriteLine();
+    }
+
+    private Tile? Next(IRay ray, Tile tile)
+    {
+        var (x, y) = ray.Apply(tile);
+        return x >= 0 && x < _width && y >= 0 && y < _height
+            ? _tiles[x, y]
+            : null;
+    }
+}

+ 16 - 5
Day16/Parser.cs

@@ -7,13 +7,24 @@ public partial class Parser
     [GeneratedRegex(@".*")]
     private partial Regex LineMatch();
     
-    public void Parse(string inputFile)
+    public Panel Parse(string inputFile)
     {
-        using var reader = File.OpenText(inputFile);
-        
-        while (!reader.EndOfStream)
+        var lines = File.ReadAllLines(inputFile);
+        var width = lines[0].Length;
+        var height = lines.Length;
+        var tiles = new Tile[width, height];
+        var y = 0;
+        foreach (var l in lines)
         {
-            var line = reader.ReadLine()!;
+            var x = 0;
+            foreach (var c in l)
+            {
+                tiles[x, y] = new Tile(x, y, c);
+                x++;
+            }
+            y++;
         }
+
+        return new Panel(tiles);
     }
 }

+ 5 - 0
Day16/Program.cs

@@ -16,5 +16,10 @@ if (args.Length < 1)
 
 var inputFile = args[0];
 var parser = new Parser();
+var panel = parser.Parse(inputFile);
+
+panel.Energize(Rays.East, 0, 0);
+var energized = panel.Print();
+Console.WriteLine($"Energized: {energized}");
 
 return 0;

+ 61 - 0
Day16/Tile.cs

@@ -0,0 +1,61 @@
+namespace Day16;
+
+public class Tile
+{
+    private ISet<IRay> _processed = new HashSet<IRay>();
+    public int X { get; }
+    public int Y { get; }
+    public char Value { get; }
+    public bool IsEnergized { get; private set; }
+
+    public Tile(int x, int y, char value)
+    {
+        X = x;
+        Y = y;
+        Value = value;
+    }
+
+    public IEnumerable<IRay> Process(IRay incoming)
+    {
+        if (_processed.Contains(incoming))
+        {
+            yield break;
+        }
+        IsEnergized = true;
+        _processed.Add(incoming);
+        switch (Value)
+        {
+            case '/':
+                yield return incoming.IsHorizontal ? incoming.Rotate() : incoming.AntiRotate();
+                break;
+            case '\\':
+                yield return incoming.IsVertical ? incoming.Rotate() : incoming.AntiRotate();
+                break;
+            case '|':
+                if (incoming.IsHorizontal)
+                {
+                    yield return incoming.Rotate();
+                    yield return incoming.AntiRotate();
+                }
+                else
+                {
+                    yield return incoming;
+                }
+                break;
+            case '-':
+                if (incoming.IsVertical)
+                {
+                    yield return incoming.Rotate();
+                    yield return incoming.AntiRotate();
+                }
+                else
+                {
+                    yield return incoming;
+                }
+                break;
+            default:
+                yield return incoming;
+                break;
+        }
+    }
+}

+ 110 - 0
Day16/inputs/input-lar.txt

@@ -0,0 +1,110 @@
+\....-./...............\.............-../...|......../........../...............|......................./.....
+\.......\..-.\/|..........|......|./..........|.../..................-...................................|....
+.|........................................./...|...........................-.........../..............\....../
+.-............................|....-./|.........-................/.-./................|...|/..............-...
+/....................\..........\.//.....-..-.........................../\................|.....-/...\........
+...........................|..|..\...-......\............................../.-...|...-....../...-./.....\.....
+..|......-\.....\.........\................../...|...|..\-........................................./..........
+............../..............|......../.....................\................/|.....\...........\...-.........
+.....-................./-.\..\..........................-.\|.........../..................../.|............-..
+/..........|.........-....-.............|....................................../...\./................-.....-.
+../|.....\|...|./.................../....../...../\/........\.......|./..-...................................|
+.-./......|.........|.......\....\-..........................-..\/.../.....-.../...|....\...........-...../...
+....................|................................../....-.......\......|..-...............................
+......-............../....|..........-..../..........--.................../..|...|...\.............-....\...|.
+.............|...................-......-...............-..|.-..-.........|..\......|......|......./..........
+......-....|.....\.................|\......................./..|.........-....|.....\.....|........-.........|
+.....\............\..\...................|.../...\.....-.-.\.......|....../\.........|........................
+.........\./|\........................./........../............................../..\...-....................\
+....|.|...........||......\........\...................-......................|.....-.-.....\.................
+.....|.........\............-..................../\...|...|../..........-/..-..-.........|.../................
+............|...|.............\.-....................|.....................\............\.............-../....
+................././......\................./..\./\..............\./.......|../......|............|..|......|.
+...|...../........../..../.............|\...........|................./.........-......./.....................
+../.|......-...........................|........-......|..\..........|.....\.../.............../..............
+.........................|.............................\...........-..../.................-............/.....\
+.....\.....|......./.\........../......................|....................................../......\..../..|
+...\............|........\........\........../................./..........-....................|\.............
+........................../............................./..../..../..../......|...../..\......\.........-.|...
+............-.........|..................-...-......\.........\............|.|.\/....././|...|................
+......./....|.................................................|......../..-./..../..|.........................
+./....-..........-........-......-.-.......-..........|-..\......|...../........................\.........../.
+.....................|../|.-...........-....\..\.................|........-............|...|.|................
+..../..........||\....................-...........|...............-....../........\......\|..............\....
+.......\...|..........|.|.\....../.....|..|...........|..........-........../..\...-.............-............
+................\........../.../...-.....\.....|.-\......\.................................-.....|............
+......-.....|........\.............-........./....../.............|..........-.|..........|......\..-.........
+.......................................\..\-..........-................../........-................./.........
+../.............................-..........-........./..-.................../...../.....|......\..............
+.....\........\......-....\..........................\/..................\......\..............\|....../......
+................./...../....|......../........../..-.....-......../........................................./.
+...................|..\...|....\..............|..........|.......|..............//\......\.......\...\..-.....
+.........\........-.............\........\....................-.............|..............|\...|..-..\.......
+..|--../.......\.........|.\.......-.........-.........../..|.....-\......-...|.........................-.\...
+........|...............................-..........................-|.-.|\\|\.........-............../........
+.......................-...\...-.....-.............|..........-..-.\.....--.../........./......../...........\
+..-..|..|.............................|........................|......................\...................\|..
+....\...............\.\.|............................-|......-................\......|......|...............-.
+..../\....\..................\.............\.......\...............|.-..../.................../.\....\........
+.......................|..........\.....-.|........./........../.....|...................../.|/...............
+-...........||.........|........||.....................................................................-....\.
+..\...../.........-......\....................................../........................|..|...-...|....|.../
+..-......\............................/.....-................./..-..........\..-.|...........|.-......|..\...\
+.........................\...\................\.................--......../.........|...|........|............
+\.....\../..........|.................-............-..............|..................\................\....-..
+....................-..........|....|..|-.................|.\.|/../....................|......................
+................/|../.../.\...../.\../...........|..\........./....\.-...................-|......./..........\
+.....\.........|..................\....../.........-.....\......\.....|....|../|.........|....................
+..............-..........././...................|..|.....|.\/....-............|..../....-.....................
+....../.-..........................\..-..-........../............./.....|............/.......|.\.......-|....|
+.....\.....................|.........../....-/............-|................/...../............./.............
+...............................|.........\.......\.......|....|..................|................./..........
+.........../.........-........................................-/.......................\............-.........
+..............................................-./......./.\........./......../...........\..|..............\\.
+...........-..|........-.............\.-|............./...-..................|./.....|................../.\...
+........|..\..\|...........-..|........-.......\..|.............-....\........./..|../....-\.........../......
+/.....|......|...............||.\|.............-..\.-....................../................../...........\...
+../.-./................-................\.....\....................\-...................|-..\.................
+....\.........................-.........|.|\......\../.................\-....../...|.....|\.|...........\.....
+......\.........|.../.......\...-....../.....|.....-..-\...................\\.-.-||...........................
+....-..../...|.......\............\.|...\.................\....../..../..|.\......|\|.|.......................
+.../...........\...................../........................../......-......................................
+.-............./......\......../.........|/-........-....................................-.....-..............
+/................../...........-.........-................................\...................................
+...../...../...................../....\..................................|../.....-................|..........
+.\.....\.-.....-...\.\-......./..........-.-....................-....................\.....\...-......-.......
+..........................|......-........../....|..........\.......................................\.........
+.................|\....\.............../\.............-.\.-.................-...........-..|......./..........
+..........|...|.-...|............./..|............-.................|............\...|.|.\....................
+.........../-....\./....-................\..-........\..............................-.........\.....|.........
+.............|..........\.................-.................\\.........-/.................-|-.........-.......
+......................./...........................\.................|...-.....\......\.........../.......|...
+....-.....-.......|..--.......\....\..................................................../...........\.........
+...........................-.\..\......................................\......|.......-........-.....|........
+..-.......\............................../...................|..-.....................-......./..\.......-.-..
+....|.....-./.................\......./...........|.............................-....|........-............-..
+.....-...............|...........-.......|/..\-.............|-|........\...........|./...../..................
+|...................-../../.....|./..................|........|....\..../.....|....................../......./
+...............-./\.......|.......|./....................-....\...................-.......................|...
+.../..........\...........|.....-..../..-..-.......................|.......|.....-./......................|...
+......-..\..\........................................|...-................................|.............|.....
+...........|............-\.........../..\....../.....\....../...........|...........|.../............\........
+|....-..........-.|...........-..-..................-..\......\.......--............|.................|\\.....
+.........../|.-..........|...\.|./......../...........\...........\/....-..........\...................../....
+.........................-...../.....-./.-............................\...........-...............\.\/....../.
+..../........-...................../............../....\............................./............../.../.....
+..................-/...-......................|....................\......\..../.........-......./............
+....../....................-....\...-.-.....//...|.....................\.........\............\.-....-../.....
+......-.....\......|.|........................|.................-............../......./......................
+....../......../.........\..................|...|..................................|..-..........|....\.......
+..........\.../......................\.......\/......................-....|.........................../.\.....
+.........-..............-.|...../../............................\../...............-.............\......../...
+...\............\......................\.\......\........\............\...-.....|./...-.............|....|.-..
+/........................../.-..\|.-........../.-............./../.........-.../..............................
+|....|.\.....-..|....|....../........|.|...........\-......................|......|..........................|
+.......................\...........................................\..........|.............-.../.............
+.\.........|.............|................................\..................|||........\...-|................
+..........\...............\.....................-.....|....................-..........\|..................|.|.
+...|...........\.|......|/.-../....../.................../......\.........................|\.\..............\.
+..........................................................\......................./.....--..........|...-..\..
+........-/...-................/........\..........................\..../........................\........-....

+ 10 - 0
Day16/inputs/sample1.txt

@@ -0,0 +1,10 @@
+.|...\....
+|.-.\.....
+.....|-...
+........|.
+..........
+.........\
+..../.\\..
+.-.-/..|..
+.|....-|.\
+..//.|....