得分异步囚徒困境游戏


15

囚徒困境练习的一轮中,两名选手各自决定在该轮中是合作还是背叛。一回合的得分是:

  • 玩家A和玩家B都合作:双方都得1分
  • 玩家A和玩家B都缺憾:双方都得2分
  • 玩家A配合而玩家B缺陷:与玩家A配合时3分,而与玩家B背离时0分

但是,您不必担心策略:您的程序仅是在计算游戏得分。(如果您已经熟悉了囚徒的困境,那么我在这里的“观点”相当于“入狱年限”。)

您的挑战是要获得代表球员在多个回合中的选择的输入并计算他们各自的总得分。一个球员在小写的提交选择,c以及d(对于合作缺陷),并以大写其他的提交选择,CD。这些选择作为字符串提供给您的程序。

通常,处于囚徒困境中的玩家会同时并反复提交动作。但是,在此挑战中,玩家可能一次提交了好几个回合的选择。如果某个球员的举动不合顺序,则计分程序会记住该举动,并将其与对方球员的下一个可用举动进行匹配。

这是一个示例输入字符串:

cDCddDDCcCc

为了显示此输入中存在的匹配项,我将分别调用小写和大写字母,并将它们配对:

cDCddDDCcCc
c  dd   c c => cddcc
 DC  DDC C  => DCDDCC

这些将成对配对:

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)

产生分数9(小写)到6(大写)的分数,因此输出应为9,6(或任何明确的定界符)。

为了以另一种方式表达它,以下是每个配对在其自己的行中拔出的:

cDCddDDCcCc
cD
  Cd
    dD
      D c
       C  c

有一个无与伦比的C,因为大写玩家提交的动作比小写玩家提交的动作更多。这是可以接受的,并且出于评分目的而被完全忽略。

要求如下:

  • 您必须编写/[cdCD]+/通过某种输入机制(STDIN,函数自变量,从文件读取等)接受正则表达式形式的字符串的程序或函数。(您的程序可以选择接受带有尾随换行符的输入。)

  • 您的程序或函数必须以字符串形式输出或返回玩家的分数。输出格式必须以小写玩家的分数开始,然后是大写玩家的分数,并由您选择的任何非空,非数字分隔符分隔。(尾随换行符是可选的。)

  • 如果一个玩家的动作多于另一个,则多余的动作将被忽略。

  • 如果输入中的所有移动都完全来自一个玩家(也就是说,根本没有进行过任何回合),则每个玩家的得分为0

  • 以字节为单位的最小提交数获胜。

测试用例

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

他们通常不会在合作中获得2分,如果双方都有缺陷,则会失去1分吗?
Eumel

1
@Eumel我只是复制了Wikipedia简介中的规范,该规范似乎使用了原始作者建议的格式。还要注意,这里的要点是“不好的”,因为它们对应于入狱多年。获胜者是得分最少的玩家。
apsillers

(0,0)还是[0,0]可以输出?
xnor 2015年

Answers:


3

Pyth,23个字节

jsMc2/L`C,@Gz-zG"cDDCdd

测试套件


说明:

@Gz: 小写字母

-zG: 大写字母

C,:配对,截断其余部分。

`:采用成对列表的字符串表示形式

/L ... "cDDCdd:对于中的每个字母"cDDCdd",计算在上面的字符串repr中出现的时间。

c2:将结果列表切成两半。

sM:每半加起来。

j:加入换行符并打印。


