Ejemplo n.º 1
0
        private IEnumerable <int> Day2(string inData, bool part2 = false)
        {
            CrabCombatGameV2 game = new CrabCombatGameV2(1);

            //Player 1:
            game.LoadPlayer1(new int[] { 6, 25, 8, 24, 30, 46, 42, 32, 27, 48, 5, 2, 14, 28, 37, 17, 9, 22, 40, 33, 3, 50, 47, 19, 41 });
            //Player 2:
            game.LoadPlayer2(new int[] { 1, 18, 31, 39, 16, 10, 35, 29, 26, 44, 21, 7, 45, 4, 20, 38, 15, 11, 34, 36, 49, 13, 23, 43, 12 });

            while (!game.Play())
            {
            }
            //Console.WriteLine("Day 22/2: What is the winning player's score? " + game.GetScore());
            yield return(game.GetScore());
        }
Ejemplo n.º 2
0
        public bool Play()
        {
            int p1 = Player1Queue.Peek();
            int p2 = Player2Queue.Peek();

            if (pastValues.Contains(EnmQueues()))
            {
                Player1Queue.Dequeue();
                Player2Queue.Dequeue();
                Player1Queue.Enqueue(p1);
                Player1Queue.Enqueue(p2);
                winner = 1;

                pastValues.Add(EnmQueues());

                return(Player1Queue.Count == 0 || Player2Queue.Count == 0);;
            }

            pastValues.Add(EnmQueues());

            if (Player1Queue.Count - 1 >= p1 && Player2Queue.Count - 1 >= p2)
            {
                int[]            arr1    = Player1Queue.Take(p1 + 1).ToArray().Skip(1).ToArray();
                int[]            arr2    = Player2Queue.Take(p2 + 1).ToArray().Skip(1).ToArray();
                CrabCombatGameV2 subGame = new CrabCombatGameV2(subGameNumber++);
                subGame.LoadPlayer1(arr1);
                subGame.LoadPlayer2(arr2);

                bool stopGame = false;
                while (!stopGame)
                {
                    stopGame = subGame.Play();
                }

                if (subGame.winner == 1)
                {
                    Player1Queue.Dequeue();
                    Player2Queue.Dequeue();
                    Player1Queue.Enqueue(p1);
                    Player1Queue.Enqueue(p2);
                    winner = 1;
                }

                if (subGame.winner == 2)
                {
                    Player1Queue.Dequeue();
                    Player2Queue.Dequeue();
                    Player2Queue.Enqueue(p2);
                    Player2Queue.Enqueue(p1);
                    winner = 2;
                }

                roundCtr++;

                return(Player1Queue.Count == 0 || Player2Queue.Count == 0);;
            }

            if (p1 > p2)
            {
                Player1Queue.Dequeue();
                Player2Queue.Dequeue();
                Player1Queue.Enqueue(p1);
                Player1Queue.Enqueue(p2);
                winner = 1;
            }

            if (p1 < p2)
            {
                Player1Queue.Dequeue();
                Player2Queue.Dequeue();
                Player2Queue.Enqueue(p2);
                Player2Queue.Enqueue(p1);
                winner = 2;
            }

            roundCtr++;

            return(Player1Queue.Count == 0 || Player2Queue.Count == 0);
        }