/*
Simple puzzle in Picat.
Problem statement in
http://www.thescripts.com/forum/thread42216.html
"""
The puzzle: a 4 x 4 grid. The rows are summed (and given), the
cols are summed (and given), and the two diagonals are summed,
and given. In addition, 4 clues are given, but none of the 4 are in
the same row or col.
Example from today's paper:...solution time is 8 minutes, 1 second,
so they say.
The set of allowable numbers is 1 thru 9
Rows:
3 + B + C + D = 22
E + F + 8 + H = 26
I + J + K + 8 = 31
M + 7 + O + P = 25
Col sums:
24, 18, 31, 31
Diag sums:
3 + F + K + P = 24
M + J + 8 + D = 24
"""
Note: This instance has two solutions.
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 =>
rowsums(RowSums),
colsums(ColSums),
diagsums(DiagSums),
N = RowSums.length,
clues(X),
X.vars() :: 1..9,
foreach(I in 1..N)
sum([X[I,J] : J in 1..N]) #= RowSums[I],
sum([X[J,I] : J in 1..N]) #= ColSums[I]
end,
sum([X[I,I] : I in 1..N]) #= DiagSums[1],
sum([X[N-I+1,I] : I in 1..N]) #= DiagSums[2],
solve(X),
foreach(Row in X) println(Row) end,
nl.
rowsums(RowSums) => RowSums = [22, 26, 31, 25].
colsums(ColSums) => ColSums = [24, 18, 31, 31].
diagsums(DiagSums) => DiagSums = [24, 24].
clues(Clues) =>
Clues=
[[3, _, _, _],
[_, _, 8, _],
[_, _, _, 8],
[_, 7, _, _]
].