Map.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. var map = new Dictionary<Vec, PathNode>();
  55. BuildEdge(startTile, Neighbors(startTile).Single().Position.Subtract(startTile.Position), map);
  56. _root = map[startTile.Position];
  57. }
  58. public int LongestPath()
  59. {
  60. Assert(_root != null);
  61. var longest = 0;
  62. var visited = new HashSet<PathNode>();
  63. var queue = new Queue<(PathNode Node, int Length)>();
  64. queue.Enqueue((_root!, 0));
  65. while (queue.Count > 0)
  66. {
  67. var (current, length) = queue.Dequeue();
  68. if (current.Outgoing.Count > 2)
  69. {
  70. throw new Exception("Graph only has T-junctions");
  71. }
  72. visited.Add(current);
  73. if (current.Outgoing.Count == 0)
  74. {
  75. longest = int.Max(longest, length);
  76. }
  77. foreach (var o in current.Outgoing)
  78. {
  79. if (!visited.Contains(o.End))
  80. {
  81. queue.Enqueue((o.End, length + o.Tiles.Count + 1));
  82. }
  83. }
  84. }
  85. // start should not be counted (-1) and end is counted double (-1)
  86. return longest - 2;
  87. }
  88. public int LongestPathIgnoreDirection()
  89. {
  90. Assert(_root != null);
  91. var longest = 0;
  92. var queue = new Queue<(PathNode Node, int Length, HashSet<PathNode> Visited)>();
  93. queue.Enqueue((_root!, 0, new HashSet<PathNode>()));
  94. while (queue.Count > 0)
  95. {
  96. var (current, length, visited) = queue.Dequeue();
  97. //Console.WriteLine(current.Id);
  98. if (current.Outgoing.Count > 2)
  99. {
  100. throw new Exception("Graph only has T-junctions");
  101. }
  102. var newVisited = new HashSet<PathNode>(visited) { current };
  103. if (current.Outgoing.Count == 0 && current != _root)
  104. {
  105. longest = int.Max(longest, length);
  106. }
  107. foreach (var o in current.Outgoing.Concat(current.Incoming.Select(x => x.Invert())))
  108. {
  109. if (!visited.Contains(o.End))
  110. {
  111. queue.Enqueue((o.End, length + o.Tiles.Count + 1, newVisited));
  112. }
  113. }
  114. }
  115. // start should not be counted (-1) and end is counted double (-1)
  116. return longest - 2;
  117. }
  118. private void BuildEdge(Tile start, Vec dir, IDictionary<Vec, PathNode> map)
  119. {
  120. var previous = At(start.Previous())!;
  121. map.TryAdd(previous.Position, new PathNode());
  122. var startNode = map[previous.Position];
  123. var tiles = Follow(start, dir).ToList();
  124. var next = At(tiles.Last().Next())!;
  125. map.TryAdd(next.Position, new PathNode());
  126. var endNode = map[next.Position];
  127. if (startNode.Outgoing.All(o => o.End != endNode))
  128. {
  129. var edge = new PathEdge(startNode, endNode, tiles);
  130. startNode.Outgoing.Add(edge);
  131. endNode.Incoming.Add(edge);
  132. if (next != At(_goal))
  133. {
  134. foreach (var outgoing in Neighbors(next)
  135. .Where(x => x.Position.Add(x.ArrowDirection()) != next.Position))
  136. {
  137. BuildEdge(outgoing, outgoing.ArrowDirection(), map);
  138. }
  139. }
  140. }
  141. }
  142. private Tile? At(Vec p)
  143. {
  144. if (p.X < 0 || p.X >= _width || p.Y < 0 || p.Y >= _height)
  145. {
  146. return null;
  147. }
  148. return _tiles[p.X, p.Y];
  149. }
  150. private IEnumerable<Tile> AllTiles()
  151. {
  152. for (int y = 0; y < _height; y++)
  153. {
  154. for (int x = 0; x < _width; x++)
  155. {
  156. yield return At(new Vec(x, y))!;
  157. }
  158. }
  159. }
  160. private void Assert(bool condition)
  161. {
  162. if (!condition)
  163. {
  164. throw new Exception("Assertion FAILED");
  165. }
  166. }
  167. private List<Tile> Neighbors(Tile tile)
  168. {
  169. return tile.Neighbors().Select(At).Where(x => x is { IsPath: true }).Cast<Tile>().ToList();
  170. }
  171. private IEnumerable<Tile> Follow(Tile start, Vec dir)
  172. {
  173. var visited = new HashSet<Vec>();
  174. visited.Add(start.Position);
  175. yield return start;
  176. var current = At(start.Position.Add(dir))!;
  177. while (!current.IsArrow && current != At(_goal))
  178. {
  179. visited.Add(current.Position);
  180. yield return current;
  181. current = Neighbors(current)
  182. .Single(x => !visited.Contains(x.Position));
  183. }
  184. yield return current;
  185. }
  186. }