| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- 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;
- }
- }
|