/* The Abbott's Window puzzle in Picat. From Martin Chlond Integer Programming Puzzles: http://www.chlond.demon.co.uk/puzzles/puzzles3.html, puzzle 2 The Abbott's Widow. Description : The Abbott's Window Source : Dudeney, H.E., (1917), Amusements in Mathematics, Thomas Nelson and Sons. """ 2. The Abbott's Window Once upon a time the Lord Abbott of St. Edmondsbury, in consequence of "devotions too strong for his head," fell sick and was unable to leave his bed. As he lay awake, tossing his head restlessly from side to side, the attentive monks noticed that something was disturbing his mind; but nobody dared ask what it might be, for the Abbott was of a stern disposition, and never would brook inquisitiveness. Suddenly he called for Father John, and that venerable monk was soon at the bedside. "Father John," said the Abbott, "dost thou know that I came into this wicked world on a Christmas Even?" The monk nodded assent. "And have I not often told thee that, having been born on Christmas Even, I have no love for the things that are odd? Look there!" The Abbott pointed to the large dormitory window, of which I give a sketch. The monk looked and was perplexed. "Dost thou not see that the sixty-four lights add up to an even number vertically and horizontally, but that all the diagonal lines, except fourteen are of a number that is odd? Why is this?" "Of a truth, my Lord Abbott, it is of the very nature of things, and cannot be changed." "Nay, but it shall be changed. I command thee that certain of the lights be closed this day, so that every line shall have an even number of lights. See thou that this be done without delay, lest the cellars be locked for a month and other grievous troubles befall thee." Father John was at his wits' end, but after consultation with one who was learned in strange mysteries (integer programming), a way was found to satisfy the whim of the Lord Abbott. Which lights were blocked up, so that those which remained added up to an even number in every line horizontally, vertically, and diagonally, while the least possible obstruction of light was caused? Father John held that the four corners should be darkened, but the sage explained that it was desired to obstruct no more light than was absolutely necessary, and he said, anticipating Lord Dundreary, "A single pane can no more be in line with itself than one bird can go into a corner and flock in solitude. The Abbott's condition was that no diagonal lines should contain an odd number of lights." (Dudeney) """ This model was inspired by the XPress Mosel model created by Martin Chlond. http://www.chlond.demon.co.uk/puzzles/sol3s2.html This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ % import sat. % 1.4s % import cp. % 4.1s import mip. % 0.07s main => go. go => Row = 8, R = 1..Row, Col = 8, C = 1..Col, X = new_array(Row,Col), X :: 0..1, A = new_array(Row), A :: 0..4, B = new_array(Col), B :: 0..4, Row2=Row-2, CC = new_list(Row2), CC :: 0..4, D = new_list(Col-1), D :: 0..4, E = new_list(Col-1), E :: 0..4, F = new_list(Row-2), F :: 0..4, TotalSum #= sum([X[I,J] : I in R, J in C]), foreach(I in R) sum([X[I,J] : J in C]) #= 2*A[I] end, foreach(J in C) sum([X[I,J] : I in R]) #= 2*B[J] end, foreach(I in 2..Row-1) sum([X[K,I-K+1] : K in 1..I]) #= 2*CC[I-1] end, foreach(J in 1..Col-1) sum([X[K,Col-K+J] : K in J..Row]) #= 2*D[J] end, foreach(J in 1..Col-1) sum([X[K,J+K-1] : K in 1..Row-J+1]) #= 2*E[J] end, foreach(I in 2..Row-1) sum([X[K,K-I+1] : K in I..Row]) #= 2*F[I-1] end, X[1,1] #= 1, X[Row,1] #= 1, X[1,Col] #= 1, X[Row,Col] #= 1, solve($[max(TotalSum)], X.to_list() ++ [TotalSum]), % solve($[max(TotalSum)]), writeln(totalSum=TotalSum), foreach(XRow in X) writeln(XRow.to_list()) end, nl.