Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,25 @@
def single_solution_queens(n):
def q: "♛";
def init(k): reduce range(0;k) as $i ([]; . + ["."]);
def matrix(k): init(k) as $row | reduce range(0;k) as $i ([]; . + [$row]);
def place(stream; i; j):
# jq indexing is based on offsets but we are using the 1-based formulae:
reduce stream as $s (.; setpath([-1+($s|i), -1+($s|j)]; q) );
def even(k):
if ((k-2) % 6) != 0 then
place( range(1; 1+(k/2)); .; 2*. )
| place( range(1; 1+(k/2)); (k/2) + .; 2*. -1 )
else place( range(1; 1+(k/2)); .; 1 + ((2*. + (k/2) - 3) % k))
| place( range(1; 1+(n/2)); n + 1 - .; n - ((2*. + (n/2) - 3) % n))
end;
matrix(n) # the chess board
| if (n % 2) == 0 then even(n)
else even(n-1) | .[n-1][n-1] = q
end;
# Example:
def pp: reduce .[] as $row
(""; reduce $row[] as $x (.; . + $x) + "\n");
single_solution_queens(8) | pp

View file

@ -0,0 +1,8 @@
...♛....
.....♛..
.......♛
.♛......
......♛.
♛.......
..♛.....
....♛...

View file

@ -0,0 +1,13 @@
# permutations of 0 .. (n-1)
def permutations(n):
# Given a single array, generate a stream by inserting n at different positions:
def insert(m;n):
if m >= 0 then (.[0:m] + [n] + .[m:]), insert(m-1;n) else empty end;
if n==0 then []
elif n == 1 then [1]
else
permutations(n-1) | insert(n-1; n)
end;
def count(g): reduce g as $i (0; .+1);

View file

@ -0,0 +1,16 @@
def queens(n):
def sums:
. as $board
| [ range(0;length) | . + $board[.]]
| unique | length;
def differences:
. as $board
| [ range(0;length) | . - $board[.]]
| unique | length;
def allowable:
length as $n
| sums == $n and differences == $n;
count( permutations(n) | select(allowable) );

View file

@ -0,0 +1 @@
queens(8)