private static void DebugOutput(Marble currentMarble, int currentPlayer)
        {
            var currentPrintMarble = currentMarble;

            while (currentMarble.Id != 0)
            {
                currentPrintMarble = currentPrintMarble.Right;
            }
            var output      = new StringBuilder();
            var backToStart = false;

            while (!backToStart)
            {
                if (currentPrintMarble.Id == 0)
                {
                    output.Append("[" + (currentPlayer - 1) + "]");
                }

                if (currentMarble.Id == currentPrintMarble.Id)
                {
                    output.Append(" (" + currentPrintMarble.Id + ")");
                }
                else
                {
                    output.Append(" " + currentPrintMarble.Id);
                }
                currentPrintMarble = currentPrintMarble.Right;
                if (currentPrintMarble.Id == 0)
                {
                    backToStart = true;
                }
            }

            Console.WriteLine(output.ToString());
        }
Exemple #2
0
        public Marble CounterClockwise(int value)
        {
            Marble currentMarble = this;

            for (int i = 0; i < value; i++)
            {
                currentMarble = currentMarble.previous;
            }

            return(currentMarble);
        }
Exemple #3
0
        public Marble Clockwise(int value)
        {
            Marble currentMarble = this;

            for (int i = 0; i < value; i++)
            {
                currentMarble = currentMarble.next;
            }

            return(currentMarble);
        }
Exemple #4
0
        private static long ComputeHighestScore(int playerCount, int lastMarbleValue)
        {
            Marble currentMarble = new Marble(0);

            currentMarble.Clockwise        = currentMarble;
            currentMarble.CounterClockwise = currentMarble;

            long[] playerScores = new long[playerCount];

            int    playerNum = 0;
            Marble start     = currentMarble;

            // PrintMarbles(currentMarble, start);

            for (int marbleNum = 1; marbleNum <= lastMarbleValue; marbleNum++)
            {
                if (marbleNum % 23 == 0)
                {
                    // Something different happens
                    playerScores[playerNum] += marbleNum;
                    Marble toRemove = currentMarble.CounterClockwise.CounterClockwise.CounterClockwise.CounterClockwise.CounterClockwise.CounterClockwise.CounterClockwise;
                    currentMarble = toRemove.Clockwise;
                    toRemove.CounterClockwise.Clockwise = currentMarble;
                    currentMarble.CounterClockwise      = toRemove.CounterClockwise;
                    playerScores[playerNum]            += toRemove.Value;
                }
                else
                {
                    Marble insertionPoint = currentMarble.Clockwise;
                    Marble newMarble      = new Marble(marbleNum);
                    Marble insertionPointClockwiseMarble = insertionPoint.Clockwise;
                    insertionPoint.Clockwise = newMarble;
                    insertionPointClockwiseMarble.CounterClockwise = newMarble;
                    newMarble.Clockwise        = insertionPointClockwiseMarble;
                    newMarble.CounterClockwise = insertionPoint;
                    currentMarble = newMarble;
                }

                //PrintMarbles(currentMarble, start);

                playerNum++;
                if (playerNum >= playerCount)
                {
                    playerNum = 0;
                }
            }

            return(playerScores.Max());
        }
Exemple #5
0
        public static void PrintMaxScore(int players, long marbles)
        {
            Marble currentMarble = new Marble(0);

            int currentPlayer = 0;

            currentMarble.previous = currentMarble;
            currentMarble.next     = currentMarble;

            List <long> scores = new List <long>(new long[players]);

            for (long i = 1; i <= marbles; i++)
            {
                if (i % 23 == 0)
                {
                    scores[currentPlayer] += i;
                    Marble otherRemovedMarble = currentMarble.CounterClockwise(7);
                    scores[currentPlayer] += otherRemovedMarble.value;

                    currentMarble = otherRemovedMarble.next;

                    otherRemovedMarble.Remove();
                }
                else
                {
                    Marble nextMarble = currentMarble.next;

                    Marble newMarble = new Marble(i);
                    newMarble.previous      = nextMarble;
                    newMarble.next          = nextMarble.next;
                    newMarble.next.previous = newMarble;
                    nextMarble.next         = newMarble;

                    currentMarble = newMarble;
                }

                currentPlayer = (currentPlayer + 1) % players;
            }

            Console.WriteLine("winning score is {0}", scores.Max());
        }
Exemple #6
0
        static void PrintMarbles(Marble current, Marble start)
        {
            Marble m = start;

            while (true)
            {
                if (m == current)
                {
                    Console.Write($"({m.Value}) ");
                }
                else
                {
                    Console.Write(m.Value + " ");
                }

                m = m.Clockwise;
                if (m == start)
                {
                    Console.WriteLine("");
                    return;
                }
            }
        }
        public static void Run1()
        {
            var playerCount    = 463;
            var maxMarbleCount = 71787 * 100;

            var currentMarble = new Marble {
                Id = 0
            };

            currentMarble.Right = currentMarble;
            currentMarble.Left  = currentMarble;

            var scores = new Dictionary <int, long>();

            for (int i = 1; i <= playerCount; i++)
            {
                scores.Add(i, 0);
            }

            long highScore     = 0;
            var  marbleCount   = 1;
            var  currentPlayer = 1;

            while (marbleCount <= maxMarbleCount)
            {
                if (marbleCount % 23 == 0)
                {
                    scores[currentPlayer] += marbleCount;
                    var marbleToRemove = currentMarble.Left.Left.Left.Left.Left.Left.Left;
                    scores[currentPlayer]    += marbleToRemove.Id;
                    marbleToRemove.Left.Right = marbleToRemove.Right;
                    marbleToRemove.Right.Left = marbleToRemove.Left;
                    currentMarble             = marbleToRemove.Right;
                    marbleToRemove.Left       = null;
                    marbleToRemove.Right      = null;
                    marbleToRemove            = null;
                }
                else
                {
                    if (marbleCount < 3)
                    {
                        while (currentMarble.Id != 0)
                        {
                            currentMarble = currentMarble.Right;
                        }
                    }
                    else
                    {
                        currentMarble = currentMarble.Right;
                    }

                    var newMarble = new Marble()
                    {
                        Id = marbleCount, Left = currentMarble, Right = currentMarble.Right
                    };
                    currentMarble.Right  = newMarble;
                    newMarble.Right.Left = newMarble;
                    currentMarble        = newMarble;
                }

                highScore = scores.Values.Max();

                marbleCount++;
                currentPlayer++;
                if (currentPlayer > playerCount)
                {
                    currentPlayer = 1;
                }

                //DebugOutput(currentMarble, currentPlayer);

                if (marbleCount <= maxMarbleCount)
                {
                    Console.Write("\r" + marbleCount + " of " + maxMarbleCount + " High Score: " + highScore);
                }
            }

            Console.WriteLine("");
            foreach (var key in scores.Keys)
            {
                Console.WriteLine("Player: " + key + " Score: " + scores[key]);
            }
        }