day 22
crab combat
approach
the two decks are deques, so drawing from the top and adding to the bottom are both cheap. part 1 plays plain rounds until one deck is empty, and the score weights each card by its position counted from the bottom.
part 2 is the recursive game. every round, the current pair of decks is turned into a tuple and checked against a set of states already seen in this game; a repeat ends the game in player 1's favour. if both players have at least as many cards left as the values they drew, the round is decided by a sub-game on copies of the top cards (islice into new deques); otherwise the higher card wins as before.
the trick
remember every deck state per game to cut off loops, and recurse on copied slices so sub-games can't touch the parent decks.
python
if len(p1) >= p1c and len(p2) >= p2c:
p1s = qslice(p1, 0, p1c)
p2s = qslice(p2, 0, p2c)
winner, _ = recursive_combat(p1s, p2s)
else:
winner = 1 if p1c > p2c else 2