/* Fort Garrisons puzzle in Picat. From Adrian Groza "Modelling Puzzles in First Order Logic" """ Puzzle 91. Fort Garrisons Here we have a system of fortifications. It will be seen that there are ten forts, con- nected by lines of outworks, and the numbers represent the strength of the small gar- risons. The General wants to dispose these garrisons afresh so that there shall be 100 men in each of the five lines of the four forts. Can you show how it can be done? The garrisons must be moved bodily—that is to say, you are not allowed to break them up into other numbers. (puzzle 397 from Dudeney 2016) """ a1 a2 b2 b1 a5 b3 b5 b4 a3 a4 The allowed values are 16,18,20,22,24,26,28,28,32,36 Note: It's two occurrences of 28. There are 24 solutions (with the symmetry breaking of A1 = 28). Here are some of them in a nicer output: [28,20,18,16,22,32,26,28,36,24] 28 20 26 32 22 28 24 36 18 16 [28,20,18,32,36,16,28,26,22,24] 28 20 28 16 36 26 24 22 18 32 This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. /* Show all solutions. */ go => p([A1,A2,A3,A4,A5, B1,B2,B3,B4,B5]), println([A1,A2,A3,A4,A5, B1,B2,B3,B4,B5]), printf(" %2d\n", A1), nl, printf(" %2d %2d %2d %2d\n",A2,B2,B1,A5), nl, printf(" %2d %2d\n", B3,B5), nl, printf(" %2d\n",B4), nl, printf(" %2d %2d\n",A3,A4), nl, fail, nl. /* Number of solutions with and without symmetry breaking (A1 #= 28): symmetry_breaking = 24 no_symmetry_breaking = 120 There are other symmetry breaking rules, for example increasing([A2,B2,B1,A5] (instead of A1 #= 28) Then there are 6 solutions: [18,16,22,28,32,28,24,36,20,26] [18,20,24,28,32,26,22,36,16,28] [20,18,32,36,28,28,26,22,24,16] [26,16,22,28,36,28,20,32,24,18] [32,18,20,24,28,28,26,22,36,16] [32,18,20,28,36,24,22,26,28,16] */ go2 => println(symmetry_breaking=count_all(p(_X,true))), println(no_symmetry_breaking=count_all(p(_X,false))), nl. p(X) => p(X,true). p(X,Sym) => N = 10, X = new_list(N), X :: [16,18,20,22,24,26,28,32,36], [A1,A2,A3,A4,A5, B1,B2,B3,B4,B5] = X, A1 + B2 + B3 + A3 #= 100, A2 + B2 + B1 + A5 #= 100, A3 + B4 + B5 + A5 #= 100, A1 + B1 + B5 + A4 #= 100, A2 + B3 + B4 + A4 #= 100, % There are 9 different values + 2 occurrences of 28 nvalue(9,X), count(28,X,#=,2), % Symmetry breaking if Sym then A1 #= 28 % 24 solutions % increasing([A2,B2,B1,A5]) % another one with 6 solutions end, solve(X).