/* Completely odd matrices in Picat. From https://puzzling.stackexchange.com/questions/128939/can-you-construct-5x5-and-6x6-completely-odd-matrices """ Can you construct 5x5 and 6x6 “completely-odd” matrices? An n x n matrix is said to be completely-odd if: the numbers in the matrix are either 0 or 1 and for every number in the matrix, the sum of the numbers in its neighborhood is odd where the neighborhood of a number includes the number itself and its horizontally and vertically adjacent numbers. """ n = 5 num_solutions = 4 Solution: 0 0 0 1 1 1 1 0 1 1 1 1 1 0 0 0 1 1 1 0 1 0 1 1 0 Number of neigbours 1 1 1 3 3 3 3 3 3 3 3 5 3 3 1 3 3 5 3 1 1 3 3 3 1 Solution: 0 1 1 0 1 0 1 1 1 0 0 0 1 1 1 1 1 0 1 1 1 1 0 0 0 Number of neigbours 1 3 3 3 1 1 3 5 3 3 1 3 3 5 3 3 3 3 3 3 3 3 1 1 1 Solution: 1 0 1 1 0 0 1 1 1 0 1 1 1 0 0 1 1 0 1 1 0 0 0 1 1 Number of neigbours 1 3 3 3 1 3 3 5 3 1 3 5 3 3 1 3 3 3 3 3 1 1 1 3 3 Solution: 1 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 1 1 1 0 0 1 1 0 1 Number of neigbours 3 3 1 1 1 3 3 3 3 3 1 3 3 5 3 1 3 5 3 3 1 3 3 3 1 n = 6 num_solutions = 1 Solution: 1 0 1 1 0 1 0 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 0 1 0 1 1 0 1 Number of neigbours 1 3 3 3 3 1 3 3 5 5 3 3 3 5 5 5 5 3 3 5 5 5 5 3 3 3 5 5 3 3 1 3 3 3 3 1 See go2/0 for the number of solutions for n=1..20. This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go ?=> nolog, member(N,5..6), nl, println(n=N), println(num_solutions=count_all(completely_odd_matrix(N, _X))), completely_odd_matrix(N, X), println("Solution:"), foreach(I in 1..N) foreach(J in 1..N) printf("%2d ", X[I,J]) end, nl end, println("Number of neigbours"), foreach(I in 1..N) foreach(J in 1..N) printf("%2d ", X[I,J] + sum([X[I+A,J+B] : A in -1..1, B in -1..1, abs(A) + abs(B) == 1, I+A >= 1, I+A <= N, J+B >= 1, J+B <= N])) end, nl end, nl, nl, fail, nl. go => true. /* Number of solutions: N = #sols --------- 1 = 1 2 = 1 3 = 1 4 = 16 5 = 4 6 = 1 7 = 1 8 = 1 9 = 256 10 = 1 11 = 64 12 = 1 13 = 1 14 = 16 15 = 1 16 = 256 17 = 4 18 = 1 19 = 65536 20 = 1 This is the OEIS sequence https://oeis.org/A075462 """ A075462 a(n) is the number of solutions to the all-ones lights out problem on an n X n square. """ */ go2 ?=> nolog, foreach(N in 1..20) println(N=count_all(completely_odd_matrix(N, _X))) end, nl. go2 => true. completely_odd_matrix(N, X) => X = new_array(N,N), X :: 0..1, foreach(I in 1..N, J in 1..N) X[I,J] + sum([X[I+A,J+B] : A in -1..1, B in -1..1, abs(A) + abs(B) == 1, I+A >= 1, I+A <= N, J+B >= 1, J+B <= N]) mod 2 #= 1 end, solve($[],X).