How to play the duck game
This is a 2-player game.
First, we start with a line of blue rubber ducks (represented here as circles):
🔵🔵🔵🔵🔵🔵🔵
Now, in each turn, the current player can turn either one blue duck or two adjacent blue ducks red.
So, a valid move would be indexes 2 and 3 (0-indexed):
🔵🔵🔴🔴🔵🔵🔵
Then, the other player could choose index 0:
🔴🔵🔴🔴🔵🔵🔵
And so on. The last player to move loses.
Example game
Starting with 7 blue ducks:
Initial board
🔵🔵🔵🔵🔵🔵🔵
Player 1 chooses 3,4
🔵🔵🔵🔴🔴🔵🔵
Player 2 chooses 0
🔴🔵🔵🔴🔴🔵🔵
Player 1 chooses 6
🔴🔵🔵🔴🔴🔵🔴
Player 2 chooses 1,2
🔴🔴🔴🔴🔴🔵🔴
Player 1 chooses 5
🔴🔴🔴🔴🔴🔴🔴
Player 1 was the last one to move, so Player 2 wins.
Challenge
The duck game can be solved. Your challenge today is to accept a board state and output one of the moves which will guarantee a win for the current player.
For example, with the board state 🔵🔵🔵🔴🔴🔵🔵
, choosing either index 0 or index 2 will guarantee a win for the current player (feel free to try it).
Input/Output
You should take in a list/array/etc. containing two distinct values representing blue and red ducks.
For example,🔵🔵🔵🔴🔴🔵🔵
could be[1, 1, 1, 0, 0, 1, 1]
.You should output one or two integers representing the ducks which should be taken in the next move.
For example, the input🔵🔵🔵🔴🔴🔵🔵
could have output[0]
or[2]
.
Clarifications
- The input board state will always have at least one move which guarantees a win for the current player.
- You may output any of the moves which guarantee a win.
- This is code-golf, so shortest solution in bytes wins.
Test cases
These test cases use 1 for blue ducks and 0 for red ducks. The output side contains all possible outputs.
Input -> Output
[1, 1, 1, 0, 0, 1, 1] -> [0], [2]
[1, 1, 1, 1, 1, 1, 1] -> [1], [3], [5]
[1, 1, 0, 1, 1, 1, 1] -> [0,1], [3,4], [5,6]
[1, 0, 1, 1, 0, 1, 1] -> [0]
[1, 1, 1] -> [0,1], [1,2]
[1, 0, 0, 1, 1, 0, 0, 1] -> [3], [4]