Using Racket, and avoiding any stateful functions (ones that end with an exclamation mark), I am trying to get all "diagonal strips" in a list of lists, or a vector if that is easier.
This is a part of solving the "N-Queens" Chess problem, but I'd rather not see spoilers on how to solve that problem in its entirety. The relevance is that I am trying to determine if any Queen can attack any other Queen diagonally. I figured I could do this the way I do it for horizontal and vertical attacks -- turn each row or column into its own list and see if more than one Queen exists in that list.
However, creating the diagonal strips is becoming very ugly and complicated code! There has got to be a nice, concise way to solve this problem.
Example:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
I would like to form the following lists from this:
'(1 6 11 16) '(2 5) '(2 7 12) '(3 6 9) '(3 8) '(4 7 10 13) '(5 10 15)
'(9 14) '(8 11 14) '(12 15)
I've noticed these lists have the a form where additional elements are either (+ n 1)
more than the previous element, or they are (- n 1)
more than the previous element, but boundary checking other issues are making my code far too complicated, and unlikely to resolve to a reasonable solution. (In this case, n
is the length of one dimension.)
Am I going about this the wrong way? Is there an easier way to check if diagonals are threatened by another Queen? Or is this the correct idea, but I'm missing some handy functions that make this all a ton simpler than I'm thinking?