ルア5.1(502)
最小化されたバージョン:
s,t,b,c,i,q,a,d,f="ABCDFGHJYUEGKSHNCOLPQIEJUSNC","METHINKS IT IS LIKE A WEASEL",1,math.random,table.insert,1,string.sub,100,28 while q~=f do r,p={},{} for x=1,d do i(r,s) i(p,0) e="" for o=1,f do if c(1,20)==1 then if c(1,27)==1 then e=e.." " else e=e..string.char(c(65,90)) end else e=e..a(r[x],o,o) end end r[x]=e for y=1,f do if a(r[x],y,y)==a(t,y,y) then p[x]=p[x]+1 end end if p[x]==f then s=r[x] end end for x=1,d do if p[x]>=q then s,q=r[x],p[x] end end print(b..":",s,"-- score: "..q) b=b+1 end
読みやすいバージョン(コメント付き!):
s,t,b,c,i,q,a,d,f="ABCDFGHJYUEGKSHNCOLPQIEJUSNC","METHINKS IT IS LIKE A WEASEL",1,math.random,table.insert,1,string.sub,100,28
--s=random string, t=target, b=counter, c=reference to math.random, i=reference to table.insert, q=top score,a=reference to string.sub, d=constant (100), f=constant (28)
while q~=f do
r,p={},{}
for x=1,d do --add 100 copies to the table of strings
i(r,s)
i(p,0)
e=""
for o=1,f do --for each character in string
if c(1,20)==1 then -- 5% chance
if c(1,27)==1 then e=e.." " else e=e..string.char(c(65,90)) end --set it to an ASCII char between 65 and 90 (A-Z) or a space character
else
e=e..a(r[x],o,o)
end
end
r[x]=e --current string = mutations
for y=1,f do
if a(r[x],y,y)==a(t,y,y) then p[x]=p[x]+1 end
end --for each char increment score if it is correct
if p[x]==f then
s=r[x]
end --if 28 then final string is this!
end
for x=1,d do
if p[x]>=q then s,q=r[x],p[x] end --if this is the highest score so far, then make the string equal to this
end
print(b..":",s,"-- score: "..q) --print it!
b=b+1 --add one to the counter!
end
正直に言うと、これは絶対に勝てませんが、この問題の合理的に短い解決策を見つけて最小化できただけです!(合理的に強調):p