1:Nim
-----

Nim: finitely many piles of coins; a move comprises removing a positive number
of coins from a single pile; a player loses if they can't move.

Remark:
    For any nim position P, either it can be won by the player with the move,
    or it can be won by the player without the move.

    i.e. one of the two players has a "winning strategy", a way to play which
    guarantees a win.

The "nim sum", n (+) m, of natural numbers n and m is the result of writing
the binary exapansions of n and m and "adding without carrying". (In computer
science, this is called "XORing the bitstrings"; in many programming
languages, it's written as "n^m".)

Theorem:
    The player without the move can win from the Nim position with piles of
    sizes n_1,...,n_k
    iff n_1 (+) n_2 (+) ... (+) n_k = 0
Proof:
    Suppose inductively that this is true for all nim positions with fewer
    coins involved.

    First, suppose
	n_1 (+) n_2 (+) ... (+) n_k = b != 0.
    We show that we can win if we have the move.

    Consider binary expansions,
    some n_i has a 1 in the same position as the leading 1 of b,
    so
	n_i (+) b < n_i .

    So we move by taking coins from the ith pile to leave n_i (+) b.

    Then in the new position, the nim sum of the pile sizes is
	n_1 (+) ... (+) n_{i-1} (+) n_i (+) b (+) n_{i+1} (+) ... (+) n_k
	    = b (+) b
	    = 0

    So by the induction hypothesis, the player without the move wins from
    here. But that's us!

    Now suppose 
	n_1 (+) n_2 (+) ... (+) n_k = 0
    and we don't have the move.

    If our opponent can't move, we've won.
    Else, suppose they move by taking coins from the ith pile, leaving m < n_i.
    But then m (+) n_i != 0, so
    n_1 (+) ... (+) m (+) ... (+) n_k != n_1 (+) ... (+) n_i (+) ... n_k = 0,
    so by induction we're left with a position won by the player with the
    move, which is us.


Bonus I: Hackenbush
-------

(Green) Hackenbush: dots joined by lines, some dots are "on the
ground"; a move comprises deleting a line, and then deleting all lines which
are no longer connected to the ground.

*n := snake of length n.

P+Q := [picture where we draw P and Q side by side, with no connections
    between them]

(so a Nim game is a sum of snakes)

Proposition:
    Every Hackenbush picture is equivalent to a snake:
	given a picture P, there is an n >= 0 such that given any Hackenbush
	picture Q, the picture P+Q has the same winner as *n+Q.

    Moreover, we have a simple recursive definition of n:
	n is the least number such that no picture obtained by making a single
	move in P is equivalent to *n.
Sketch Proof (see abstract version below for a cleaner proof):
    Suppose inductively that the proposition holds for all pictures smaller
    than P.

    Let Q be another picture. Then the player who wins *n+Q can win P+Q as
    follows: if ever our winning strategy for *n+Q has us play in *n, we
    play the corresponding move in P. While our opponent moves in Q, we make
    the moves from our winning strategy for *n+Q. If ever the opponent plays
    in P, then: let P' be the resulting picture, so the move is from P+Q' to
    P'+Q' say. If P' is equivalent to *m with m < n, we treat it as if they
    made the corresponding move in *n; else, P' is equivalent to *m with
    m > n, so there's a move in P' to a picture P'' equivalent to *n; so move
    to that, and we win since we win *n+Q', since we were using our winning
    strategy up to this point.


So here's how to win hackenbush (when we can):
    on your turn, make a move such that the resulting picture is equivalent to
    a sum of snakes of lengths which have nim sum 0. If you don't have any such
    move, you'll lose if your opponent is using this strategy!


Bonus II: abstract impartial games and the Sprague-Grundy theorem
--------

An _impartial game_ is a two player game where, like in nim but unlike in
chess, the moves available to a player on their turn don't depend on whose
turn it is, and where victory is defined by saying that a player loses if it's
their turn to move but they can't move.

For impartial games G and H, G+H is the game where you can choose on your turn
either to play in G or to play in H.

Any nim position is an impartial game. Write *n for the nim position with a
single pile of size n.

So e.g. *2 + *2 + *3 is the nim position with two piles of size 2 and one of
size 3.

In particular, *0 is the "zero game" (also just written 0): whoever's turn it
is to move loses.

We consider a position in a game as a game in itself, so a move is a move from
one game to another.

e.g. a (green) Hackenbush picture is an impartial game.

We refer to the player with the move as the "first player", and their opponent
as the "second player". So after a move, the first player in the new game was
the second player in the original game, and vice versa.

An impartial game is _finite_ if there's maximum number of moves that can be
made before the game ends. Call this maximum the _length_ of the game.

Lemma:
    Any finite impartial game is either a "first player win" or a "second
    player win".
Proof:
    Suppose inductively that this is true for all shorter games.

    If there's a move available which leads to a game which is a second player
    win, then that's a winning move.

    Otherwise, it's a second player win, because whatever the first move is
    leaves that player as first player in a first player win game.

Definition:
    Two games G and H are _equivalent_, G = H,
    if for any game K,
    G+K is a first-player win iff H+K is.

Remark:
    G+H = H+G
    (G+H)+K = G+(H+K)
    *0+G = G
    if G = H then G+K = H+K for any K.

Lemma:
    G = *0 iff G is a second player win.
Proof:
    *0 is a second player win, so left to right is immediate.

    Suppose G is a second player win, and K is any game. Then G+K is won by
    the same player as K. Indeed, given a winning strategy for K for one of
    the players, and a winning strategy for G for the second player, the
    following is a winning strategy for G+K:
	On your turn, play the next move in the winning strategy for K, unless
	your opponent just played in G - then play the next move in the
	winning strategy for G.

Example:
    G+G = *0

    Indeed, the second player wins by mirroring any move made in one copy of G
    in the other copy of G.

Lemma:
    G+H = *0 iff G = H
Proof:
    If G=H, then G+H = G+G = *0 by the above example.

    If G+H = *0, then by adding H to both sides, we see
	G = G + *0 = G+H+H = *0 + H = H.

Our winning strategy for nim tells us how to add nim games:

Proposition:
    *n + *m = *( n (+) m )
Proof:
    n (+) m (+) ( n (+) m ) = 0,
    so as we saw above,
    *n + *m + *( n (+) m ) is a second-player win.

Now we can prove a general version of the reduction of green Hackenbush to
Nim:

Theorem [Sprague-Grundy]:
    Any finite impartial game G is equivalent to a nim pile.

    Specifically, G = *n where n is the smallest non-negative number such that
    there's no move from G to a game equivalent to *n.
Proof:
    A winning strategy in G + *n for the second player:
	Response to a move in *n, say to *m: mirror in G by making a move in G
	    to a game equivalent to *m. This results in a game equivalent to
	    *m + *m = *0.
	Response to a move in G:
	    by induction, the game moved to in G is equivalent to some *m;
	    so we have to win as first player in *m + *n,
	    but by definition of n, m != n, so *m + *n != *0,
	    so *m + *n is indeed a first player win. So follow the winning
	    strategy.

Further reading: Berlekamp, Conway, and Guy, "Winning Ways for your
    Mathematical Plays, vol 1"