Write a program that takes in two non-negative integers S and N in that order. S represents the side length of a square grid of .
characters. N represents the number of those .
's that need to be changed to x
's. You may assume N is no greater than S squared.
Your program needs to output this S×S square of .
's and N x
's but the requirement is that the square must always have a diagonal line of symmetry from its top left to its bottom right. Any grid arrangement is valid output as long as it has this symmetry.
For example, if S is 3 and N is 4 here are several grids that have this diagonal symmetry and would be valid output:
x.x
...
x.x
x..
.xx
.x.
...
.xx
.xx
The following grids however would not be valid:
.x.
xxx
...
(lacks diagonal symmetry)
..x
xx.
.x.
(has diagonal symmetry but not from the top left to the bottom right)
x.x
.x.
x.x
(incorrect number of x's)
This is code golf so the shortest program in bytes wins!
Details:
A trailing newline after the grid is fine.
You may use any two distinct printable-ASCII characters in place of
.
andx
if you prefer.If you prefer you may even output a binary-matrix instead of a string.
When N is 0 the output will be a pure S×S square of
.
's with nox
's.When S is 0 the output will be an empty string (or single trailing newline).
The output does not need to be deterministic, as long as it is always guaranteed to be valid.
More Examples:
Not all valid arrangements are listed for each example input. Your output might look different yet still be valid.
S = 0, N = 0
[empty string]
S = 1, N = 0
.
S = 1, N = 1
x
S = 2, N = 0
..
..
S = 2, N = 1
x.
..
..
.x
S = 2, N = 2
x.
.x
.x
x.
S = 2, N = 3
xx
x.
.x
xx
S = 2, N = 4
xx
xx
S = 3, N = 2
x..
.x.
...
..x
...
x..
S = 4, N = 1
....
.x..
....
....
S = 4, N = 5
x..x
.x..
..x.
x...
xxx.
x...
x...
....
S = 5, N = 23
xxxxx
xxxxx
xxxxx
xxx.x
xxxx.
xxxxx
xxxxx
xxxxx
xxxx.
xxx.x
S = 7, N = 13
...x...
...x...
...x...
xxxxxxx
...x...
...x...
...x...
xxxxxxx
x......
x......
x......
x......
x......
x......
.
andx
? \$\endgroup\$binary-matrix
rather thanstring
. (Unless there are more clever ways of doing it with string manipulation than with matrix manipulation, but this seems a bit unlikely.) \$\endgroup\$