/* Logic equation in Picat. From Adrian Groza "Modelling Puzzles in First Order Logic" Puzzles 1..4. See descriptions below. Here's a generalization of these puzzles. This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. /* puzzle = 1 [3,2,1,4] puzzle = 2 [2,1,4,3] puzzle = 3 [2,1,5,4,3] puzzle = 4 [7,9,8,5,4,1,3,2,6] */ go ?=> puzzle(Puzzle,X,Eqs), nl, println(puzzle=Puzzle), all_different(X), foreach(T in Eqs) T end, solve(X), println(X), fail, nl. go => true. /* """ Puzzle 1. Logic equation In this 4 x 4 logic equation, you have to find unique integer values for the variables A, B, C, D-ranging from 1 to 4-to make the following statements true: 2 x C = B and 4 x C = D. (puzzle taken from Brainzilla—www.brainzilla.com) """ */ puzzle(1,X,Eqs) :- N = 4, X = new_list(N), X = [_A,B,C,D], X :: 1..N, Eqs = $[2*C #= B,4*C #= D]. /* """ Puzzle 2. Logic equation In this 4 x 4 logic equation, you have to find unique integer values for the variables A, B, C, D-ranging from 1 to 4-to make all the following statements true: (puzzle taken from Brainzilla—www.brainzilla.com) """ */ puzzle(2,X,Eqs) :- N = 4, X = new_list(N), X = [A,B,_C,D], X :: 1..N, Eqs = $[B+D #= A+2, A+D #= B+4]. /* """ Puzzle 3. Logic equation In this 5 x 5 logic equation, you have to find unique integer values for the variables A, B, C, D, E - ranging from 1 to 5 - to make all statements true: (puzzle taken from Brainzilla—www.brainzilla.com) C = A + E E = B + 2 B ∗ E + 3 ∗ E != B -> A ∗ A + D > E """ */ puzzle(3,X,Eqs) :- N = 5, X = new_list(N), X = [A,B,C,D,E], X :: 1..N, Eqs = $[C #= A + E, E #= B + 2, B * E + 3 * E #!= B #=> A * A + D #> E]. /* """ Puzzle 4. Logic equation Find distinct values for the variables ranging from 1 to 9 to make all statements true. (puzzle taken from Brainzilla-www.brainzilla.com) 4 ∗ F = E (2.2) I = G ∗ H (2.3) C + E != 12 -> G = D + E + 2 (2.4) B = A + H (2.5) A + G != C (2.6) """ */ puzzle(4,X,Eqs) :- N = 9, X = new_list(N), X = [A,B,C,D,E,F,G,H,I], X :: 1..N, Eqs = $[4 * F #= E, I #= G * H, C + E #!= 12 #=> G #= D + E + 2, B #= A + H, A + G #!= C ].