必须使用`代替s,以使一方无法参加工作。


5

Haskell,139134字节

g=filter
(n!m)(a,b)=(a+n,b+m)
f s=init$tail$show$foldr id(0,0)$zipWith(#)(g(>'a')s)$g(<'E')s
'c'# 'C'=1!1
'c'#_=3!0
_# 'D'=2!2
_#_=0!3

用例:f "cDCddDDCcCc"- > "9,6"

15个字节只是为了获得正确的输出格式,即将一对数字(x,y)变成一个字符串"x,y"

怎么运行的:

               g(>'a')s        -- extract all lowercase letters
                     g(<'E')s  -- extract all uppercase letters
         zipWith(#)            -- combine both lists element wise with function #
                               -- # calls ! depending on the combination of c/d/C/D
                               -- ! takes 2 numbers a and b and returns a function
                               -- that takes a pair (x,y) and returns (x+a,y+b)
                               -- now we have a list of such functions
    foldr id(0,0)              -- apply those functions starting with (0,0)
init$tail$show                 -- format output                    

编辑:@Zgarb帮助我节省了5个字节。谢谢!


4

LabVIEW,77字节

在此处输入图片说明

该代码从令牌中扫描并使用这些标记来确定要去的位置。

计数是这样这样


3

Python 3、110

感谢FryAmTheEggman,节省了5个字节。
由于使用了开膛手,节省了7个字节。
DSM节省了26个字节。

x=[[],[]]
a=b=0
for m in input():x[m<'E']+=m
for w,p in zip(*x):d=p>'C';c=w<'d';b+=d*2+c;a+=3-d-2*c
print(b,a)

我认为一切都终于结束了。

它扫描输入中的每个字符并根据是否为大写对它进行排序。然后,它做一些花哨的数学运算,滥用Python将布尔值转换为整数的隐式转换。


2

的JavaScript(ES6),124个 118字节

s=>(A=B=i=0,U=(r=x=>s.replace(/c|d/g,x))``,r(l=>U[i]&&(U[i++]<'D'?l<'d'?++A&++B:B+=3:l<'d'?A+=3:(A+=2,B+=2))),A+','+B)

现场演示

(为便于阅读,略有扩展。)

var f=function (s) {
    A=B=i=0;
    U=(r=function(x){return s.replace(/c|d/g,x)})("");
    r(l=>U[i]&&(U[i++]<'D'?l<'d'?++A&++B:B+=3:l<'d'?A+=3:(A+=2,B+=2)));
    return A+','+B;
}

var input = ["cDCddDDCcCc","cccDDD","DDDDDDccc","cDcDcD","dcDDC","CcdCDDcd","Ddd","ccccccccccc"];
var output = ["9,6","9,0","9,0","9,0","5,2","6,6","2,2","0,0"];
var passed = true;

for (var index=0;index<input.length;index++) {
    if (f(input[index]) !== output[index]) passed = false;
}

document.getElementById("result").textContent = 
  passed ? "All tests passed." : "Some tests failed.";
<div id="result"></div>

由于user81655节省了6个字节。


我最初有数组理解,但最终使用了另一种方法。谢谢。
intrepidcoder

1

Par,49字节

(lW▼·L)w▼·U))t˅y])[h7%Z2*↓″4>5*-]z2↔-″0<4*+╞)t.Σ¡

每个字符使用一个字节。看这里

说明

(              ## Construct array
 l             ## Read line
 W             ## Assign to w
 ▼·L)          ## Filter by immutable under lower-case
 w             ## Get w
 ▼·U)          ## Filter by immutable under upper-case
)              ## 
t              ## Transpose and truncate
˅y])           ## If empty, empty 2-D matrix
[              ## Map
 h             ## Decimal to hex
 7%            ## Modulo 7
 Z             ## Assign to z
 2*↓″4>5*-     ## Score of lower case
 ]             ## Put in array
 z2↔-″0<4*+    ## Score of upper case
 ╞             ## Add to array
)              ## 
t              ## Transpose and truncate
.Σ             ## Map - sum
¡              ## Empty array onto stack

输出形式为9 6


作为从未使用过(或从未听说过)Par的人,我发现您的解释很有趣。谢谢!
a药

1

CJam,92 83 81字节

结果比我想象的要长。

0]K*X3tC30tG22tZ11t:L;0'a]q+{'D>}:B$_{B}%1#/z{,1>},{2<[:i:#K%L=]sY0e[{si}%}%:.+S*

在这里尝试。

说明(我敢解释吗?:O):

0]K*C3tX30tG22tZ11t:L;    e# Creates this array [0,30,0,11,0,0,0,0,0,0,0,0,3,0,0,0,22,0,0,0]
0'a]q+                    e# Creates an array that looks like [0, 'a', input string]
{'D>}:B$                  e# Sorts the array by if the int representation of each element is greater than the int value of the character 'D' (e.g. [0,C,D,a,c,d])
_{B}%1#/                  e# Finds the index of the first value in the array that is > 'D' and splits the array at that index.
z{,1>},{                  e# Zip the two sub arrays and filter for only sub arrays with more than one element. (e.g [[0,a],[C,c],[D,d]])
{2<[:i:#K%L=]s            e# For each sub array, take the first two elements, convert each to an it, calculate n=(x[0]^x[1]) mod 20, and get the nth element in the very first array, and convert it to a string
Y0e[                      e# Pad the string with 0 so it is length 2. (e.g. [["00"],["22"],["11"]])
{si}%}%:.+                e# get the numerical representation of each digit and dot sum all of them (e.g [[0,0],[2,2],[1,1] => [3,3])
S*                        e# Join with a space (e.g "3 3")
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.