Beispiel #1
0
        public static string Visualise(this Marble start, Marble current)
        {
            var visualisation = new StringBuilder();

            Marble active = start;

            do
            {
                if (active == current)
                {
                    visualisation.Append("(");
                    visualisation.Append(active.Number);
                    visualisation.Append(")");
                }
                else
                {
                    visualisation.Append(active.Number);
                }

                visualisation.Append(" ");
                active = active.Clockwise;
            }while (active != start);

            return(visualisation.ToString());
        }
Beispiel #2
0
        public static Marble Clockwise(this Marble marble, int count)
        {
            Marble current = marble;

            for (int i = 0; i < count; i++)
            {
                current = current.Clockwise;
            }

            return(current);
        }
Beispiel #3
0
        public static Marble Remove(this Marble marble)
        {
            Marble prev = marble.CounterClockwise;
            Marble next = marble.Clockwise;

            prev.Clockwise        = next;
            next.CounterClockwise = prev;

            marble.Clockwise        = null;
            marble.CounterClockwise = null;

            return(marble);
        }
Beispiel #4
0
        public static Marble InsertAfter(this Marble marble, int number)
        {
            Marble next      = marble.Clockwise;
            var    newMarble = new Marble
            {
                Number           = number,
                Clockwise        = next,
                CounterClockwise = marble,
            };

            marble.Clockwise      = newMarble;
            next.CounterClockwise = newMarble;

            return(newMarble);
        }
Beispiel #5
0
        public long Solve(int players, int marbles)
        {
            long[] scores = new long[players];

            // Set up Marble 0.
            // We're going to solve this using a doubly linked list
            var marble0 = new Marble
            {
                Number = 0,
            };

            marble0.Clockwise        = marble0;
            marble0.CounterClockwise = marble0;

            Marble currentMarble = marble0;
            int    currentPlayer = 0;

            for (int i = 1; i <= marbles; i++)
            {
                if (i % 23 == 0)
                {
                    // Current player keeps this marble
                    scores[currentPlayer] += i;

                    // Also keeps the marble 7 counter clockwise
                    Marble marbleToRemove = currentMarble.CounterClockwise(7);
                    currentMarble          = marbleToRemove.Clockwise;
                    scores[currentPlayer] += marbleToRemove.Number;
                    marbleToRemove.Remove();
                }
                else
                {
                    currentMarble = currentMarble.Clockwise(1).InsertAfter(i);
                }

                currentPlayer = ++currentPlayer % players;
            }

            return(scores.Max());
        }