/* 4-rings or 4-squares puzzle (Rosetta code) in Picat. http://rosettacode.org/wiki/4-rings_or_4-squares_puzzle """ Task Replace a, b, c, d, e, f, and g with the decimal digits LOW ───► HIGH such that the sum of the letters inside of each of the four large squares add up to the same sum. ╔══════════════╗ ╔══════════════╗ ║ ║ ║ ║ ║ a ║ ║ e ║ ║ ║ ║ ║ ║ ┌───╫──────╫───┐ ┌───╫─────────┐ ║ │ ║ ║ │ │ ║ │ ║ │ b ║ ║ d │ │ f ║ │ ║ │ ║ ║ │ │ ║ │ ║ │ ║ ║ │ │ ║ │ ╚══════════╪═══╝ ╚═══╪══════╪═══╝ │ │ c │ │ g │ │ │ │ │ │ │ │ │ └──────────────┘ └─────────────┘ Show all output here. Show all solutions for each letter being unique with LOW=1 HIGH=7 Show all solutions for each letter being unique with LOW=3 HIGH=9 Show only the number of solutions when each letter can be non-unique LOW=0 HIGH=9 """ This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go => puzzle_all(1, 7, true, Sol1), foreach(Sol in Sol1) println(Sol) end, nl, puzzle_all(3, 9, true, Sol2), foreach(Sol in Sol2) println(Sol) end, nl, puzzle_all(0, 9, false, Sol3), println(len=Sol3.len), nl. go2 => puzzle_all2(1, 7, true, Sol1), foreach(Sol in Sol1) println(Sol) end, nl, puzzle_all2(3, 9, true, Sol2), foreach(Sol in Sol2) println(Sol) end, nl, puzzle_all2(0, 9, false, Sol3), println(len=Sol3.len), nl. puzzle_all(Min, Max, Distinct, LL) => L = [A,B,C,D,E,F,G], L :: Min..Max, if Distinct then all_different(L) else true end, T #= A+B, T #= B+C+D, T #= D+E+F, T #= F+G, LL = solve_all(L). % Another approach puzzle_all2(Min, Max, Distinct, LL) => L = [A,B,C,D,E,F,G], L :: Min..Max, if Distinct then all_different(L) else true end, Sums = $[A+B,B+C+D,D+E+F,F+G], foreach(I in 2..Sums.len) Sums[I] #= Sums[I-1] end, LL = solve_all(L).