Reaper's Game Nim with a split move, fifteen rounds live

misc
flag RxR{4beb88b6-f5ee-421a-9c66-0855db939d48}

A live TCP service, 451 points. The banner describes a Nim variant: on your turn, either take any number of stones from one heap, or split one heap into two non-empty heaps; take the last stone to win a round. Win 15 rounds back to back, moving first, against a Reaper that "never misplays." Lose one and the session ends.

Insight

The split move is what makes this interesting. Plain Nim is a solved XOR game; here you can also create heaps, so the reachable heap sizes aren't just "smaller" ones.

Working out the value of a heap

Moves act on one heap at a time and the position is a disjoint sum of heaps - impartial game, so one Sprague-Grundy number per heap size is enough:

output
G(n) = mex( { G(n-c) : 1 <= c <= n }            # take c stones
          U { G(a) ^ G(n-a) : a + n-a = n } )   # split into two non-empty heaps

The take move alone reproduces plain Nim (G(n)=n); the split move layers G(a)^G(b) pairs on top. Computing it up to a few thousand settles into a tidy closed form:

output
G(n) = n, except pairs (3,4), (7,8), (11,12), ... are swapped
     = n + 1  if n % 4 == 3
     = n - 1  if n % 4 == 0

Almost Nim, shifted at every 4k boundary. The win condition follows immediately: XOR the G of every heap; if nonzero, move so it becomes zero, and the opponent is always the one facing a lost position.

Two things the wire taught me

Pitfall

Heap indexing. The server prints Heaps: 20 3 15 11 with no indices, and guessing wrong about 0- vs 1-based is fatal - an out-of-range heap counts as illegal and ends the run. Since each connection starts a fresh Round 1, I burned one throwaway connection on a deliberate probe: 1 1. Under 0-based this hits the second heap; under 1-based, the first. The echo (and the Reaper's own "removes 13 from heap 3" matching the fourth entry) confirmed 0-based.

A round is not a move. My first pass assumed one move per round and looped 15 times. Every move was a genuine winning move - but 15 moves only gets through about four rounds, because a round lasts as long as there are stones left to remove. The fix: stop counting moves and just play until the flag appears, feeding whatever heaps the server hands back into the same move finder every turn.

Result

solve_final.py recomputes the winning move from the current heaps every single turn, never losing the XOR invariant:

output
Flawless. The Reaper bows.
RxR{4beb88b6-f5ee-421a-9c66-0855db939d48}

solve.py holds the reusable pieces (Grundy table, move finder, socket client, a probe subcommand for the indexing question); solve_final.py is the end-to-end run, raw.log its full transcript.

Flag

output
RxR{4beb88b6-f5ee-421a-9c66-0855db939d48}