예제 #1
0
        public static void Day9a(bool isDay9b = false)
        {
            int             numPlayers = 0;
            int             numMarbles = 0;
            Day9GameManager _mgr;

            //452 players; last marble is worth 71250 points
            using (Stream stream = thisExe.GetManifestResourceStream("AdventOfCode2018._data.AdventOfCode_Day9.txt"))
                using (StreamReader reader = new StreamReader(stream))
                {
                    string[] s = reader.ReadLine().Split(' ');
                    numPlayers = Convert.ToInt32(s[0]);
                    numMarbles = Convert.ToInt32(s[6]) + 1;
                }

            if (isDay9b)
            {
                numMarbles *= 100;
            }

            Stopwatch sw = new Stopwatch();

            sw.Start();
            _mgr = new Day9GameManager(numPlayers, numMarbles);

            Queue <Day9Player> endGame = _mgr.RunGame();

            Day9Player winner = endGame.OrderByDescending(o => o.Score).First();

            sw.Stop();

            StringBuilder sb = new StringBuilder();

            sb.Append("The winning player is Player " + winner.PlayerNumber + " with a score of " + winner.Score.ToString() + ".");
            sb.Append(Environment.NewLine);
            sb.Append("Run in " + sw.ElapsedMilliseconds + "ms, or " + sw.ElapsedTicks + " ticks.");

            MessageBox.Show(sb.ToString());
        }
예제 #2
0
        public Queue <Day9Player> RunGame()
        {
            Day9Marble currentMarble;
            Day9Player currentPlayer;
            int        currentMarblePosition = 0;

            for (int i = 1; i <= numPlayers; i++)
            {
                Day9Player p = new Day9Player(i);
                playerList.Enqueue(p);
            }

            while (currentTurn < numMarbles)
            {
                currentMarble = DrawMarble(this.currentTurn);
                currentPlayer = playerList.AssignCircular();

                if (currentTurn != 0 && currentTurn % 23 == 0)
                {
                    currentPlayer.Score  += currentTurn;
                    currentMarblePosition = RotateBack7(currentMarblePosition);
                    Day9Marble fetchMarbleForScoring = marbleList[currentMarblePosition];
                    currentPlayer.Score += fetchMarbleForScoring.Value;
                    marbleList.RemoveAt(currentMarblePosition);
                }

                else
                {
                    currentMarblePosition = RotateBetween1And2(currentMarblePosition);
                    marbleList.Insert(currentMarblePosition, currentMarble);
                }


                currentTurn++;
            }

            return(playerList);
        }