| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- namespace Day8;
- public class MapData
- {
- public string Instructions { get; }
- public Node Start { get; }
- public MapData(string instructions, Node start)
- {
- Instructions = instructions;
- Start = start;
- }
- public long Walk()
- {
- var current = Start;
- var steps = 0L;
- while (current.Name != "ZZZ")
- {
- if (steps >= 1000000)
- {
- throw new Exception("Over ONE MILLION");
- }
-
- var instruction = Instructions[(int)(steps % Instructions.Length)];
- if (instruction != 'L' && instruction != 'R')
- {
- throw new Exception($"Unknown instruction: {instruction} @ {steps}");
- }
-
- Console.WriteLine($"{current.Name} => {instruction}");
-
- steps++;
- current = instruction == 'L' ? current.Left : current.Right;
-
- if (current == null)
- {
- throw new Exception($"Ended in null after: {instruction} @ {steps}");
- }
- }
- return steps;
- }
- }
|