The "sequential" game
A number of people stand in a line with their hands hidden in a pre-chosen state of either Rock ✊, Paper ✋ or Scissors ✌️. Here's an example line:
Initial Line |
---|
✌️ ✋ ✊ ✋ ✊ ✌️ ✌️ ✊ ✌️ ✋ ✊ |
The game goes like this: at each turn, the leftmost player "challenges" the next one: the one who wins the Rock Paper Scissors challenge stays in the line (and becomes/remains the leftmost player), the other is eliminated. Tying equals to a win for the challenger (or for the challenged, it doesn't really matter).
This repeats until only one player is left, who wins the game.
Let's see how this unfolds, while keeping track of the eliminated players (in order of elimination).
Current challenge | Eliminated players | Updated line |
---|---|---|
✌️ ✋ ✊ ✋ ✊ ✌️ ✌️ ✊ ✌️ ✋ ✊ (start) | ||
✌️>✋ | +✋ | ✌️ ✊ ✋ ✊ ✌️ ✌️ ✊ ✌️ ✋ ✊ |
✌️<✊ | ✋ +✌️ | ✊ ✋ ✊ ✌️ ✌️ ✊ ✌️ ✋ ✊ |
✊<✋ | ✋✌️ +✊ | ✋ ✊ ✌️ ✌️ ✊ ✌️ ✋ ✊ |
✋>✊ | ✋✌️✊ +✊ | ✋ ✌️ ✌️ ✊ ✌️ ✋ ✊ |
✋<✌️ | ✋✌️✊✊ +✋ | ✌️ ✌️ ✊ ✌️ ✋ ✊ |
✌️=✌️ | ✋✌️✊✊✋ +✌️ | ✌️ ✊ ✌️ ✋ ✊ |
✌️<✊ | ✋✌️✊✊✋✌️ +✌️ | ✊ ✌️ ✋ ✊ |
✊>✌️ | ✋✌️✊✊✋✌️✌️ +✌️ | ✊ ✋ ✊ |
✊<✋ | ✋✌️✊✊✋✌️✌️✌️ +✊ | ✋ ✊ |
✋>✊ | ✋✌️✊✊✋✌️✌️✌️✊ +✊ | ✋ (winner!) |
Your challenge
Your challenge is to write the shortest code (function or entire program) that
Takes as input a line formed of at least two players.
Outputs
- the winner, and
- the sequence of all other players in order (or reverse order) of elimination
Input and output can be in any of the commonly accepted formats (standard input/output, function arguments, return values, printouts, file I/O, ...).
The two outputs can be in any order, as long as it's consistent. They can also be clumped together (just define upfront if the winner is the first or last element)
Choose your own representation of Rock, Paper and Scissors. It can be strings of "R", "P", "S", sequences of these characters, strings of emojis, arrays of integers (0, 1, 2) or (-1, 0, 1), ...
Examples
PP -> P, P
RS -> R, S
RSPPR -> P, SRPR
PSPPRSSP -> P, PPPSSSR
RSPPRSRPSR -> R, SRPRPSRPS
SPRPRSSRSPR -> P, PSRRPSSSRR # (this is the one from the above breakdown)
PSSPSRRRRSP -> P, PSPSSRRRSR
RSPRRPPRPRRS -> S, SRRRPPRPRRP
PRPSSRPRRSRPRR -> P, RPPSSRRRPSRRR
RSRRPRSRPRSRPSSR -> R, SRRRRPSRRPSRPSS
SPSSRRSSRSSSRPSSS -> S, PSSSRSSRSSSRRPSS
SPSSPPPRSSPRPSSPSPP -> S, PSSPPPSSSRRPPSPSPP
( Reverse order is also acceptable
as long as it's consistent:
SPSSPPPRSSPRPSSPSPP -> S, PPSPSPPRRSSSPPPSSP
SPSSPPPRSSPRPSSPSPP -> PSSPPPSSSRRPPSPSPP, S )
SPSSPPPRSSPRPSSPSPP -> PPSPSPPRRSSSPPPSSP, S )
```