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,26 @@
# Create an m x n matrix
def matrix(m; n; init):
if m == 0 then []
elif m == 1 then [range(0;n)] | map(init)
elif m > 0 then
matrix(1;n;init) as $row
| [range(0;m)] | map( $row )
else error("matrix\(m);_;_) invalid")
end ;
# Print a matrix neatly, each cell occupying n spaces
def neatly(n):
def right: tostring | ( " " * (n-length) + .);
. as $in
| length as $length
| reduce range (0;$length) as $i
(""; . + reduce range(0;$length) as $j
(""; "\(.)\($in[$i][$j] | right )" ) + "\n" ) ;
def right:
if . == [1, 0] then [ 0, -1]
elif . == [0, -1] then [-1, 0]
elif . == [-1, 0] then [ 0, 1]
elif . == [0, 1] then [ 1, 0]
else error("invalid direction: \(.)")
end;

View file

@ -0,0 +1,14 @@
def spiral(n):
# we just placed m at i,j, and we are moving in the direction d
def _next(i; j; m; d):
if m == (n*n) - 1 then .
elif .[i+d[0]][j+d[1]] == false
then .[i+d[0]][j+d[1]] = m+1 | _next(i+d[0]; j+d[1]; m+1; d)
else (d|right) as $d
| .[i+$d[0]][j+$d[1]] = m+1 | _next(i+$d[0]; j+$d[1]; m+1; $d)
end;
matrix(n;n;false) | .[0][0] = 0 | _next(0;0;0; [0,1]) ;
# Example
spiral(5) | neatly(3)