├── crime.pl ├── cryptarth.pl ├── factorial.pl ├── hello.pl ├── mapcoloring.pl ├── queens.pl ├── revabcde.pl ├── strangesquares.pl ├── sudoku.pl └── threecoins.pl /crime.pl: -------------------------------------------------------------------------------- 1 | % To discover who killed Mr. Boddy, you need to learn where each person was, and what weapon was in the room. Clues are scattered throughout the quiz (you cannot solve question 1 until all 10 are read). 2 | 3 | 4 | % 1. To begin, you need to know the suspects. There are three men (George, John, Robert) and three women (Barbara, Christine, Yolanda). Each person was in a different room (Bathroom, Dining Room, Kitchen, Living Room, Pantry, Study). A suspected weapon was found in each room (Bag, Firearm, Gas, Knife, Poison, Rope). 5 | % Who was found in the kitchen? 6 | 7 | man(george). man(john). man(robert). 8 | woman(barbara). woman(christine). woman(yolanda). 9 | person(X):- man(X). 10 | person(X):- woman(X). 11 | 12 | uniq_ppl(A,B,C,D,E,F):- person(A), person(B), person(C), person(D), person(E), person(F), \+A=B, \+A=C, \+A=D, \+A=E, \+A=F, \+B=C, \+B=D, \+B=E, \+B=F, \+C=D, \+C=E, \+C=F, \+D=E, \+D=F, \+E=F. 13 | 14 | writeanswers(Bathroom, Dining, Kitchen, Livingroom, Pantry, Study, Bag, Firearm, Gas, Knife, Poison, Rope):- 15 | write("Bathroom: "), write(Bathroom), nl, 16 | write("Dining: "), write(Dining), nl, 17 | write("Livingroom: "), write(Livingroom), nl, 18 | write("Pantry: "), write(Pantry), nl, 19 | write("Study: "), write(Study), nl, 20 | write("Kitchen: "), write(Kitchen), nl, 21 | 22 | write("Knife: "), write(Knife), nl, 23 | write("Gas: "), write(Gas), nl, 24 | write("Rope: "), write(Rope), nl, 25 | write("Bag: "), write(Bag), nl, 26 | write("Poison: "), write(Poison), nl, 27 | write("Firearm: "), write(Firearm), nl. 28 | 29 | % 2. Clue 1: The man in the kitchen was not found with the rope, knife, or bag. 30 | % Which weapon, then, which was not the firearm, was found in the kitchen? 31 | 32 | clue1(Bathroom, Dining, Kitchen, Livingroom, Pantry, Study, Bag, Firearm, Gas, Knife, Poison, Rope) :- 33 | man(Kitchen), 34 | \+Kitchen=Rope, \+Kitchen=Knife, \+Kitchen=Bag, \+Kitchen=Firearm. 35 | 36 | % % 3. Clue 2: Barbara was either in the study or the bathroom; Yolanda was in the other. 37 | % % Which room was Barbara found in? 38 | 39 | clue2(Bathroom, Dining, Kitchen, Livingroom, Pantry, Study, Bag, Firearm, Gas, Knife, Poison, Rope) :- 40 | barbara=Bathroom, yolanda=Study. 41 | clue2(Bathroom, Dining, Kitchen, Livingroom, Pantry, Study, Bag, Firearm, Gas, Knife, Poison, Rope) :- 42 | barbara=Study, yolanda=Bathroom. 43 | 44 | % % 4. Clue 3: The person with the bag, who was not Barbara nor George, was not in the bathroom nor the dining room. 45 | % % Who had the bag in the room with them? 46 | 47 | clue3(Bathroom, Dining, Kitchen, Livingroom, Pantry, Study, Bag, Firearm, Gas, Knife, Poison, Rope) :- 48 | \+barbara=Bag, \+george=Bag, \+Bag=Bathroom, \+Bag=Dining. 49 | 50 | % % 5. Clue 4: The woman with the rope was found in the study. 51 | % % Who had the rope? 52 | 53 | clue4(Bathroom, Dining, Kitchen, Livingroom, Pantry, Study, Bag, Firearm, Gas, Knife, Poison, Rope) :- 54 | woman(Rope), Rope=Study. 55 | 56 | % % 6. Clue 5: The weapon in the living room was found with either John or George. 57 | % % What weapon was in the living room? 58 | clue5(Bathroom, Dining, Kitchen, Livingroom, Pantry, Study, Bag, Firearm, Gas, Knife, Poison, Rope) :- 59 | Livingroom = john. 60 | clue5(Bathroom, Dining, Kitchen, Livingroom, Pantry, Study, Bag, Firearm, Gas, Knife, Poison, Rope) :- 61 | Livingroom = george. 62 | 63 | % % 7. Clue 6: The knife was not in the dining room. 64 | % % So where was the knife? 65 | clue6(Bathroom, Dining, Kitchen, Livingroom, Pantry, Study, Bag, Firearm, Gas, Knife, Poison, Rope) :- 66 | \+Knife=Dining. 67 | 68 | % % 8. Clue 7: Yolanda was not with the weapon found in the study nor the pantry. 69 | % % What weapon was found with Yolanda? 70 | clue7(Bathroom, Dining, Kitchen, Livingroom, Pantry, Study, Bag, Firearm, Gas, Knife, Poison, Rope) :- 71 | \+yolanda=Pantry, \+yolanda=Study. 72 | 73 | % % 9. Clue 8: The firearm was in the room with George. 74 | % % In which room was the firearm found? 75 | clue8(Bathroom, Dining, Kitchen, Livingroom, Pantry, Study, Bag, Firearm, Gas, Knife, Poison, Rope) :- 76 | Firearm=george. 77 | 78 | % % 10. It was discovered that Mr. Boddy was gassed in the pantry. The suspect found in that room was the murderer. 79 | % % Who, then, do you point the finger towards? 80 | murderer(X) :- 81 | uniq_ppl(Bathroom, Dining, Kitchen, Livingroom, Pantry, Study), 82 | uniq_ppl(Bag, Firearm, Gas, Knife, Poison, Rope), 83 | clue1(Bathroom, Dining, Kitchen, Livingroom, Pantry, Study, Bag, Firearm, Gas, Knife, Poison, Rope), 84 | clue2(Bathroom, Dining, Kitchen, Livingroom, Pantry, Study, Bag, Firearm, Gas, Knife, Poison, Rope), 85 | clue3(Bathroom, Dining, Kitchen, Livingroom, Pantry, Study, Bag, Firearm, Gas, Knife, Poison, Rope), 86 | clue4(Bathroom, Dining, Kitchen, Livingroom, Pantry, Study, Bag, Firearm, Gas, Knife, Poison, Rope), 87 | clue5(Bathroom, Dining, Kitchen, Livingroom, Pantry, Study, Bag, Firearm, Gas, Knife, Poison, Rope), 88 | clue6(Bathroom, Dining, Kitchen, Livingroom, Pantry, Study, Bag, Firearm, Gas, Knife, Poison, Rope), 89 | clue7(Bathroom, Dining, Kitchen, Livingroom, Pantry, Study, Bag, Firearm, Gas, Knife, Poison, Rope), 90 | clue8(Bathroom, Dining, Kitchen, Livingroom, Pantry, Study, Bag, Firearm, Gas, Knife, Poison, Rope), 91 | X = Gas, X = Pantry, 92 | 93 | write("KILLER IS :"), write(X), nl, 94 | writeanswers(Bathroom, Dining, Kitchen, Livingroom, Pantry, Study, Bag, Firearm, Gas, Knife, Poison, Rope). 95 | -------------------------------------------------------------------------------- /cryptarth.pl: -------------------------------------------------------------------------------- 1 | % SEND 2 | % MORE + 3 | %--------- 4 | % MONEY 5 | 6 | %find distinct values for characters to add up to MONEY. 7 | 8 | dig(0). dig(1). dig(2). dig(3). dig(4). 9 | dig(5). dig(6). dig(7). dig(8). dig(9). 10 | 11 | uniq_d(S,E,N,D,M,O,R,Y) :- 12 | dig(S), dig(E), dig(N), dig(D), dig(M), dig(O), dig(R), dig(Y), 13 | \+ S=E, \+ S=N, \+ S=D, \+ S=M, \+ S=O, \+ S=R, \+ S=Y, 14 | \+ E=N, \+ E=D, \+ E=M, \+ E=O, \+ E=R, \+ E=Y, 15 | \+ N=D, \+ N=M, \+ N=O, \+ N=R, \+ N=Y, 16 | \+ D=M, \+ D=O, \+ D=R, \+ D=Y, 17 | \+ M=O, \+ M=R, \+ M=Y, 18 | \+ O=R, \+ O=Y, 19 | \+ R=Y. 20 | 21 | solve(S, E, N, D, M, O, R, Y) :- 22 | %assert they are uniq digits. 23 | uniq_d(S, E, N, D, M, O, R, Y), S>0, M>0, 24 | Y is (D+E) mod 10, C1 is (D+E)// 10, % Y = D+E (integer part), C1 holds the carry to add to N+R 25 | E is (N+R+C1) mod 10, C2 is (N+R+C1) // 10, 26 | N is (E+O+C2) mod 10, C3 is (E+O+C2) // 10, 27 | O is (S+M+C3) mod 10, C4 is (S+M+C3) // 10, 28 | M is C4. 29 | 30 | 31 | -------------------------------------------------------------------------------- /factorial.pl: -------------------------------------------------------------------------------- 1 | factorial(1, 1). 2 | factorial(N, Fact) :- N>0, N1 is N-1, factorial(N1, FactSub), Res is N*FactSub). 3 | -------------------------------------------------------------------------------- /hello.pl: -------------------------------------------------------------------------------- 1 | friend(john, julia). 2 | friend(john, jack). 3 | friend(julia, sam). 4 | friend(julia, molly). 5 | 6 | loves(john, julia). 7 | loves(julia, sam). 8 | loves(sam, julia). 9 | 10 | 11 | 12 | male(brad). 13 | male(john). 14 | male(jim). 15 | male(alfred). 16 | female(marry). 17 | child(brad, alfred). 18 | child(john, jim). 19 | child(john, marry). 20 | 21 | parent(X, Y) :- child(Y,X). 22 | father(X, Y) :- child(Y,X), male(X). 23 | mother(X, Y) :- child(Y,X), female(X). 24 | friend(X, Y) :- friend(Y,X). 25 | friendzoned(X) :- loves(X, Y), \+ loves(Y,X). 26 | -------------------------------------------------------------------------------- /mapcoloring.pl: -------------------------------------------------------------------------------- 1 | %http://www.cpp.edu/~jrfisher/www/prolog_tutorial/2_1.html 2 | 3 | color(red). 4 | color(green). 5 | color(blue). 6 | 7 | colorify(A,B,C,D,E) :- 8 | color(A), color(B), color(C), color(D), color(E), 9 | \+ A=B, \+ A=C, \+ A=D, \+ A=E, 10 | \+ B=C, \+ C=D, \+ D=E. 11 | 12 | 13 | -------------------------------------------------------------------------------- /queens.pl: -------------------------------------------------------------------------------- 1 | % The eight queens problem 2 | % place 8 queens on 8x8 chessboard 3 | % with constraints: 4 | % queens don't capture each other. 5 | % queen can capture in its 6 | % row 7 | % column 8 | % left, right diag. 9 | % right diag (row+col are the same) 10 | % left diag (row-col are the same) 11 | 12 | col(1). col(2). col(3). col(4). 13 | col(5). col(6). col(7). col(8). 14 | 15 | 16 | cap(R,_,R,_). %captures on the same row. 17 | cap(_,C,_,C). %captures on the same col. 18 | cap(R1,C1, R2,C2) :- R1-C1 =:= R2-C2. %left diag. 19 | cap(R1,C1, R2,C2) :- R1+C1 =:= R2+C2. %right diag. 20 | 21 | 22 | solve(C1,C2,C3,C4,C5,C6,C7,C8) :- 23 | col(C1), %R1 queen 24 | col(C2), \+ cap(2,C2, 1,C1), 25 | col(C3), \+ cap(3,C3, 1,C1), \+ cap(3,C3, 2,C2), 26 | col(C4), \+ cap(4,C4, 1,C1), \+ cap(4,C4, 2,C2), \+ cap(4,C4,3,C3), 27 | col(C5), \+ cap(5,C5, 1,C1), \+ cap(5,C5, 2,C2), \+ cap(5,C5,3,C3), \+ cap(5,C5,4,C4), 28 | col(C6), \+ cap(6,C6, 1,C1), \+ cap(6,C6, 2,C2), \+ cap(6,C6,3,C3), \+ cap(6,C6,4,C4), \+ cap(6,C6,5,C5), 29 | col(C7), \+ cap(7,C7, 1,C1), \+ cap(7,C7, 2,C2), \+ cap(7,C7,3,C3), \+ cap(7,C7,4,C4), \+ cap(7,C7,5,C5), cap(7,C7,6,C6), 30 | col(C8), \+ cap(8,C8, 1,C1), \+ cap(8,C8, 2,C2), \+ cap(8,C8,3,C3), \+ cap(8,C8,4,C4), \+ cap(8,C8,5,C5), cap(8,C8,6,C6),cap(8,C8,7,C7). 31 | -------------------------------------------------------------------------------- /revabcde.pl: -------------------------------------------------------------------------------- 1 | %% Solve ABCDE * 4 = EDCBA 2 | %% WHERE A, B, C, D, E are unique ints from 0 to 9 3 | 4 | %% number to string. 5 | n2s(N,S):- atom_number(S, N). 6 | 7 | %% join list of digits as a string. 8 | concatenate(NumsList, OutString):- maplist(n2s, NumsList, CharsList), atom_codes(OutString, CharsList). 9 | %% CONSTRAINTS 10 | %% DEFINE WHAT IS A NUMBER. 11 | num(0). num(1). num(2). num(3). num(4). num(5). num(6). num(7). num(8). num(9). 12 | 13 | %% UNIQUENESS. 14 | uniq(A,B,C,D,E) :- num(A), num(B), num(C), num(D), num(E), \+ A=B, \+ A=C, \+ A=D, \+ A=E, \+ B=C, \+ B=D, \+ B=E, \+ C=D, \+ C=E, \+ D=E. 15 | 16 | %% LEFT HAND SIDE OF EQUATION ABCDE * 4 17 | lhs(A,B,C,D,E, Res):- concatenate([A,B,C,D,E],Rescon), atom_number(Rescon, Res1), Res is Res1*4. 18 | 19 | %% RIGHT HAND SIDE OF EQUATION EDCBA 20 | rhs(A,B,C,D,E, Res) :- 21 | concatenate([E,D,C,B,A], Rescon), atom_number(Rescon, Res1), Res is Res1. 22 | 23 | %% SOLVE USING CONSTRAINTS (A,B,C,D,E uniq numbers) and LHS (ABCDE*4) = RHS (EDCBA). 24 | solve(A,B,C,D,E):- 25 | uniq(A,B,C,D,E), lhs(A,B,C,D,E,Res1), rhs(A,B,C,D,E,Res2), Res1 = Res2, write(Res1), string_to_list(Res1, LRes1), reverse(LRes1, RevL), atom_chars(RevS, RevL), write(' '), write(RevS). 26 | -------------------------------------------------------------------------------- /strangesquares.pl: -------------------------------------------------------------------------------- 1 | %% STRANGE SQUARES. 2 | %%% 45 -> 2025 is strange because 4+5 = 2+0+2+5. 3 | 4 | solve(X, Y):- 5 | between(10, 99, X), 6 | Y is X*X, 7 | Y >= 1000, 8 | Y // 100 + Y mod 100 =:= X. 9 | -------------------------------------------------------------------------------- /sudoku.pl: -------------------------------------------------------------------------------- 1 | % based on thinking as computation example 2 | 3 | %domain 4 | num(1). num(2). 5 | num(3). num(4). 6 | 7 | 8 | showspace :- write(' '). 9 | %row representation. 10 | showrow(A, B, C, D) :- 11 | showspace, write(A), showspace, write(B), showspace, write(C), showspace, write(D), nl. 12 | 13 | %create unique values of A,B,C,D based. 14 | uniq(A,B,C,D) :- 15 | num(A), num(B), num(C), num(D), \+ A=B, \+ A=C, \+ A=D, \+ B=C, \+ B=D, \+ C=D. 16 | 17 | %find solution to sudoku 4x4 based on constraints. 18 | solution(C11, C12, C13, C14, 19 | C21, C22, C23, C24, 20 | C31, C32, C33, C34, 21 | C41, C42, C43, C44) :- 22 | %rows (every row is uniq). 23 | uniq(C11, C12, C13, C14), uniq(C21, C22, C23, C24), uniq(C31, C32, C33, C34), uniq(C41, C42, C43, C44), 24 | %columns (every column is uniq). 25 | uniq(C11, C21, C31, C41), uniq(C12, C22, C32, C42), uniq(C13, C23, C33, C43), uniq(C14, C24, C34, C44), 26 | %blocks (every block (2x2) cells is uniq). 27 | uniq(C11, C12, C21, C22), uniq(C31, C32, C41, C42), uniq(C13, C14, C23, C24), uniq(C33, C34, C43, C44). 28 | 29 | 30 | solve(C11, C12, C13, C14, 31 | C21, C22, C23, C24, 32 | C31, C32, C33, C34, 33 | C41, C42, C43, C44) :- 34 | 35 | solution(C11, C12, C13, C14, 36 | C21, C22, C23, C24, 37 | C31, C32, C33, C34, 38 | C41, C42, C43, C44), 39 | showrow(C11, C12, C13, C14), showrow(C21, C22, C23, C24), showrow(C31, C32, C33, C34), showrow(C41, C42, C43, C44),nl. 40 | 41 | 42 | -------------------------------------------------------------------------------- /threecoins.pl: -------------------------------------------------------------------------------- 1 | % 3 coins H H T. 2 | % make them all HHH or TTT with in 3 moves. 3 | % a move is changing H to T or changing T to H. 4 | 5 | init_state([h,h,t]). 6 | 7 | goal_state([h,h,h]). 8 | goal_state([t,t,t]). 9 | 10 | 11 | flip(h,t). %flip head -> tail. 12 | flip(t,h). %flip tail -> head. 13 | 14 | legal_move([X,Y,Z], flip_left, [X1, Y,Z]) :- flip(X,X1). 15 | legal_move([X,Y,Z], flip_middle, [X, Y1,Z]) :- flip(Y,Y1). 16 | legal_move([X,Y,Z], flip_right, [X, Y,Z1]) :- flip(Z,Z1). 17 | 18 | plan(L):- init_state(I), goal_state(G), reachable(I,L,G). 19 | 20 | %stay in S if we got no moves. 21 | reachable(S,[],S). 22 | 23 | %if we can reach S2 from S1 by M, and we can reach S3 from S2 by L 24 | reachable(S1,[M|L], S3) :- legal_move(S1,M,S2), reachable(S2,L,S3). 25 | 26 | 27 | --------------------------------------------------------------------------------