Beispiel #1
0
 public Day23(List <string> d)
 {
     allcups = new List <Cup>();
     cupsQ2  = new List <Cup>();
     foreach (char c in d[0])
     {
         int number = (int)char.GetNumericValue(c);
         Cup a      = new Cup(number);
         Cup b      = new Cup(number);
         allcups.Add(a);
         cupsQ2.Add(b);
     }
     for (int i = 0; i < allcups.Count; i++)
     {
         if (i == allcups.Count - 1)
         {
             allcups[i].Next = allcups[0];
         }
         else
         {
             allcups[i].Next = allcups[i + 1];
             cupsQ2[i].Next  = cupsQ2[i + 1];
         }
     }
 }
Beispiel #2
0
        private Circle CreateCircle(string input)
        {
            var cups = new List <Cup>();
            Cup cup  = null;

            for (var i = 0; i < input.Length; i++)
            {
                cup = new Cup()
                {
                    Id = int.Parse($"{input[i]}")
                };
                cups.Add(cup);
                if (i > 0)
                {
                    cups[i - 1].Next = cup;
                    cup.Previous     = cups[i - 1];
                }
            }
            cup.Next         = cups[0];
            cups[0].Previous = cup;

            var circle = new Circle()
            {
                Cups    = cups.ToDictionary(x => x.Id, x => x),
                Current = cups[0]
            };

            return(circle);
        }
Beispiel #3
0
 public CrabCubGame(List <Cup> cups)
 {
     _cups        = new CircularLinkedList <Cup>(cups);
     _labelToCup  = cups.ToDictionary(c => c.Label, c => c);
     _maxLabelCup = cups.Aggregate((c1, c2) => c1.Label > c2.Label ? c1 : c2);
     _minLabelCup = cups.Aggregate((c1, c2) => c1.Label > c2.Label ? c2 : c1);
 }
Beispiel #4
0
        public string Answer1()
        {
            Cup current = allcups[0];

            for (int i = 0; i < 100; i++)
            {
                Cup aftercurrent = current.Next;
                Cup mid          = aftercurrent.Next;
                Cup tail         = mid.Next;
                current.Next = tail.Next;
                int addto = current.number - 1;
                while (true)
                {
                    if (addto == 0)
                    {
                        addto += 9;
                    }

                    if (aftercurrent.number == addto || mid.number == addto || tail.number == addto)
                    {
                        addto -= 1;
                    }
                    else
                    {
                        Cup toaddto = allcups.Find(x => x.number == addto);
                        tail.Next    = toaddto.Next;
                        toaddto.Next = aftercurrent;
                        break;
                    }
                }
                current = current.Next;
            }
            StringBuilder sb        = new StringBuilder();
            Cup           toProcess = allcups.Find(x => x.number == 1).Next;

            while (toProcess != allcups.Find(x => x.number == 1))
            {
                sb.Append(toProcess.number);
                toProcess = toProcess.Next;
            }
            return(sb.ToString());
        }
Beispiel #5
0
 public string Answer2()
 {
     Cup previous = cupsQ2[^ 1];
Beispiel #6
0
 private Cup GetNextDestination(Cup currentCup)
 {
     return(currentCup.Label == _minLabelCup.Label ? _maxLabelCup : _labelToCup[currentCup.Label - 1]);
 }