namespace Day8; public class MapData { public string Instructions { get; } public IList Nodes { get; } public MapData(string instructions, IList nodes) { Instructions = instructions; Nodes = nodes; } public long Walk() { var current = Nodes.First(n => n.IsStart); var steps = 0L; while (!current.IsEnd) { current = Move(steps, current); steps++; } return steps; } public long[] MultiWalk() { var starts = Nodes.Where(n => n.Name.Last() == 'A').ToArray(); var cycleLengths = new List(); foreach (var node in starts) { var current = node; var steps = 0; while (!current.IsEnd) { current = Move(steps, current); steps++; } cycleLengths.Add(steps); Console.WriteLine(string.Join(", ", cycleLengths)); } return cycleLengths.ToArray(); } private Node Move(long step, Node node) { if (step >= 10000000) { throw new Exception("Over TEN MILLION"); } var instruction = Instructions[(int)(step % Instructions.Length)]; if (instruction != 'L' && instruction != 'R') { throw new Exception($"Unknown instruction: {instruction} @ {step}"); } return (instruction == 'L' ? node.Left : node.Right) ?? throw new Exception($"Ended in null after: {instruction} @ {step}"); } }