% Autor:
% Datum: 25.11.2016


:- use_module(library(clpfd)).

black_squares(Black):-
     read_in(0,BL),!,
     b(_,_,_,Black,BL,[]).

% reads brackets into a list of 1s and -1s
read_in(N,[B|BL]):-
     N1 is N+1,
     bracket(N,B),!,
     read_in(N1,BL).
     
read_in(_,[]).

% b(Tree,Width,Height,Black,String,RestString)
b(T,W,H,B) --> bs(T,W,H,B).  % necessary to avoid loop

% list of unconnected bracket structures
% BlackOfTotal = Sum of Blacks of 1st element + Black of rest elements
% WidthTotal = Sum of Width of 1st element + Width of rest elements +1 (separator)
% HeightTotal = Maximum of Height of 1st element and Height of rest elements
b([T1|T2],W,H,B) --> bs(T1,W1,H1,B1),b(T2,W2,H2,B2),
     {W#=W1+W2+1,H#=max(H1,H2),B#=B1+B2}.

% [1,-1] gives 1 block, Black=Height=Widht=1
bs(b,1,1,1) --> [1,-1].

% 1 bracket structure
% Width of outer structure = Width of inner structure +2
% Height of outer structure = Height of inner structure +1
% Black of outer structure = Widht * Height - Black of inner structure
bs(t(T),W,H,B) --> [1],b(T,W1,H1,B1),[-1],
     {W #=W1+2,H#=H1+1,B#=W*H-B1}.




% description of bracket structure
/*
bracket(0,1).
bracket(1,1).
bracket(2,1).
bracket(3,-1).
bracket(4,-1).
bracket(5,-1).
*/


%/*
bracket(0,1).
bracket(1,-1).
bracket(2,1).
bracket(3,1).
bracket(4,-1).
bracket(5,-1).
bracket(6,1).
bracket(7,1).
bracket(8,1).
bracket(9,-1).
bracket(10,-1).
bracket(11,-1).
%*/

/*
bracket(0,1).
bracket(1,1).
bracket(2,1).
bracket(3,-1).
bracket(4,-1).
bracket(5,1).
bracket(6,1).
bracket(7,1).
bracket(8,-1).
bracket(9,1).
bracket(10,-1).
bracket(11,1).
bracket(12,-1).
bracket(13,-1).
bracket(14,-1).
bracket(15,-1).
*/