Skip to content

Instantly share code, notes, and snippets.

@Kuniwak
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kuniwak/b0d5bfccbc3c87c953b9 to your computer and use it in GitHub Desktop.
Save Kuniwak/b0d5bfccbc3c87c953b9 to your computer and use it in GitHub Desktop.
count(List, X, Count) :- count_sub(List, X, 0, Count).
count_sub([], _, N, N).
count_sub([Head | Xs], X, N, Count) :-
(Head == X -> N1 is N + 1; N1 is N),
count_sub(Xs, X, N1, Count).
% トランプの数は1〜13まで。
card(X) :- between(1, 13, X).
% さやかのヒントにあてはまる。
is_satisfy_sayaka_said(CardA, CardB) :-
card(CardA),
card(CardB),
CardA =\= 1,
CardB =\= 1,
CardA =\= CardB,
CardB mod CardA =:= 0.
% 相手の数字を確定できる。
can_guess(WinnerCard, AllCandidates) :-
flatten(AllCandidates, AllCandidateCards),
card(WinnerCard),
count(AllCandidateCards, WinnerCard, 1).
% どちらのカードからも、相手の数字を確定できない。
cannot_eiter_guess(CardA, CardB, AllCandidates) :-
card(CardA), card(CardB),
not(can_guess(CardA, AllCandidates)),
not(can_guess(CardB, AllCandidates)).
% カードの組は、解候補に含まれる。
is_candidate(CardA, CardB, AllCandidates) :-
member([CardA, CardB], AllCandidates);
member([CardB, CardA], AllCandidates).
% 以下の条件を満たす解である。
% (1) さやかのヒントにあてはまる
% (2) どちらも相手の数字を確定できない
% (3) (2)を知ると、どちらかが相手の数字を確定できる
is_answer(WinnerCard, LoserCard) :-
findall([CardA, CardB], (
is_satisfy_sayaka_said(CardA, CardB)
), CandidatesSatisfySayakaSaid),
findall([CardA, CardB], (
% (1)
is_satisfy_sayaka_said(CardA, CardB),
% (2)
cannot_eiter_guess(CardA, CardB, CandidatesSatisfySayakaSaid)
), CandidatesWhenEitherCannotGuess),
% (3)
can_guess(WinnerCard, CandidatesWhenEitherCannotGuess),
is_candidate(WinnerCard, LoserCard, CandidatesWhenEitherCannotGuess).
% Result %%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% ?- is_answer(CardA, CardB, Minami).
% CardA = Minami, Minami = 10,
% CardB = 2 ;
% false.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment