piles. Two players take turns, and the first one moves first. On a turn a player picks any non-empty pile and removes as many stones from it as they like, but at least one. A player who cannot move loses.
Both play perfectly. Return True if the first player wins and False if the second one does.
Searching over all positions fits the limits here, but there is also a linear-time solution.piles=[4] → True
The first player takes the whole pile at once, and the second has no move.
piles=[3, 3] → False
The second player simply mirrors every move in the other pile. The piles stay equal, so the second player makes the last move.
piles=[2, 5, 1] → True
The first player takes 2 stones from the pile of 5, leaving [2, 3, 1]; from there the second player loses whatever they do.
piles=[4]
True
Run checks the first 3 tests in your browser. Submit checks all 80 tests on the server, hidden ones included.