16

# 挑战

## 输入值

``````Joe: Both I am a knight and neither Steve is a knave nor I am a knave.
Steve: Joe is a knave. Either Joe is a knight or I am a knight.
``````

## 解析中

``````both [clause] and [clause]
either [clause] or [clause]
neither [clause] nor [clause]
[I am | (other person's name) is] a [knight | knave]
``````

``````Both I am a knight and neither Steve is a knave nor I am a knave.
``````

``````(I am a knight) AND ((Steve is a knave) NOR (I am a knave))
``````

## 输出量

``````Joe is the knave.
Steve is the knight.
``````

# 更多例子

``````Sir Lancelot: Either both I am a knight and Merlin is a knave or both I am a knave and Merlin is a knight.
Merlin: Either both I am a knight and Sir Lancelot is a knight or both I am a knave and Sir Lancelot is a knave.
``````

``````Sir Lancelot is the knight.
Merlin is the knave.
``````

``````David: Neither I am a knave nor Patrick is a knight. Either I am a knight or Patrick is a knave.
Patrick: Either I am a knight or both I am a knight and David is a knight.
``````

``````David is the knave.
Patrick is the knight.
``````

``````Lizard: I am a knight.
Spock: I am a knave.
``````

``````Rock Paper Scissors
``````

## 规则，条例和说明

1. 适用标准守则的高尔夫球规则
2. 您的程序只能由可打印的ASCII组成
3. 所有输入和输出将来自STDIN和STDOUT

ugoren

PhiNotPi'4

ugoren

6

## Python，491个字符

``````import sys
def p():
a=s.pop(0)
try:return{"both":"(%s*%s)","either":"(%s|%s)","neither":"(1-(%s|%s))"}[a.lower()]%(p(),p())
except KeyError:r=s[2];del s[:4];return"(%s==%s)"%((a,m)[a=="I"],r)
x=[];w=[]
for l in sys.stdin:
m,l=l.split(":");w+=[m]
for s in l.split("."):
s=s.split()
while s:x+=["%s==%s"%(m,p())]
k=("knave","knight")
r=[a for a in[{w[0]:z,w[1]:1-z}for z in(0,1)]if all(eval(l,{k[0]:0,k[1]:1},a)for l in x)]
print"\n".join(x+" is the "+k[r[0][x]]+"."for x in w+r[1:])
``````

3

### Ruby，352个字符

``````q=->{gets.split /: |\.\s/}
C,*E=q[]
D,*F=q[]
r=->c,m{t=c.shift;t[1]?(x=r[c,m];c.shift;y=r[c,m];t[0]==?B?x&y :t[0]==?E?x^y :1-(x|y)):(c.shift(2);h=c.shift[2]==?I?m : 1-m;t==?I?h :1-h)}
s=->u,v,b{u.map{|c|r[c.gsub(v,?J).upcase.split,b]==b}.all?}
t=->b{s[E,D,b]&&s[F,C,1-b]}
u=->v,b{v+" is the kn#{b ?:ight: :ave}."}
puts t[1]^t[0]&&u[C,t[1]]+\$/+u[D,t[0]]``````

PhiNotPi'4

1
@PhiNotPi完成。是零字符修复吗？
Howard

0

# Perl-483字节

``````((\$a,\$b),(\$c,\$d))=map{split':'}@ARGV;\$h='y';\$i='x';\$s=' is the kn';\$g='ight.';\$v='ave.';for(\$b,\$d){\$_.=' 1';s/ am a | is a /==/g;s/knight/1)/g;s/knave/0)/g;s/I=/(\\$\$i=/g;s/(\$a|\$c)=/(\\$\$h=/g;s/([^.]+)\./(\$1)and/g;s/ or / xor /g;s/ nor / or /g;while(s/(?<= )(\w+ \((?:[^()]+|(?1))\) \w+ \((?:[^()]+|(?1))\))/(\$1)/g){}s/neither/!/gi;s/both|either//gi;\$h=\$i++}\$x=0;\$y=1;\$k=!eval(\$b)&&eval(\$d);\$x=\$y--;\$n=!eval(\$d)&&eval(\$b);print"\$a\$s\$v
\$c\$s\$g"if(\$k&&!\$n);print"\$a\$s\$g
\$c\$s\$v"if(\$n&&!\$k)
``````