LoginSignup
1
1

More than 5 years have passed since last update.

かなり古い論理パズル

Posted at

Qiita内を逍遥していましたら、論理パズルをrubyで解かれていました。
論理といえばprologですので、挑戦してみました。
問題->https://twitter.com/puzzlegiver_bot/status/220508689981902849/photo/1
rubyの解->https://qiita.com/nidouchi/items/92f351731858ddbba931

cal([],N,N).
cal([H|T],N,R):-H=_:G,(G==t->N1 is N+1;N1=N),cal(T,N1,R).

res:-L=[red:R,blue:B,yellow:Y,brown:BR,green:G,violet:V,black:BL],maplist(g,L),
      (R==V->R==f;R==t),(Y==G->B==f;B==t),(Y==G->Y==t;Y==f),(BR==Y->BR==t;BR==f),
      (V==BR->BL==f;BL==t),cal(L,0,N),(N==3->G==t;G==f),
      disp(L,[],[]),!.

disp([],T,F):-write("honest"),write(T),write("liar"),write(F).
disp([H|L],T,F):-H=P:G,(G==t->(T1=[P|T],F1=F);(T1=T,F1=[P|F])),disp(L,T1,F1).

g(_:t).
g(_:f).

%res.

登場人物(とその状態)を並べてそのあとに条件を連ねれば、バックトラックのおかげで解が出ます。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1