/* No consecutive numbers in adjacent nodes in Picat. From Adrian Groza: "Modelling Puzzles in First Order Logic" and https://www.researchgate.net/publication/374588335_Measuring_reasoning_capabilities_of_ChatGPT """ Puzzle 28. No consecutive numbers in adjacent nodes Place numbers 1 through 8 on eight nodes a, b, c, d, e, f , g, h so that: 1. Each number appears exactly once; 2. No connected nodes have consecutive numbers Node a is connected with b, g and f . Node b is connected with a, g, h and c. Node c is connected with b, g, h and d. Node d is connected with c, h and f . Node e is connected with d, g, h and f . Node g is connected with a, b, c, h, f , e and d. Node h is connected with d, b, c, g, f , e and d. How many solutions exist b - c / | X | \ a - g - h - d \ | X | / f - e """ This is a port of Groza's Mace4 encoding: https://users.utcluj.ro/~agroza/puzzles/maloga/all_code/ch_03_practical_puzzles/consecutivenumbers.in Four solutions: [2,5,3,7,4,6,8,1] [2,6,4,7,3,5,8,1] [7,3,5,2,6,4,1,8] [7,4,6,2,5,3,1,8] Cf: - http://hakank.org/picat/place_number_puzzle.pi - http://hakank.org/picat/place_number_puzzle2.pi This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go ?=> X = [A,B,C,D,E,F,G,H], X :: 1..8, all_different(X), abs(A-B) #!= 1, abs(A-G) #!= 1, abs(A-F) #!= 1, abs(B-C) #!= 1, abs(B-H) #!= 1, abs(B-G) #!= 1, abs(C-D) #!= 1, abs(C-H) #!= 1, abs(C-G) #!= 1, abs(D-H) #!= 1, abs(D-E) #!= 1, abs(E-F) #!= 1, abs(E-G) #!= 1, abs(E-H) #!= 1, abs(F-G) #!= 1, abs(F-H) #!= 1, abs(G-H) #!= 1, solve(X), println(X), fail, nl. go => true. % % A variant encoding the nodes. % Same solutions: % [2,5,3,7,4,6,8,1] % [2,6,4,7,3,5,8,1] % [7,3,5,2,6,4,1,8] % [7,4,6,2,5,3,1,8] % go2 => X = [A,B,C,D,E,F,G,H], X :: 1..8, all_different(X), neibs([A,B,C,D,E,F,G,H],L), foreach($P-Q in L) abs(P-Q) #!= 1 end, solve(X), println(X), fail, nl. neibs([A,B,C,D,E,F,G,H],L) => L = $[A-B,A-G,A-F, B-C,B-H,B-G, C-D,C-H,C-G, D-H,D-E, E-F,E-G,E-H, F-G,F-H, G-H].