Map.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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));
  55. // Assert that we have no cycles in the graph
  56. var seen = new HashSet<PathNode>();
  57. foreach (var n in BreadthFirst())
  58. {
  59. if (seen.Contains(n))
  60. {
  61. throw new Exception("There is a cycle in the graph");
  62. }
  63. seen.Add(n);
  64. }
  65. }
  66. public int LongestPath()
  67. {
  68. Assert(_root != null);
  69. var longest = 0;
  70. var queue = new Queue<(PathNode Path, int Length)>();
  71. queue.Enqueue((_root!, 0));
  72. while (queue.Count > 0)
  73. {
  74. var (current, length) = queue.Dequeue();
  75. var nextLength = length + current.Tiles.Count + 1;
  76. if (current.Connected.Count == 0)
  77. {
  78. longest = int.Max(longest, nextLength);
  79. }
  80. foreach (var next in current.Connected)
  81. {
  82. queue.Enqueue((next, nextLength));
  83. }
  84. }
  85. // start should not be counted (-1) and end is counted double (-1)
  86. return longest - 2;
  87. }
  88. private PathNode BuildPath(Tile start, Vec dir)
  89. {
  90. var pathNode = new PathNode();
  91. pathNode.Tiles.AddRange(Follow(start, dir));
  92. var crossing = At(pathNode.Tiles.Last().Position.Add(pathNode.Tiles.Last().ArrowDirection()))!;
  93. crossing.Incoming.Add(pathNode);
  94. if (crossing != At(_goal))
  95. {
  96. foreach (var outgoing in Neighbors(crossing)
  97. .Where(x => x.Position.Add(x.ArrowDirection()) != crossing.Position))
  98. {
  99. pathNode.Connected.Add(BuildPath(outgoing, outgoing.ArrowDirection()));
  100. }
  101. }
  102. return pathNode;
  103. }
  104. private Tile? At(Vec p)
  105. {
  106. if (p.X < 0 || p.X >= _width || p.Y < 0 || p.Y >= _height)
  107. {
  108. return null;
  109. }
  110. return _tiles[p.X, p.Y];
  111. }
  112. private IEnumerable<Tile> AllTiles()
  113. {
  114. for (int y = 0; y < _height; y++)
  115. {
  116. for (int x = 0; x < _width; x++)
  117. {
  118. yield return At(new Vec(x, y))!;
  119. }
  120. }
  121. }
  122. private void Assert(bool condition)
  123. {
  124. if (!condition)
  125. {
  126. throw new Exception("Assertion FAILED");
  127. }
  128. }
  129. private List<Tile> Neighbors(Tile tile)
  130. {
  131. return tile.Neighbors().Select(At).Where(x => x is { IsPath: true }).Cast<Tile>().ToList();
  132. }
  133. private IEnumerable<Tile> Follow(Tile start, Vec dir)
  134. {
  135. var visited = new HashSet<Vec>();
  136. visited.Add(start.Position);
  137. yield return start;
  138. var current = At(start.Position.Add(dir))!;
  139. while (!current.IsArrow && current != At(_goal))
  140. {
  141. visited.Add(current.Position);
  142. yield return current;
  143. current = Neighbors(current)
  144. .Single(x => !visited.Contains(x.Position));
  145. }
  146. yield return current;
  147. }
  148. private IEnumerable<PathNode> BreadthFirst()
  149. {
  150. Assert(_root != null);
  151. var queue = new Queue<PathNode>();
  152. queue.Enqueue(_root!);
  153. while (queue.Count > 0)
  154. {
  155. var current = queue.Dequeue();
  156. yield return current;
  157. foreach (var next in current.Connected)
  158. {
  159. queue.Enqueue(next);
  160. }
  161. }
  162. }
  163. }