Panel.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. namespace Day16;
  2. public class Panel
  3. {
  4. private readonly Tile[,] _tiles;
  5. private readonly int _width;
  6. private readonly int _height;
  7. public Panel(Tile[,] tiles)
  8. {
  9. _tiles = tiles;
  10. _width = tiles.GetLength(0);
  11. _height = tiles.GetLength(1);
  12. }
  13. public int Print()
  14. {
  15. var energized = 0;
  16. Console.WriteLine($"{_width} x {_height}");
  17. for (var y = 0; y < _height; y++)
  18. {
  19. for (var x = 0; x < _width; x++)
  20. {
  21. energized += _tiles[x, y].IsEnergized ? 1 : 0;
  22. Console.Write(_tiles[x, y].IsEnergized ? '#' : _tiles[x, y].Value);
  23. }
  24. Console.WriteLine();
  25. }
  26. Console.WriteLine();
  27. return energized;
  28. }
  29. public void Energize(IRay start, int x, int y)
  30. {
  31. var tile = _tiles[x, y];
  32. var stack = new Stack<(IRay Ray, Tile Tile)>();
  33. stack.Push((start, tile));
  34. while (stack.Count > 0)
  35. {
  36. var item = stack.Pop();
  37. foreach (var r in item.Tile.Process(item.Ray))
  38. {
  39. var next = Next(r, item.Tile);
  40. if (next != null)
  41. {
  42. stack.Push((r, next));
  43. }
  44. }
  45. Console.WriteLine($"Stack: {stack.Count}");
  46. }
  47. Console.WriteLine();
  48. }
  49. private Tile? Next(IRay ray, Tile tile)
  50. {
  51. var (x, y) = ray.Apply(tile);
  52. return x >= 0 && x < _width && y >= 0 && y < _height
  53. ? _tiles[x, y]
  54. : null;
  55. }
  56. }