루아 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