/* Magic hexagon in Picat. Port of Markus Triska's Prolog code: https://github.com/triska/clpz/blob/master/magic_hexagon.pl """ Magic Hexagon of order 3, solved with CLP(FD) constraints Written 2006 by Markus Triska triska@metalevel.at Public domain code. Place the integers 1,...,19 in the following grid so that the sum of all numbers in a straight line (there are lines of length 3, 4 and 5) is equal to 38: A B C D E F G H I J K L M N O P Q R S """ Picat> magic_hexagon(Vs), labeling([ff],Vs),println(Vs),fail [3,17,18,19,7,1,11,16,2,5,6,9,12,4,8,14,10,13,15] [3,19,16,17,7,2,12,18,1,5,4,10,11,6,8,13,9,14,15] [9,11,18,14,6,1,17,15,8,5,7,3,13,4,2,19,10,12,16] [9,14,15,11,6,8,13,18,1,5,4,10,17,7,2,12,3,19,16] [10,12,16,13,4,2,19,15,8,5,7,3,14,6,1,17,9,11,18] [10,13,15,12,4,8,14,16,2,5,6,9,19,7,1,11,3,17,18] [15,13,10,14,8,4,12,9,6,5,2,16,11,1,7,19,18,17,3] [15,14,9,13,8,6,11,10,4,5,1,18,12,2,7,17,16,19,3] [16,12,10,19,2,4,13,3,7,5,8,15,17,1,6,14,18,11,9] [16,19,3,12,2,7,17,10,4,5,1,18,13,8,6,11,15,14,9] [18,11,9,17,1,6,14,3,7,5,8,15,19,2,4,13,16,12,10] [18,17,3,11,1,7,19,9,6,5,2,16,14,8,4,12,15,13,10] This model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import v3_utils. import cp. main => go. go ?=> magic_hexagon(Vs), labeling([ff,split], Vs), println(Vs), fail, nl. go => true. % Aliases label(Vs) :- solve(Vs). labeling(Opt,Vs) :- solve(Opt,Vs). % From https://github.com/triska/clpz/blob/master/magic_hexagon.pl sum38(Vs) :- 38 #= sum(Vs). magic_hexagon(Vs) :- Vs = [A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S], Vs :: 1..19, all_different(Vs), maplist(sum38, [[A,B,C], [D,E,F,G], [H,I,J,K,L], [M,N,O,P], [Q,R,S], [H,D,A], [M,I,E,B], [Q,N,J,F,C], [R,O,K,G], [S,P,L], [C,G,L], [B,F,K,P], [A,E,J,O,S], [D,I,N,R], [H,M,Q]]). %?- magic_hexagon(Vs), labeling([ff], Vs). %@ Vs = [3, 17, 18, 19, 7, 1, 11, 16, 2, 5, 6, 9, 12, 4, 8, 14, 10, 13, 15] ; %@ Vs = [3, 19, 16, 17, 7, 2, 12, 18, 1, 5, 4, 10, 11, 6, 8, 13, 9, 14, 15] . %@ etc.