MapData.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. namespace Day8;
  2. public class MapData
  3. {
  4. public string Instructions { get; }
  5. public Node Start { get; }
  6. public MapData(string instructions, Node start)
  7. {
  8. Instructions = instructions;
  9. Start = start;
  10. }
  11. public long Walk()
  12. {
  13. var current = Start;
  14. var steps = 0L;
  15. while (current.Name != "ZZZ")
  16. {
  17. if (steps >= 1000000)
  18. {
  19. throw new Exception("Over ONE MILLION");
  20. }
  21. var instruction = Instructions[(int)(steps % Instructions.Length)];
  22. if (instruction != 'L' && instruction != 'R')
  23. {
  24. throw new Exception($"Unknown instruction: {instruction} @ {steps}");
  25. }
  26. Console.WriteLine($"{current.Name} => {instruction}");
  27. steps++;
  28. current = instruction == 'L' ? current.Left : current.Right;
  29. if (current == null)
  30. {
  31. throw new Exception($"Ended in null after: {instruction} @ {steps}");
  32. }
  33. }
  34. return steps;
  35. }
  36. }