aoc-2020-zig

Advent of Code 2020 Solutions in Zig
git clone https://git.sinitax.com/sinitax/aoc-2020-zig
Log | Files | Refs | README | sfeed.txt

part2 (8490B)


      1--- Part Two ---
      2
      3You lost to the small crab! Fortunately, crabs aren't very good at recursion. To defend your honor
      4as a Raft Captain, you challenge the small crab to a game of Recursive Combat.
      5
      6Recursive Combat still starts by splitting the cards into two decks (you offer to play with the same
      7starting decks as before - it's only fair). Then, the game consists of a series of
      8rounds with a few changes:
      9
     10
     11 - Before either player deals a card, if there was a previous round in this game that had exactly
     12the same cards in the same order in the same players' decks, the game instantly ends in
     13a win for player 1. Previous rounds from other games are not considered. (This prevents infinite
     14games of Recursive Combat, which everyone agrees is a bad idea.)
     15 - Otherwise, this round's cards must be in a new configuration; the players begin the round by each
     16drawing the top card of their deck as normal.
     17 - If both players have at least as many cards remaining in their deck as the value of the card they
     18just drew, the winner of the round is determined by playing a new game of Recursive Combat (see
     19below).
     20 - Otherwise, at least one player must not have enough cards left in their deck to recurse; the
     21winner of the round is the player with the higher-value card.
     22
     23
     24As in regular Combat, the winner of the round (even if they won the round by winning a sub-game)
     25takes the two cards dealt at the beginning of the round and places them on the bottom of their own
     26deck (again so that the winner's card is above the other card). Note that the winner's card might be
     27the lower-valued of the two cards if they won the round due to winning a sub-game. If
     28collecting cards by winning the round causes a player to have all of the cards, they win, and the
     29game ends.
     30
     31Here is an example of a small game that would loop forever without the infinite game prevention
     32rule:
     33
     34Player 1:
     3543
     3619
     37
     38Player 2:
     392
     4029
     4114
     42
     43During a round of Recursive Combat, if both players have at least as many cards in their own decks
     44as the number on the card they just dealt, the winner of the round is determined by recursing into a
     45sub-game of Recursive Combat. (For example, if player 1 draws the 3 card, and player 2 draws the 7
     46card, this would occur if player 1 has at least 3 cards left and player 2 has at least 7 cards left,
     47not counting the 3 and 7 cards that were drawn.)
     48
     49To play a sub-game of Recursive Combat, each player creates a new deck by making a copy
     50of the next cards in their deck (the quantity of cards copied is equal to the number on the card
     51they drew to trigger the sub-game). During this sub-game, the game that triggered it is on hold and
     52completely unaffected; no cards are removed from players' decks to form the sub-game. (For example,
     53if player 1 drew the 3 card, their deck in the sub-game would be copies of the next
     54three cards in their deck.)
     55
     56Here is a complete example of gameplay, where Game 1 is the primary game of Recursive Combat:
     57
     58=== Game 1 ===
     59
     60-- Round 1 (Game 1) --
     61Player 1's deck: 9, 2, 6, 3, 1
     62Player 2's deck: 5, 8, 4, 7, 10
     63Player 1 plays: 9
     64Player 2 plays: 5
     65Player 1 wins round 1 of game 1!
     66
     67-- Round 2 (Game 1) --
     68Player 1's deck: 2, 6, 3, 1, 9, 5
     69Player 2's deck: 8, 4, 7, 10
     70Player 1 plays: 2
     71Player 2 plays: 8
     72Player 2 wins round 2 of game 1!
     73
     74-- Round 3 (Game 1) --
     75Player 1's deck: 6, 3, 1, 9, 5
     76Player 2's deck: 4, 7, 10, 8, 2
     77Player 1 plays: 6
     78Player 2 plays: 4
     79Player 1 wins round 3 of game 1!
     80
     81-- Round 4 (Game 1) --
     82Player 1's deck: 3, 1, 9, 5, 6, 4
     83Player 2's deck: 7, 10, 8, 2
     84Player 1 plays: 3
     85Player 2 plays: 7
     86Player 2 wins round 4 of game 1!
     87
     88-- Round 5 (Game 1) --
     89Player 1's deck: 1, 9, 5, 6, 4
     90Player 2's deck: 10, 8, 2, 7, 3
     91Player 1 plays: 1
     92Player 2 plays: 10
     93Player 2 wins round 5 of game 1!
     94
     95-- Round 6 (Game 1) --
     96Player 1's deck: 9, 5, 6, 4
     97Player 2's deck: 8, 2, 7, 3, 10, 1
     98Player 1 plays: 9
     99Player 2 plays: 8
    100Player 1 wins round 6 of game 1!
    101
    102-- Round 7 (Game 1) --
    103Player 1's deck: 5, 6, 4, 9, 8
    104Player 2's deck: 2, 7, 3, 10, 1
    105Player 1 plays: 5
    106Player 2 plays: 2
    107Player 1 wins round 7 of game 1!
    108
    109-- Round 8 (Game 1) --
    110Player 1's deck: 6, 4, 9, 8, 5, 2
    111Player 2's deck: 7, 3, 10, 1
    112Player 1 plays: 6
    113Player 2 plays: 7
    114Player 2 wins round 8 of game 1!
    115
    116-- Round 9 (Game 1) --
    117Player 1's deck: 4, 9, 8, 5, 2
    118Player 2's deck: 3, 10, 1, 7, 6
    119Player 1 plays: 4
    120Player 2 plays: 3
    121Playing a sub-game to determine the winner...
    122
    123=== Game 2 ===
    124
    125-- Round 1 (Game 2) --
    126Player 1's deck: 9, 8, 5, 2
    127Player 2's deck: 10, 1, 7
    128Player 1 plays: 9
    129Player 2 plays: 10
    130Player 2 wins round 1 of game 2!
    131
    132-- Round 2 (Game 2) --
    133Player 1's deck: 8, 5, 2
    134Player 2's deck: 1, 7, 10, 9
    135Player 1 plays: 8
    136Player 2 plays: 1
    137Player 1 wins round 2 of game 2!
    138
    139-- Round 3 (Game 2) --
    140Player 1's deck: 5, 2, 8, 1
    141Player 2's deck: 7, 10, 9
    142Player 1 plays: 5
    143Player 2 plays: 7
    144Player 2 wins round 3 of game 2!
    145
    146-- Round 4 (Game 2) --
    147Player 1's deck: 2, 8, 1
    148Player 2's deck: 10, 9, 7, 5
    149Player 1 plays: 2
    150Player 2 plays: 10
    151Player 2 wins round 4 of game 2!
    152
    153-- Round 5 (Game 2) --
    154Player 1's deck: 8, 1
    155Player 2's deck: 9, 7, 5, 10, 2
    156Player 1 plays: 8
    157Player 2 plays: 9
    158Player 2 wins round 5 of game 2!
    159
    160-- Round 6 (Game 2) --
    161Player 1's deck: 1
    162Player 2's deck: 7, 5, 10, 2, 9, 8
    163Player 1 plays: 1
    164Player 2 plays: 7
    165Player 2 wins round 6 of game 2!
    166The winner of game 2 is player 2!
    167
    168...anyway, back to game 1.
    169Player 2 wins round 9 of game 1!
    170
    171-- Round 10 (Game 1) --
    172Player 1's deck: 9, 8, 5, 2
    173Player 2's deck: 10, 1, 7, 6, 3, 4
    174Player 1 plays: 9
    175Player 2 plays: 10
    176Player 2 wins round 10 of game 1!
    177
    178-- Round 11 (Game 1) --
    179Player 1's deck: 8, 5, 2
    180Player 2's deck: 1, 7, 6, 3, 4, 10, 9
    181Player 1 plays: 8
    182Player 2 plays: 1
    183Player 1 wins round 11 of game 1!
    184
    185-- Round 12 (Game 1) --
    186Player 1's deck: 5, 2, 8, 1
    187Player 2's deck: 7, 6, 3, 4, 10, 9
    188Player 1 plays: 5
    189Player 2 plays: 7
    190Player 2 wins round 12 of game 1!
    191
    192-- Round 13 (Game 1) --
    193Player 1's deck: 2, 8, 1
    194Player 2's deck: 6, 3, 4, 10, 9, 7, 5
    195Player 1 plays: 2
    196Player 2 plays: 6
    197Playing a sub-game to determine the winner...
    198
    199=== Game 3 ===
    200
    201-- Round 1 (Game 3) --
    202Player 1's deck: 8, 1
    203Player 2's deck: 3, 4, 10, 9, 7, 5
    204Player 1 plays: 8
    205Player 2 plays: 3
    206Player 1 wins round 1 of game 3!
    207
    208-- Round 2 (Game 3) --
    209Player 1's deck: 1, 8, 3
    210Player 2's deck: 4, 10, 9, 7, 5
    211Player 1 plays: 1
    212Player 2 plays: 4
    213Playing a sub-game to determine the winner...
    214
    215=== Game 4 ===
    216
    217-- Round 1 (Game 4) --
    218Player 1's deck: 8
    219Player 2's deck: 10, 9, 7, 5
    220Player 1 plays: 8
    221Player 2 plays: 10
    222Player 2 wins round 1 of game 4!
    223The winner of game 4 is player 2!
    224
    225...anyway, back to game 3.
    226Player 2 wins round 2 of game 3!
    227
    228-- Round 3 (Game 3) --
    229Player 1's deck: 8, 3
    230Player 2's deck: 10, 9, 7, 5, 4, 1
    231Player 1 plays: 8
    232Player 2 plays: 10
    233Player 2 wins round 3 of game 3!
    234
    235-- Round 4 (Game 3) --
    236Player 1's deck: 3
    237Player 2's deck: 9, 7, 5, 4, 1, 10, 8
    238Player 1 plays: 3
    239Player 2 plays: 9
    240Player 2 wins round 4 of game 3!
    241The winner of game 3 is player 2!
    242
    243...anyway, back to game 1.
    244Player 2 wins round 13 of game 1!
    245
    246-- Round 14 (Game 1) --
    247Player 1's deck: 8, 1
    248Player 2's deck: 3, 4, 10, 9, 7, 5, 6, 2
    249Player 1 plays: 8
    250Player 2 plays: 3
    251Player 1 wins round 14 of game 1!
    252
    253-- Round 15 (Game 1) --
    254Player 1's deck: 1, 8, 3
    255Player 2's deck: 4, 10, 9, 7, 5, 6, 2
    256Player 1 plays: 1
    257Player 2 plays: 4
    258Playing a sub-game to determine the winner...
    259
    260=== Game 5 ===
    261
    262-- Round 1 (Game 5) --
    263Player 1's deck: 8
    264Player 2's deck: 10, 9, 7, 5
    265Player 1 plays: 8
    266Player 2 plays: 10
    267Player 2 wins round 1 of game 5!
    268The winner of game 5 is player 2!
    269
    270...anyway, back to game 1.
    271Player 2 wins round 15 of game 1!
    272
    273-- Round 16 (Game 1) --
    274Player 1's deck: 8, 3
    275Player 2's deck: 10, 9, 7, 5, 6, 2, 4, 1
    276Player 1 plays: 8
    277Player 2 plays: 10
    278Player 2 wins round 16 of game 1!
    279
    280-- Round 17 (Game 1) --
    281Player 1's deck: 3
    282Player 2's deck: 9, 7, 5, 6, 2, 4, 1, 10, 8
    283Player 1 plays: 3
    284Player 2 plays: 9
    285Player 2 wins round 17 of game 1!
    286The winner of game 1 is player 2!
    287
    288
    289== Post-game results ==
    290Player 1's deck: 
    291Player 2's deck: 7, 5, 6, 2, 4, 1, 10, 8, 9, 3
    292
    293After the game, the winning player's score is calculated from the cards they have in their original
    294deck using the same rules as regular Combat. In the above game, the winning player's score is
    295291.
    296
    297Defend your honor as Raft Captain by playing the small crab in a game of Recursive Combat using the
    298same two decks as before. What is the winning player's score?
    299
    300