private static (Cup first, Cup last, IDictionary <long, Cup> lookup) ReadCups(string?fileName = null) { var labels = InputFile.ReadAllLines(fileName) .Single() .Select(s => (long)(s - '0')); var lookup = new Dictionary <long, Cup>(); var first = (Cup?)null; var last = first; foreach (var label in labels) { var cup = new Cup(label); cup.Next = first ??= cup; if (last != null) { last.Next = cup; } last = cup; lookup[label] = cup; } if (first is null || last is null) { throw new Exception("No cups found."); } return(first, last, lookup); }
private static Cup PickUpNextThreeCups(Cup cup) { var firstRemoved = cup.Next; var lastRemoved = firstRemoved.Next.Next; cup.Next = lastRemoved.Next; lastRemoved.Next = firstRemoved; return(firstRemoved); }
private static Cup FindNextDestination(Cup currentCup, Cup pickedUp, IDictionary <long, Cup> lookup) { long destinationLabel = currentCup.Label; while (true) { if (--destinationLabel < 1) { destinationLabel = lookup.Count; } if (pickedUp.Label != destinationLabel && pickedUp.Next.Label != destinationLabel && pickedUp.Next.Next.Label != destinationLabel) { break; } } return(lookup[destinationLabel]); }