Пример #1
0
        internal static long Part2(string input)
        {
            string[]   lines = input.Split('\n');
            PlayerDeck p1    = new PlayerDeck();
            PlayerDeck p2    = new PlayerDeck();

            bool playerone = true;

            foreach (string ln in lines)
            {
                if (int.TryParse(ln, out int v))
                {
                    if (playerone)
                    {
                        p1.cards.Enqueue(v);
                    }
                    else
                    {
                        p2.cards.Enqueue(v);
                    }
                }
                else if (ln == "")
                {
                    playerone = false;
                }
            }
            GameClass gc    = new GameClass(p1, int.MaxValue, p2, int.MaxValue);
            int       score = 0;

            if (gc.GetWinner())
            {
                //p1
                while (gc.p1.cards.Count > 0)
                {
                    score += gc.p1.cards.Count * gc.p1.cards.Dequeue();
                }
            }
            else
            {
                //p2
                while (gc.p2.cards.Count > 0)
                {
                    score += gc.p2.cards.Count * gc.p2.cards.Dequeue();
                }
            }
            return(score);
        }
Пример #2
0
            public bool GetWinner()
            {
                while (!GameOver(p1, p2))
                {
                    if (CheckInfinite(p1, p2))
                    {
                        return(true);
                    }
                    int card1 = p1.cards.Dequeue();
                    int card2 = p2.cards.Dequeue();

                    if (card1 <= p1.cards.Count && card2 <= p2.cards.Count)
                    {
                        GameClass gc = new GameClass(p1, card1, p2, card2);
                        if (gc.GetWinner())
                        {
                            p1.GainCards(card1, card2);
                        }
                        else
                        {
                            p2.GainCards(card2, card1);
                        }
                        continue;
                    }

                    if (card1 > card2)
                    {
                        p1.GainCards(card1, card2);
                    }
                    else
                    {
                        p2.GainCards(card2, card1);
                    }
                }

                return(p2.cards.Count == 0);                //true => player 1 wins
            }