public static void partTwoMove(ref NodeLinkedList cupLinkedList, ref Dictionary <int, Node> cupsDict, ref List <int> dictKeys) { // The three cups the crab will pick up Node firstCup = cupLinkedList.current.next; Node secondCup = firstCup.next; Node lastCup = secondCup.next; // Same as partOne int destCup = cupLinkedList.current.value - 1; // Set now, before we move the above cups and the next cup for our 3 picked up cups changes int newCurrentCup = lastCup.next.value; cupLinkedList.current.next = lastCup.next; while (true) { bool foundCup = false; if (destCup < 1) { // Similar to what we did in partOne but without the array itself we need the dictKeys destCup = dictKeys.Max(); } if (destCup == firstCup.value || destCup == secondCup.value || destCup == lastCup.value) { destCup -= 1; foundCup = false; } else { foundCup = true; } if (foundCup) { break; } } // Set our current cup as our destCup to make it easier for when we place the cups down (left of the dest cup) cupLinkedList.current = cupsDict[destCup]; cupLinkedList.placeCupsDown(firstCup, secondCup, lastCup, destCup); // Once we've placed the cups down we get our new current cup, based off the old lastcup next cup cupLinkedList.current = cupsDict[newCurrentCup]; }
public static void partTwo(ref int [] cupNumbers) { int numberOfCups = 1000000; int moves = 10000000; NodeLinkedList cupLinkedList = new NodeLinkedList(); // All credit for this idea to James and Tim Dictionary <int, Node> cupsDict = new Dictionary <int, Node>(); foreach (int cup in cupNumbers) { Node n = cupLinkedList.addNewLinkedNode(cup); cupsDict.Add(cup, n); } // Because we need to bulk out our cup Numbers to match 1000000 we itterate through and add i + 1. // Because the array length = 9 and always contains numbers 1 - 9 we can just use the length as our starting point for (int i = cupNumbers.Length; i < numberOfCups; i++) { Node n = cupLinkedList.addNewLinkedNode(i + 1); cupsDict.Add(i + 1, n); } // We'll need this later for the dest cup values List <int> dictKeys = cupsDict.Keys.ToList(); int firstCup = cupNumbers[0]; cupLinkedList.current = cupsDict[firstCup]; for (int i = 0; i < moves; i++) { partTwoMove(ref cupLinkedList, ref cupsDict, ref dictKeys); } cupLinkedList.current = cupsDict[1]; long firstCupValue = cupsDict[1].next.value; long secondCupValue = cupsDict[Convert.ToInt32(firstCupValue)].next.value; Console.WriteLine("Part Two Answer -- {0}", firstCupValue * secondCupValue); }