:- use_module(contestlib, [writeN/2, int_width/2, write_int/2]).
diamond(N) :-
N1 is N*N,
int_width(N1,W),
Spaces1 is N*W,
first(N,1,1,N,Spaces1,W),
Start is N*(N - 1) + 2,
Spaces2 is 2*W,
last(N,Start,N,Spaces2,W).
first(0,_,_,_,_,_) :- !.
first(I,Start,Row,N,Spaces,W) :-
I1 is I - 1,
Start1 is Start + N,
Row1 is Row + 1,
Spaces1 is Spaces - W,
writeN(Spaces,' '),
writeline(Row,Start,N,W),
first(I1,Start1,Row1,N,Spaces1,W).
last(1,_,_,_,_) :- !.
last(I,Start,N,Spaces,W) :-
I1 is I - 1,
Start1 is Start + 1,
writeN(Spaces,' '),
Spaces1 is Spaces + W,
writeline(I1,Start,N,W),
last(I1,Start1,N,Spaces1,W).
writeline(0,_,_,_) :- !, nl.
writeline(R,S,N,W) :-
write_int(S,W),
writeN(W,' '),
S1 is S - N + 1,
R1 is R - 1,
writeline(R1,S1,N,W).