プロジェクトオイラー問59

http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2059
Problem 59 「XOR暗号解読」 †
全部試してよく使われる単語Theとthisを探す。
なぜか処理が異常に遅い。

isThe("the ",1):-!.
isThe(_,0):-!.

isThis("this",1):-!.
isThis(_,0). 

bad(0,_):-!.
bad(_,0):-!.

check([_,_,_],_,Count1,Count2,_):-bad(Count1,Count2),!,fail.
check([A,B,C],[X,Y,Z,_],_,_,Ans):-
	!,
	Ans1 is Ans+(A xor X)+(B xor Y)+(C xor Z),
	write(Ans1).
check([A,B,C,D|Rest],[X,Y,Z,X],CountA,CountB,Ans):-
 	A1 is A xor X,
 	B1 is B xor Y,
	C1 is C xor Z,
	D1 is D xor X,
	Ans1 is Ans+A1,
	isThe([A1,B1,C1,D1],Add1),
	isThat([A1,B1,C1,D1],Add2),
	CountA1 is CountA+Add1,
 	CountB1 is CountB+Add2,
	check([B,C,D|Rest],[Y,Z,X,Y],CountA1,CountB1,Ans1).


main:-
	open("pe59.txt",read,Is),
	read(Is,R),
 	close(Is),
	between(97,122,X),
	between(97,122,Y),
	between(97,122,Z),
	check(R,[X,Y,Z,X],0,0,0).
最終更新:2014年12月10日 03:07