In a round of the prisoner's dilemma exercise, two players each decide whether to cooperate or defect for that round. The scoring for a round is:
- Player A and Player B both cooperate: 1 point for both
- Player A and Player B both defect: 2 points for both
- Player A cooperates and Player B defects: 3 points for cooperating Player A and 0 points for defecting Player B
You don't need to worry about strategy, though: your program will merely be tabulating the score for a game. (In case you are already familiar with prisoner's dilemma, my "points" here correspond to "years in prison.")
Your challenge is to take input that represents the players' choices over several rounds and compute their respective total scores. One player submits choices in lowercase,
d (for cooperate and defect), and the other submits choices in uppercase,
D. These choices are supplied to your program as a string.
Normally, players in prisoner's dilemma submit their moves simultaneously and iteratively. In this challenge, however, the players may have submitted their choices for several rounds at once. If a player's move is out of sequence, the scoring program remembers it and matches it with the next available move from the opposing player.
Here's a sample input string:
To show the matches that exist in this input, I'll call out the lowercase and uppercase separately, and pair them up:
cDCddDDCcCc c dd c c => cddcc DC DDC C => DCDDCC
These will be paired into the rounds:
c vs D (3 pts for lowercase-player, 0 pts for uppercase-player) d vs C (0 pts for lowercase-player, 3 pts for uppercase-player) d vs D (2 pts for both) c vs D (3 pts for lowercase-player, 0 pts for uppercase-player) c vs C (1 pt for both)
Which produces the score
9 (lowercase) to
6 (uppercase), so the output should be
9,6 (or any unambiguous delimiter).
To express it in yet another way, here's each pairing pulled out on its own row:
cDCddDDCcCc cD Cd dD D c C c
There is one unmatched
C, because the uppercase player submitted more moves than the lowercase player. That is acceptable, and it is totally ignored for scoring purposes.
Here are the requirements:
You must write a program or function that accepts a string of the regular-expression form
/[cdCD]+/, through some input mechanism (STDIN, function argument, read from file, etc.). (Your program may optionally accept input with a trailing newline.)
Your program or function must output or return the players' scores as a string. The output format must start with the lowercase player's score, followed by the uppercase player's score, separated by any non-empty, non-numeric delimiter of your choice. (A trailing newline is optional.)
If one player has more moves than the other, the excess moves are ignored.
If all moves in the input are from exclusively one player (that is, no rounds have been played at all), then each player's score is
The smallest submission in bytes wins.
Input: cDCddDDCcCc Output: 9,6 -- or any delimiter; I chose commas here Input: cccDDD Output: 9,0 Input: DDDDDDccc Output: 9,0 Input: cDcDcD Output: 9,0 Input: dcDDC Output: 5,2 Input: CcdCDDcd Output: 6,6 Input: Ddd Output: 2,2 Input: ccccccccccc Output: 0,0