challenge.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. --- Day 22: Crab Combat ---
  2. 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.
  3. Fortunately, it doesn't take long to teach the crab the rules.
  4. 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.
  5. For example, consider the following starting decks:
  6. Player 1:
  7. 9
  8. 2
  9. 6
  10. 3
  11. 1
  12. Player 2:
  13. 5
  14. 8
  15. 4
  16. 7
  17. 10
  18. This arrangement means that player 1's deck contains 5 cards, with 9 on top and 1 on the bottom; player 2's deck also contains 5 cards, with 5 on top and 10 on the bottom.
  19. The first round begins with both players drawing the top card of their decks: 9 and 5. Player 1 has the higher card, so both cards move to the bottom of player 1's deck such that 9 is above 5. In total, it takes 29 rounds before a player has all of the cards:
  20. -- Round 1 --
  21. Player 1's deck: 9, 2, 6, 3, 1
  22. Player 2's deck: 5, 8, 4, 7, 10
  23. Player 1 plays: 9
  24. Player 2 plays: 5
  25. Player 1 wins the round!
  26. -- Round 2 --
  27. Player 1's deck: 2, 6, 3, 1, 9, 5
  28. Player 2's deck: 8, 4, 7, 10
  29. Player 1 plays: 2
  30. Player 2 plays: 8
  31. Player 2 wins the round!
  32. -- Round 3 --
  33. Player 1's deck: 6, 3, 1, 9, 5
  34. Player 2's deck: 4, 7, 10, 8, 2
  35. Player 1 plays: 6
  36. Player 2 plays: 4
  37. Player 1 wins the round!
  38. -- Round 4 --
  39. Player 1's deck: 3, 1, 9, 5, 6, 4
  40. Player 2's deck: 7, 10, 8, 2
  41. Player 1 plays: 3
  42. Player 2 plays: 7
  43. Player 2 wins the round!
  44. -- Round 5 --
  45. Player 1's deck: 1, 9, 5, 6, 4
  46. Player 2's deck: 10, 8, 2, 7, 3
  47. Player 1 plays: 1
  48. Player 2 plays: 10
  49. Player 2 wins the round!
  50. ...several more rounds pass...
  51. -- Round 27 --
  52. Player 1's deck: 5, 4, 1
  53. Player 2's deck: 8, 9, 7, 3, 2, 10, 6
  54. Player 1 plays: 5
  55. Player 2 plays: 8
  56. Player 2 wins the round!
  57. -- Round 28 --
  58. Player 1's deck: 4, 1
  59. Player 2's deck: 9, 7, 3, 2, 10, 6, 8, 5
  60. Player 1 plays: 4
  61. Player 2 plays: 9
  62. Player 2 wins the round!
  63. -- Round 29 --
  64. Player 1's deck: 1
  65. Player 2's deck: 7, 3, 2, 10, 6, 8, 5, 9, 4
  66. Player 1 plays: 1
  67. Player 2 plays: 7
  68. Player 2 wins the round!
  69. == Post-game results ==
  70. Player 1's deck:
  71. Player 2's deck: 3, 2, 10, 6, 8, 5, 9, 4, 7, 1
  72. 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:
  73. 3 * 10
  74. + 2 * 9
  75. + 10 * 8
  76. + 6 * 7
  77. + 8 * 6
  78. + 5 * 5
  79. + 9 * 4
  80. + 4 * 3
  81. + 7 * 2
  82. + 1 * 1
  83. = 306
  84. So, once the game ends, the winning player's score is 306.
  85. Play the small crab in a game of Combat using the two decks you just dealt. What is the winning player's score?
  86. Your puzzle answer was 31308.
  87. --- Part Two ---
  88. 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.
  89. 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:
  90. Before either player deals a card, if there was a previous round in this game that had exactly the same cards in the same order in the same players' decks, the game instantly ends in a win for player 1. Previous rounds from other games are not considered. (This prevents infinite games of Recursive Combat, which everyone agrees is a bad idea.)
  91. Otherwise, this round's cards must be in a new configuration; the players begin the round by each drawing the top card of their deck as normal.
  92. If both players have at least as many cards remaining in their deck as the value of the card they just drew, the winner of the round is determined by playing a new game of Recursive Combat (see below).
  93. Otherwise, at least one player must not have enough cards left in their deck to recurse; the winner of the round is the player with the higher-value card.
  94. 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.
  95. Here is an example of a small game that would loop forever without the infinite game prevention rule:
  96. Player 1:
  97. 43
  98. 19
  99. Player 2:
  100. 2
  101. 29
  102. 14
  103. 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 3 card, and player 2 draws the 7 card, this would occur if player 1 has at least 3 cards left and player 2 has at least 7 cards left, not counting the 3 and 7 cards that were drawn.)
  104. 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 3 card, their deck in the sub-game would be copies of the next three cards in their deck.)
  105. Here is a complete example of gameplay, where Game 1 is the primary game of Recursive Combat:
  106. === Game 1 ===
  107. -- Round 1 (Game 1) --
  108. Player 1's deck: 9, 2, 6, 3, 1
  109. Player 2's deck: 5, 8, 4, 7, 10
  110. Player 1 plays: 9
  111. Player 2 plays: 5
  112. Player 1 wins round 1 of game 1!
  113. -- Round 2 (Game 1) --
  114. Player 1's deck: 2, 6, 3, 1, 9, 5
  115. Player 2's deck: 8, 4, 7, 10
  116. Player 1 plays: 2
  117. Player 2 plays: 8
  118. Player 2 wins round 2 of game 1!
  119. -- Round 3 (Game 1) --
  120. Player 1's deck: 6, 3, 1, 9, 5
  121. Player 2's deck: 4, 7, 10, 8, 2
  122. Player 1 plays: 6
  123. Player 2 plays: 4
  124. Player 1 wins round 3 of game 1!
  125. -- Round 4 (Game 1) --
  126. Player 1's deck: 3, 1, 9, 5, 6, 4
  127. Player 2's deck: 7, 10, 8, 2
  128. Player 1 plays: 3
  129. Player 2 plays: 7
  130. Player 2 wins round 4 of game 1!
  131. -- Round 5 (Game 1) --
  132. Player 1's deck: 1, 9, 5, 6, 4
  133. Player 2's deck: 10, 8, 2, 7, 3
  134. Player 1 plays: 1
  135. Player 2 plays: 10
  136. Player 2 wins round 5 of game 1!
  137. -- Round 6 (Game 1) --
  138. Player 1's deck: 9, 5, 6, 4
  139. Player 2's deck: 8, 2, 7, 3, 10, 1
  140. Player 1 plays: 9
  141. Player 2 plays: 8
  142. Player 1 wins round 6 of game 1!
  143. -- Round 7 (Game 1) --
  144. Player 1's deck: 5, 6, 4, 9, 8
  145. Player 2's deck: 2, 7, 3, 10, 1
  146. Player 1 plays: 5
  147. Player 2 plays: 2
  148. Player 1 wins round 7 of game 1!
  149. -- Round 8 (Game 1) --
  150. Player 1's deck: 6, 4, 9, 8, 5, 2
  151. Player 2's deck: 7, 3, 10, 1
  152. Player 1 plays: 6
  153. Player 2 plays: 7
  154. Player 2 wins round 8 of game 1!
  155. -- Round 9 (Game 1) --
  156. Player 1's deck: 4, 9, 8, 5, 2
  157. Player 2's deck: 3, 10, 1, 7, 6
  158. Player 1 plays: 4
  159. Player 2 plays: 3
  160. Playing a sub-game to determine the winner...
  161. === Game 2 ===
  162. -- Round 1 (Game 2) --
  163. Player 1's deck: 9, 8, 5, 2
  164. Player 2's deck: 10, 1, 7
  165. Player 1 plays: 9
  166. Player 2 plays: 10
  167. Player 2 wins round 1 of game 2!
  168. -- Round 2 (Game 2) --
  169. Player 1's deck: 8, 5, 2
  170. Player 2's deck: 1, 7, 10, 9
  171. Player 1 plays: 8
  172. Player 2 plays: 1
  173. Player 1 wins round 2 of game 2!
  174. -- Round 3 (Game 2) --
  175. Player 1's deck: 5, 2, 8, 1
  176. Player 2's deck: 7, 10, 9
  177. Player 1 plays: 5
  178. Player 2 plays: 7
  179. Player 2 wins round 3 of game 2!
  180. -- Round 4 (Game 2) --
  181. Player 1's deck: 2, 8, 1
  182. Player 2's deck: 10, 9, 7, 5
  183. Player 1 plays: 2
  184. Player 2 plays: 10
  185. Player 2 wins round 4 of game 2!
  186. -- Round 5 (Game 2) --
  187. Player 1's deck: 8, 1
  188. Player 2's deck: 9, 7, 5, 10, 2
  189. Player 1 plays: 8
  190. Player 2 plays: 9
  191. Player 2 wins round 5 of game 2!
  192. -- Round 6 (Game 2) --
  193. Player 1's deck: 1
  194. Player 2's deck: 7, 5, 10, 2, 9, 8
  195. Player 1 plays: 1
  196. Player 2 plays: 7
  197. Player 2 wins round 6 of game 2!
  198. The winner of game 2 is player 2!
  199. ...anyway, back to game 1.
  200. Player 2 wins round 9 of game 1!
  201. -- Round 10 (Game 1) --
  202. Player 1's deck: 9, 8, 5, 2
  203. Player 2's deck: 10, 1, 7, 6, 3, 4
  204. Player 1 plays: 9
  205. Player 2 plays: 10
  206. Player 2 wins round 10 of game 1!
  207. -- Round 11 (Game 1) --
  208. Player 1's deck: 8, 5, 2
  209. Player 2's deck: 1, 7, 6, 3, 4, 10, 9
  210. Player 1 plays: 8
  211. Player 2 plays: 1
  212. Player 1 wins round 11 of game 1!
  213. -- Round 12 (Game 1) --
  214. Player 1's deck: 5, 2, 8, 1
  215. Player 2's deck: 7, 6, 3, 4, 10, 9
  216. Player 1 plays: 5
  217. Player 2 plays: 7
  218. Player 2 wins round 12 of game 1!
  219. -- Round 13 (Game 1) --
  220. Player 1's deck: 2, 8, 1
  221. Player 2's deck: 6, 3, 4, 10, 9, 7, 5
  222. Player 1 plays: 2
  223. Player 2 plays: 6
  224. Playing a sub-game to determine the winner...
  225. === Game 3 ===
  226. -- Round 1 (Game 3) --
  227. Player 1's deck: 8, 1
  228. Player 2's deck: 3, 4, 10, 9, 7, 5
  229. Player 1 plays: 8
  230. Player 2 plays: 3
  231. Player 1 wins round 1 of game 3!
  232. -- Round 2 (Game 3) --
  233. Player 1's deck: 1, 8, 3
  234. Player 2's deck: 4, 10, 9, 7, 5
  235. Player 1 plays: 1
  236. Player 2 plays: 4
  237. Playing a sub-game to determine the winner...
  238. === Game 4 ===
  239. -- Round 1 (Game 4) --
  240. Player 1's deck: 8
  241. Player 2's deck: 10, 9, 7, 5
  242. Player 1 plays: 8
  243. Player 2 plays: 10
  244. Player 2 wins round 1 of game 4!
  245. The winner of game 4 is player 2!
  246. ...anyway, back to game 3.
  247. Player 2 wins round 2 of game 3!
  248. -- Round 3 (Game 3) --
  249. Player 1's deck: 8, 3
  250. Player 2's deck: 10, 9, 7, 5, 4, 1
  251. Player 1 plays: 8
  252. Player 2 plays: 10
  253. Player 2 wins round 3 of game 3!
  254. -- Round 4 (Game 3) --
  255. Player 1's deck: 3
  256. Player 2's deck: 9, 7, 5, 4, 1, 10, 8
  257. Player 1 plays: 3
  258. Player 2 plays: 9
  259. Player 2 wins round 4 of game 3!
  260. The winner of game 3 is player 2!
  261. ...anyway, back to game 1.
  262. Player 2 wins round 13 of game 1!
  263. -- Round 14 (Game 1) --
  264. Player 1's deck: 8, 1
  265. Player 2's deck: 3, 4, 10, 9, 7, 5, 6, 2
  266. Player 1 plays: 8
  267. Player 2 plays: 3
  268. Player 1 wins round 14 of game 1!
  269. -- Round 15 (Game 1) --
  270. Player 1's deck: 1, 8, 3
  271. Player 2's deck: 4, 10, 9, 7, 5, 6, 2
  272. Player 1 plays: 1
  273. Player 2 plays: 4
  274. Playing a sub-game to determine the winner...
  275. === Game 5 ===
  276. -- Round 1 (Game 5) --
  277. Player 1's deck: 8
  278. Player 2's deck: 10, 9, 7, 5
  279. Player 1 plays: 8
  280. Player 2 plays: 10
  281. Player 2 wins round 1 of game 5!
  282. The winner of game 5 is player 2!
  283. ...anyway, back to game 1.
  284. Player 2 wins round 15 of game 1!
  285. -- Round 16 (Game 1) --
  286. Player 1's deck: 8, 3
  287. Player 2's deck: 10, 9, 7, 5, 6, 2, 4, 1
  288. Player 1 plays: 8
  289. Player 2 plays: 10
  290. Player 2 wins round 16 of game 1!
  291. -- Round 17 (Game 1) --
  292. Player 1's deck: 3
  293. Player 2's deck: 9, 7, 5, 6, 2, 4, 1, 10, 8
  294. Player 1 plays: 3
  295. Player 2 plays: 9
  296. Player 2 wins round 17 of game 1!
  297. The winner of game 1 is player 2!
  298. == Post-game results ==
  299. Player 1's deck:
  300. Player 2's deck: 7, 5, 6, 2, 4, 1, 10, 8, 9, 3
  301. 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.
  302. 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?
  303. Your puzzle answer was 33647.