Project Euler Problem79

久しぶりにProject Eulerやってみました。
制約論理プログラミングで解きました。ほとんど頭使ってない…
SWI-Prologです。

Problem79

:-use_module(library(clpfd)).

solve(A,Len):-
	length(A,Len),
	A ins 0..9,
	choice(A,[3,1,9],IdxLst1,Len),
	choice(A,[6,8,0],IdxLst2,Len),
	choice(A,[1,8,0],IdxLst3,Len),
	choice(A,[6,9,0],IdxLst4,Len),
	choice(A,[1,2,9],IdxLst5,Len),
	choice(A,[6,2,0],IdxLst6,Len),
	choice(A,[7,6,2],IdxLst7,Len),
	choice(A,[6,8,9],IdxLst8,Len),
	choice(A,[7,6,2],IdxLst9,Len),
	choice(A,[3,1,8],IdxLst10,Len),
	choice(A,[3,6,8],IdxLst11,Len),
	choice(A,[7,1,0],IdxLst12,Len),
	choice(A,[7,2,0],IdxLst13,Len),
	choice(A,[7,1,0],IdxLst14,Len),
	choice(A,[6,2,9],IdxLst15,Len),
	choice(A,[1,6,8],IdxLst16,Len),
	choice(A,[1,6,0],IdxLst17,Len),
	choice(A,[6,8,9],IdxLst18,Len),
	choice(A,[7,1,6],IdxLst19,Len),
	choice(A,[7,3,1],IdxLst20,Len),
	choice(A,[7,3,6],IdxLst21,Len),
	choice(A,[7,2,9],IdxLst22,Len),
	choice(A,[3,1,6],IdxLst23,Len),
	choice(A,[7,2,9],IdxLst24,Len),
	choice(A,[7,2,9],IdxLst25,Len),
	choice(A,[7,1,0],IdxLst26,Len),
	choice(A,[7,6,9],IdxLst27,Len),
	choice(A,[2,9,0],IdxLst28,Len),
	choice(A,[7,1,9],IdxLst29,Len),
	choice(A,[6,8,0],IdxLst30,Len),
	choice(A,[3,1,8],IdxLst31,Len),
	choice(A,[3,8,9],IdxLst32,Len),
	choice(A,[1,6,2],IdxLst33,Len),
	choice(A,[2,8,9],IdxLst34,Len),
	choice(A,[1,6,2],IdxLst35,Len),
	choice(A,[7,1,8],IdxLst36,Len),
	choice(A,[7,2,9],IdxLst37,Len),
	choice(A,[3,1,9],IdxLst38,Len),
	choice(A,[7,9,0],IdxLst39,Len),
	choice(A,[6,8,0],IdxLst40,Len),
	choice(A,[8,9,0],IdxLst41,Len),
	choice(A,[3,6,2],IdxLst42,Len),
	choice(A,[3,1,9],IdxLst43,Len),
	choice(A,[7,6,0],IdxLst44,Len),
	choice(A,[3,1,6],IdxLst45,Len),
	choice(A,[7,2,9],IdxLst46,Len),
	choice(A,[3,8,0],IdxLst47,Len),
	choice(A,[3,1,9],IdxLst48,Len),
	choice(A,[7,2,8],IdxLst49,Len),
	choice(A,[7,1,6],IdxLst50,Len),
	label(A).


choice(A,[P1,P2,P3],[N1,N2,N3],Cnt):-
	[N1,N2,N3] ins 1..Cnt,
	element(N1,A,P1),
	element(N2,A,P2),
	element(N3,A,P3),
	N1 #< N2,
	N2 #< N3.

回答伏せます

コメント

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です