/* Lace problem in Picat. From http://www.sci.brooklyn.cuny.edu/~zhou/prolog_contest/pc2012.txt """ Solutions in B-Prolog for the 2012 Prolog Programming Contest Team members: Christian Theil Have, Nuno Lopes, and Neng-Fa Zhou """ This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ % import util. % import cp. main => go. %% 1. lace.pl % Example query ?-lace(5). go => lace(5). go2 => foreach(N in 1..11) writeln(n=N), lace(N), nl end, nl. lace(N) => grid_size(N,S), Grid = new_array(S,S), R0 = S//2+1, C0 = R0, foreach(I in 1..N) draw(I,Grid,R0,C0) end, foreach(R in 1..S) foreach(C in 1..S) var(Grid[C,R]) -> printf(" ") ; printf("*") end, nl end. draw(I,Grid,R0,C0), I mod 2 == 0 => grid_size(I,S), foreach(Offset in 0..S//2) (Grid[R0-Offset,C0-S//2+Offset] = '*', Grid[R0-Offset,C0+S//2-Offset] = '*', Grid[R0+Offset,C0-S//2+Offset] = '*', Grid[R0+Offset,C0+S//2-Offset] = '*') end. draw(I,Grid,R0,C0) => grid_size(I,S), foreach(C in C0-S//2..C0+S//2) (Grid[R0-S//2,C] = '*', Grid[R0+S//2,C] = '*') end, foreach(R in R0-S//2..R0+S//2) (Grid[R,C0-S//2] = '*', Grid[R,C0+S//2] = '*') end. table (+,-) grid_size(1,S) => S = 3. grid_size(N,S) ?=> 0 == N mod 2,% !, N1 = N-1, grid_size(N1,S1), S = S1 + S1 // 2 * 2. grid_size(N,S) ?=> N1 = N - 1, grid_size(N1,S).