示例#1
0
        public void LoopThroughCircle(List <Elf> circle)
        {
            for (int i = 1; i < circle.Count; i++)
            {
                Elf currentElf = circle.ElementAt <Elf>(i);
                Elf nextElf    = currentElf._nextElf;
                //currentElf._presentCount += nextElf._presentCount;
                currentElf._nextElf = nextElf._nextElf;

                circle.Remove(nextElf);
            }

            if (circle.Count > 1)
            {
                LoopThroughCircle(circle);
            }
        }
示例#2
0
        static int StealingPart1(int maxElves)
        {
            Elf root = new Elf {
                Seat = 1, Presents = 1
            };
            Elf elf    = root;
            Elf target = null;

            for (int i = 1; i < maxElves; i++)
            {
                if (i == maxElves)
                {
                    elf.Next = root;
                }
                else
                {
                    elf.Next = new Elf {
                        Seat = i + 1, Presents = 1, Prev = elf
                    };
                }
                elf = elf.Next;
                if (i == 1)
                {
                    target = elf;
                }
            }
            elf.Next  = root;
            root.Prev = elf;
            elf       = root;

            while (elf.Next != elf)
            {
                elf.Presents += target.Presents;

                target.Prev.Next = target.Next;
                target.Next.Prev = target.Prev;
                target           = target.Next.Next;

                elf = elf.Next;
            }

            return(elf.Seat);
        }
示例#3
0
        public List <Elf> SetupCircle()
        {
            List <Elf> circle      = new List <Elf>();
            Elf        previousElf = null;

            for (int i = 0; i < _amountOfElves; i += 2)
            {
                Elf elf = new Elf(i + 1, 2);
                circle.Add(elf);

                if (previousElf != null)
                {
                    previousElf._nextElf = elf;
                }

                previousElf = elf;
            }

            circle.Last <Elf>()._nextElf = circle.First <Elf>();
            return(circle);
        }
示例#4
0
            public Elf(int id, Elf prev)
            {
                this.id = id;

                prev.next = this;
            }
示例#5
0
        //private const int input = 5;
        static void Main(string[] args)
        {
            Console.WriteLine("Day 19");
            Console.WriteLine("Star 1");
            Console.WriteLine();

            Elf firstElf = new Elf(1);
            Elf lastElf  = firstElf;

            for (int i = 2; i <= input; i++)
            {
                lastElf = new Elf(i, lastElf);
            }
            lastElf.next = firstElf;

            Elf currentElf = firstElf;

            while (currentElf.Steal())
            {
                currentElf = currentElf.next;
            }


            Console.WriteLine($"Last Elf: {currentElf.id}");


            Console.WriteLine();
            Console.WriteLine("Star 2");
            Console.WriteLine();

            firstElf = new Elf(1);
            lastElf  = firstElf;

            for (int i = 2; i <= input; i++)
            {
                lastElf = new Elf(i, lastElf);
            }
            lastElf.next = firstElf;

            int currentCount = input;

            Elf preAcross = firstElf;
            int steps     = currentCount / 2 - 1;

            for (int i = 0; i < steps; i++)
            {
                preAcross = preAcross.next;
            }

            while (preAcross.next != preAcross)
            {
                preAcross.next = preAcross.next.next;
                if (currentCount % 2 == 1)
                {
                    preAcross = preAcross.next;
                }

                currentCount--;
            }

            Console.WriteLine($"Last Elf: {preAcross.id}");

            Console.WriteLine();
            Console.ReadKey();
        }
示例#6
0
 internal void StealGifts(Elf source)
 {
     source.ClearGifts();
 }
示例#7
0
 static void Remove(Elf elf)
 {
     elf.previous.next = elf.next;
     elf.next.previous = elf.previous;
 }
示例#8
0
        public void intialize_with_gift()
        {
            var elf = new Elf();

            Assert.True(elf.HasGift);
        }