示例#1
0
        private static (int destValue, List <Cup> nextThree) FindDestValue(int currCupValue, Cup curr, int max)
        {
            var nextThree = new List <Cup> {
                curr.Next, curr.Next.Next, curr.Next.Next.Next
            };

            curr.Next = nextThree[2].Next;
            nextThree[2].Next.Prev = curr;

            nextThree[0].Prev = null;
            nextThree[2].Next = null;

            int destValue = curr.Value;

            destValue--; // zero based
            for (int i = 0; i < 4; i++)
            {
                if (destValue == 0)
                {
                    destValue += max;
                    destValue--;
                    destValue %= max;
                }
                else
                {
                    destValue--;
                }

                if (nextThree.All(x => x.Value != destValue + 1))
                {
                    return(destValue + 1, nextThree);
                }
            }

            throw new ApplicationException();
        }
示例#2
0
        static void PartTwo(string input)
        {
            var map = new Dictionary <int, Cup>();

            Cup head = null;
            Cup curr = null;

            foreach (var cupVal in input)
            {
                var cup = new Cup(cupVal - '0');
                map.Add(cupVal - '0', cup);

                if (head == null && curr == null)
                {
                    head = cup;
                    curr = cup;
                }
                else
                {
                    curr.Next = cup;
                    cup.Prev  = curr;
                    curr      = cup;
                }
            }

            int maxCups = 1000000;

            for (int i = 10; i <= maxCups; i++)
            {
                var cup = new Cup(i);
                curr.Next = cup;
                cup.Prev  = curr;
                curr      = cup;

                map.Add(i, cup);
            }

            curr.Next = head;
            head.Prev = curr;


            curr = head;

            // Play game
            var maxTurns = 10000000;

            for (int t = 0; t < maxTurns; t++)
            {
                int currCupValue = curr.Value;
                var(destValue, nextThree) = FindDestValue(currCupValue, curr, maxCups);

                var destCup = map[destValue];
                nextThree[2].Next = destCup.Next;
                nextThree[0].Prev = destCup;
                destCup.Next.Prev = nextThree[2];
                destCup.Next      = nextThree[0];

                curr = curr.Next;
            }

            var p12    = 0;
            var p1Node = map[1];

            for (int i = 0; i < 8; i++)
            {
                p1Node = p1Node.Next;
                Console.WriteLine(p1Node.Value);
                p12 *= 10;
                p12 += p1Node.Value;
            }
            Console.WriteLine(p12);


            var prevValue = map[1].Next.Value;
            var nextValue = map[1].Next.Next.Value;

            Console.WriteLine(prevValue);
            Console.WriteLine(nextValue);

            p2 = prevValue * nextValue;
        }