-
Couldn't load subscription status.
- Fork 0
Solve Day 22 #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Solve Day 22 #38
Conversation
Codecov Report
@@ Coverage Diff @@
## main #38 +/- ##
==========================================
- Coverage 93.67% 93.62% -0.06%
==========================================
Files 32 33 +1
Lines 664 690 +26
Branches 47 50 +3
==========================================
+ Hits 622 646 +24
- Misses 27 28 +1
- Partials 15 16 +1
Continue to review full report at Codecov.
|
Day 22: Crab Combat
It only takes a few hours of sailing the ocean on a raft for boredom to sink in. Fortunately, you brought a small deck of space cards ! You'd like to play a game of Combat , and there's even an opponent available: a small crab that climbed aboard your raft before you left.
Fortunately, it doesn't take long to teach the crab the rules.
Before the game starts, split the cards so each player has their own deck (your puzzle input). Then, the game consists of a series of rounds : both players draw their top card, and the player with the higher-valued card wins the round. The winner keeps both cards, placing them on the bottom of their own deck so that the winner's card is above the other card. If this causes a player to have all of the cards, they win, and the game ends.
For example, consider the following starting decks:
This arrangement means that player 1's deck contains 5 cards, with
9on top and1on the bottom; player 2's deck also contains 5 cards, with5on top and10on the bottom.The first round begins with both players drawing the top card of their decks:
9and5. Player 1 has the higher card, so both cards move to the bottom of player 1's deck such that9is above5. In total, it takes 29 rounds before a player has all of the cards:Once the game ends, you can calculate the winning player's score . The bottom card in their deck is worth the value of the card multiplied by 1, the second-from-the-bottom card is worth the value of the card multiplied by 2, and so on. With 10 cards, the top card is worth the value on the card multiplied by 10. In this example, the winning player's score is:
So, once the game ends, the winning player's score is
306.Play the small crab in a game of Combat using the two decks you just dealt. What is the winning player's score?
Part Two
You lost to the small crab! Fortunately, crabs aren't very good at recursion. To defend your honor as a Raft Captain, you challenge the small crab to a game of Recursive Combat .
Recursive Combat still starts by splitting the cards into two decks (you offer to play with the same starting decks as before - it's only fair). Then, the game consists of a series of rounds with a few changes:
As in regular Combat, the winner of the round (even if they won the round by winning a sub-game) takes the two cards dealt at the beginning of the round and places them on the bottom of their own deck (again so that the winner's card is above the other card). Note that the winner's card might be the lower-valued of the two cards if they won the round due to winning a sub-game. If collecting cards by winning the round causes a player to have all of the cards, they win, and the game ends.
Here is an example of a small game that would loop forever without the infinite game prevention rule:
During a round of Recursive Combat, if both players have at least as many cards in their own decks as the number on the card they just dealt, the winner of the round is determined by recursing into a sub-game of Recursive Combat. (For example, if player 1 draws the
3card, and player 2 draws the7card, this would occur if player 1 has at least 3 cards left and player 2 has at least 7 cards left, not counting the3and7cards that were drawn.)To play a sub-game of Recursive Combat, each player creates a new deck by making a copy of the next cards in their deck (the quantity of cards copied is equal to the number on the card they drew to trigger the sub-game). During this sub-game, the game that triggered it is on hold and completely unaffected; no cards are removed from players' decks to form the sub-game. (For example, if player 1 drew the
3card, their deck in the sub-game would be copies of the next three cards in their deck.)Here is a complete example of gameplay, where
Game 1is the primary game of Recursive Combat:After the game, the winning player's score is calculated from the cards they have in their original deck using the same rules as regular Combat. In the above game, the winning player's score is
291.Defend your honor as Raft Captain by playing the small crab in a game of Recursive Combat using the same two decks as before. What is the winning player's score?
Link
https://adventofcode.com/2020/day/22