Map.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. namespace Day23;
  2. public class Map
  3. {
  4. private readonly int _width;
  5. private readonly int _height;
  6. private readonly Tile[,] _tiles;
  7. private readonly Vec _start;
  8. private readonly Vec _goal;
  9. private PathNode? _root;
  10. public Map(Tile[,] tiles)
  11. {
  12. _tiles = tiles;
  13. _width = _tiles.GetLength(0);
  14. _height = _tiles.GetLength(1);
  15. _start = new Vec(1, 0);
  16. _goal = new Vec(_width - 2, _height - 1);
  17. }
  18. public void Print()
  19. {
  20. foreach (var t in AllTiles())
  21. {
  22. if (t.Position.X == 0 && t.Position.Y != 0)
  23. {
  24. Console.WriteLine();
  25. }
  26. Console.Write(t.Value);
  27. }
  28. Console.WriteLine();
  29. }
  30. public void Verify()
  31. {
  32. Assert(At(_start)?.IsPath ?? false);
  33. Assert(At(_goal)?.IsPath ?? false);
  34. foreach (var t in AllTiles())
  35. {
  36. if (t.IsPath)
  37. {
  38. var neighbors = Neighbors(t);
  39. Assert(neighbors.Count != 0);
  40. if (neighbors.Count == 1)
  41. {
  42. Assert(t == At(_start) || t == At(_goal));
  43. }
  44. else if (neighbors.Count >= 3)
  45. {
  46. Assert(neighbors.All(n => n.IsArrow));
  47. }
  48. }
  49. }
  50. }
  51. public void BuildGraph()
  52. {
  53. var startTile = At(_start)!;
  54. _root = BuildPath(startTile, Neighbors(startTile).Single().Position.Subtract(startTile.Position), new Dictionary<Vec, PathNode>());
  55. }
  56. public int LongestPath()
  57. {
  58. Assert(_root != null);
  59. var longest = 0;
  60. var queue = new Queue<(PathNode Path, int Length)>();
  61. queue.Enqueue((_root!, 0));
  62. while (queue.Count > 0)
  63. {
  64. var (current, length) = queue.Dequeue();
  65. var nextLength = length + current.Tiles.Count + 1;
  66. if (current.Outgoing.Count == 0)
  67. {
  68. longest = int.Max(longest, nextLength);
  69. }
  70. foreach (var next in current.Outgoing)
  71. {
  72. queue.Enqueue((next, nextLength));
  73. }
  74. }
  75. // start should not be counted (-1) and end is counted double (-1)
  76. return longest - 2;
  77. }
  78. private PathNode BuildPath(Tile start, Vec dir, IDictionary<Vec, PathNode> map)
  79. {
  80. if (map.TryGetValue(start.Position, out PathNode? path))
  81. {
  82. return path;
  83. }
  84. var pathNode = new PathNode();
  85. pathNode.Tiles.AddRange(Follow(start, dir));
  86. map[start.Position] = pathNode;
  87. var arrowTile = pathNode.Tiles.Last();
  88. var crossing = At(arrowTile.Position.Add(arrowTile.ArrowDirection()))!;
  89. if (crossing != At(_goal))
  90. {
  91. foreach (var outgoing in Neighbors(crossing)
  92. .Where(x => x.Position.Add(x.ArrowDirection()) != crossing.Position))
  93. {
  94. var outgoingPath = BuildPath(outgoing, outgoing.ArrowDirection(), map);
  95. outgoingPath.Incoming.Add(pathNode);
  96. pathNode.Outgoing.Add(outgoingPath);
  97. }
  98. }
  99. return pathNode;
  100. }
  101. private Tile? At(Vec p)
  102. {
  103. if (p.X < 0 || p.X >= _width || p.Y < 0 || p.Y >= _height)
  104. {
  105. return null;
  106. }
  107. return _tiles[p.X, p.Y];
  108. }
  109. private IEnumerable<Tile> AllTiles()
  110. {
  111. for (int y = 0; y < _height; y++)
  112. {
  113. for (int x = 0; x < _width; x++)
  114. {
  115. yield return At(new Vec(x, y))!;
  116. }
  117. }
  118. }
  119. private void Assert(bool condition)
  120. {
  121. if (!condition)
  122. {
  123. throw new Exception("Assertion FAILED");
  124. }
  125. }
  126. private List<Tile> Neighbors(Tile tile)
  127. {
  128. return tile.Neighbors().Select(At).Where(x => x is { IsPath: true }).Cast<Tile>().ToList();
  129. }
  130. private IEnumerable<Tile> Follow(Tile start, Vec dir)
  131. {
  132. var visited = new HashSet<Vec>();
  133. visited.Add(start.Position);
  134. yield return start;
  135. var current = At(start.Position.Add(dir))!;
  136. while (!current.IsArrow && current != At(_goal))
  137. {
  138. visited.Add(current.Position);
  139. yield return current;
  140. current = Neighbors(current)
  141. .Single(x => !visited.Contains(x.Position));
  142. }
  143. yield return current;
  144. }
  145. }