/* Department numbers (Rosetta code) in Picat. http://rosettacode.org/wiki/Department_numbers#MAD """ There is a highly organized city that has decided to assign a number to each of their departments: * police department * sanitation department * fire department Each department can have a number between 1 and 7(inclusive). The three department numbers are to be unique (different from each other) and must add up to 12. The Chief of the Police doesn't like odd numbers and wants to have an even number for his department. Task Write a computer program which outputs all valid combinations. Possible output (for the 1st and 14th solutions): --police-- --sanitation-- --fire-- 2 3 7 6 5 1 """ This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. import util. main => go. % Constraint model go ?=> N = 7, Sols = findall([P,S,F], department_numbers(N, P,S,F)), println(" P S F"), foreach([P,S,F] in Sols) printf("%2d %2d %2d\n",P,S,F) end, nl, printf("Number of solutions: %d\n", Sols.len), nl. go => true. department_numbers(N, Police,Sanitation,Fire) => Police :: 1..N, Sanitation :: 1..N, Fire :: 1..N, all_different([Police,Sanitation,Fire]), Police + Sanitation + Fire #= 12, Police mod 2 #= 0, solve([Police,Sanitation,Fire]). % Loop approach go2 => department_numbers2(7), nl. department_numbers2(N) => println(" P S F"), foreach(P in 1..N, P mod 2 == 0) foreach(S in 1..N, P != S) foreach(F in 1..N, F != P, F != S, P + S + F == 12) printf("%2d %2d %2d\n",P,S,F) end end end. go3 => department_numbers3(7), nl. % List comprehension department_numbers3(N) => println("P S F"), L = [[P.to_string,S.to_string,F.to_string] : P in 1..N, P mod 2 == 0, S in 1..N, P != S, F in 1..N, F != P, F != S, P + S + F == 12], println(map(L,join).join("\n")). % {{trans|Prolog}} go4 :- println("P F S"), assign(Police, Fire, Sanitation), printf("%w %w %w\n", Police, Fire, Sanitation), fail, nl. % {{trans|prolog}} dept(X) :- between(1, 7, X). police(X) :- member(X, [2, 4, 6]). fire(X) :- dept(X). san(X) :- dept(X). assign(A, B, C) :- police(A), fire(B), san(C), A != B, A != C, B != C, 12 is A + B + C.