Пример #1
0
        private static void CupShuffle(int currentCup, Dictionary <int, Cup> cups, int movesToComplete)
        {
            for (int move = 0; move < movesToComplete; move++)
            {
                var cup1 = cups[currentCup].Next;
                var cup2 = cup1.Next;
                var cup3 = cup2.Next;

                var label          = cups[currentCup].Value;
                Cup destinationCup = null;
                while (destinationCup == null || destinationCup.Value == cup1.Value || destinationCup.Value == cup2.Value || destinationCup.Value == cup3.Value)
                {
                    label--;
                    if (label < _minValue)
                    {
                        label = _maxValue;
                    }
                    destinationCup = cups[label];
                }

                var cn = cup3.Next;
                cups[currentCup].Next = cn;
                cn.Previous           = cups[currentCup];

                var dn = destinationCup.Next;
                cup3.Next   = dn;
                dn.Previous = cup3;

                destinationCup.Next = cup1;
                cup1.Previous       = destinationCup;

                currentCup = cups[currentCup].Next.Value;
            }
        }
Пример #2
0
        private static void CupShuffle(int currentCup, Dictionary <int, Cup> cups, int moves)
        {
            for (int move = 0; move < moves; move++)
            {
                var cup1 = cups[currentCup].Next;
                var cup2 = cup1.Next;
                var cup3 = cup2.Next;

                var label          = cups[currentCup].Value;
                Cup destinationCup = null;

                while (destinationCup == null || destinationCup.Value == cup1.Value || destinationCup.Value == cup2.Value || destinationCup.Value == cup3.Value)
                {
                    label--;
                    if (label < minValue)
                    {
                        label = maxValue;
                    }
                    destinationCup = cups[label];
                }

                cups[currentCup].Next = cup3.Next;
                (cup3.Next).Previous  = cups[currentCup];

                cup1.Previous = destinationCup;
                cup3.Previous = cups[currentCup];
                cup3.Next     = destinationCup.Next;

                destinationCup.Next = cup1;

                currentCup = cups[currentCup].Next.Value;
            }
        }
Пример #3
0
        public void Place(Cup cups)
        {
            Cup first = cups;
            Cup last  = cups.Prev;

            last.Connect(Next);
            Connect(first);
        }
Пример #4
0
        public IEnumerable <int> Labels()
        {
            yield return(Label);

            for (Cup cup = Next; cup != this; cup = cup.Next)
            {
                yield return(cup.Label);
            }
        }
Пример #5
0
        public Cup Pickup()
        {
            Cup first = Next;
            Cup last  = first.Next.Next;

            Connect(last.Next);
            last.Connect(first);
            return(first);
        }
Пример #6
0
        private static void ArrangeCups(int[] cupArray, Dictionary <int, Cup> cups)
        {
            for (var cupIndex = 0; cupIndex < cupArray.Length; cupIndex++)
            {
                var value = cupArray[cupIndex];
                cups.Add(value, new Cup()
                {
                    Value    = value,
                    Previous = cupIndex > 0 ? cups[cupArray[cupIndex - 1]] : null,
                    Next     = null
                });

                if (cupIndex > 0)
                {
                    cups[cupArray[cupIndex - 1]].Next = cups[cupArray[cupIndex]];
                }

                if (value > _maxValue)
                {
                    _maxValue = value;
                }
                if (value < _minValue)
                {
                    _minValue = value;
                }
            }

            Cup previous  = cups[cupArray[cupArray.Length - 1]];
            int nextValue = _maxValue + 1;

            while (cups.Count < ONE_MILLION)
            {
                var nextCup = new Cup()
                {
                    Value    = nextValue,
                    Previous = previous,
                    Next     = null
                };

                cups.Add(nextValue, nextCup);
                previous.Next = nextCup;
                previous      = nextCup;

                if (nextValue > _maxValue)
                {
                    _maxValue = nextValue;
                }

                nextValue++;
            }

            cups[cupArray[0]].Previous = cups[nextValue - 1];
            cups[nextValue - 1].Next   = cups[cupArray[0]];
        }
Пример #7
0
        static void Part1(Cups cups)
        {
            for (int i = 0; i < 100; ++i)
            {
                cups.Move();
            }

            // zero based indexing
            Cup one = cups.Find(0);

            Console.WriteLine("Part 1: {0}", string.Join("", one.Labels().Select(label => label + 1).Skip(1)));
        }
Пример #8
0
        static void Part2(Cups cups)
        {
            for (int i = 0; i < 10000000; ++i)
            {
                cups.Move();
            }

            Cup  one   = cups.Find(0);
            long next0 = one.Next.Label + 1;
            long next1 = one.Next.Next.Label + 1;

            Console.WriteLine("Part 2: {0}", next0 * next1);
        }
Пример #9
0
        public void Move()
        {
            Cup           pickup       = _current.Pickup();
            HashSet <int> pickupLabels = new(pickup.Labels());
            int           destLabel    = (_current.Label + _cups.Count - 1) % _cups.Count;

            while (pickupLabels.Contains(destLabel))
            {
                destLabel = (destLabel + _cups.Count - 1) % _cups.Count;
            }

            _cups[destLabel].Place(pickup);
            _current = _current.Next;
        }
Пример #10
0
 public void Connect(Cup next)
 {
     Next      = next;
     next.Prev = this;
 }