gpt4 book ai didi

prolog - 使用 Prolog 解决脑筋急转弯 (Master Mind)

转载 作者:行者123 更新时间:2023-12-04 01:29:47 25 4
gpt4 key购买 nike

一位同事与我们的 whatsapp 群组分享了这个:

This lock has a 3 digit code.
Can you guess it using only these hints?

If you had to solve this using Prolog how'd you do it?

我们使用类似于真值表的方法解决了这个问题。不过,我很好奇,这在 Prolog 中是如何解决的?

最佳答案

check 谓词的直接编码:

check( Solution, Guess, NValues, NPlaces ) :-
Solution = [A,B,C],
Guess = [X,Y,Z],
findall( t, (member(E, Guess), member(E, Solution)), Values ),
length( Values, NValues ),
( A=X -> V1 is 1 ; V1 is 0 ),
( B=Y -> V2 is 1+V1 ; V2 is V1 ),
( C=Z -> NPlaces is 1+V2 ; NPlaces is V2 ).

然后简单地抄录线索,不涉及创意:

puzzle( [A,B,C] ):-
findall( X, between(0,9,X), XS ),
select(A,XS,RA), select(B,RA,RB), member(C,RB),
/* "291": one digit is right and in its place
"245": one digit is right but in the wrong place
"463": two digits are right but both are in the wrong place
"578": all digits are wrong
"569": one digit is right but in the wrong place */
check( [A,B,C], [2,9,1], 1, 1 ),
check( [A,B,C], [2,4,5], 1, 0 ),
check( [A,B,C], [4,6,3], 2, 0 ),
check( [A,B,C], [5,7,8], 0, 0 ),
check( [A,B,C], [5,6,9], 1, 0 ).

运行它:

<i>23 ?- time( puzzle(X) ).
/* 13,931 inferences, 0.000 CPU in 0.000 seconds (?% CPU, Infinite Lips) */
X = [3, 9, 4] <b>;</b>
/* 20,671 inferences, 0.000 CPU in 0.000 seconds (?% CPU, Infinite Lips) */
<b>false.</b></i>

关于prolog - 使用 Prolog 解决脑筋急转弯 (Master Mind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61276283/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com