Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
46
Task/Cut-a-rectangle/REXX/cut-a-rectangle-1.rexx
Normal file
46
Task/Cut-a-rectangle/REXX/cut-a-rectangle-1.rexx
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
/*REXX program cuts rectangles into two symmetric pieces, the rectangles are cut along */
|
||||
/*────────────────────────────────────────────────── unit dimensions and may be rotated.*/
|
||||
numeric digits 20 /*be able to handle some big integers. */
|
||||
parse arg N .; if N=='' | N=="," then N= 10 /*N not specified? Then use default.*/
|
||||
dir.= 0; dir.0.1= -1; dir.1.0= -1; dir.2.1= 1; dir.3.0= 1 /*the four directions*/
|
||||
|
||||
do y=2 to N; say /*calculate rectangles up to size NxN.*/
|
||||
do x=1 for y; if x//2 & y//2 then iterate /*Both X&Y odd? Skip.*/
|
||||
z= solve(y,x,1); _= comma(z); _= right(_, max(14, length(_))) /*align output.*/
|
||||
say right(y, 9) "x" right(x, 2) 'rectangle can be cut' _ "way"s(z).
|
||||
end /*x*/
|
||||
end /*y*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
comma: procedure; arg _; do k=length(_)-3 to 1 by -3; _=insert(',',_,k); end; return _
|
||||
s: if arg(1)=1 then return arg(3); return word( arg(2) 's', 1) /*pluralizer.*/
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
solve: procedure expose # @. dir. h len next. w; @.= 0 /*zero rectangle coördinates.*/
|
||||
parse arg h,w,recur /*get values for some args. */
|
||||
if h//2 then do; t= w; w= h; h= t; if h//2 then return 0
|
||||
end
|
||||
if w==1 then return 1
|
||||
if w==2 then return h
|
||||
if h==2 then return w /* [↓] % is REXX's integer ÷*/
|
||||
cy= h % 2; cx= w % 2; wp= w + 1 /*cut the rectangle in half. */
|
||||
len= (h+1) * wp - 1 /*extend area of rectangle. */
|
||||
next.0= '-1'; next.1= -wp; next.2= 1; next.3= wp /*direction & distance*/
|
||||
if recur then #= 0
|
||||
cywp= cy * wp /*shortcut calculation*/
|
||||
do x=cx+1 to w-1; t= cywp + x; @.t= 1
|
||||
_= len - t; @._= 1; call walk cy - 1, x
|
||||
end /*x*/
|
||||
#= # + 1
|
||||
if h==w then #= # + # /*double rectangle cut count.*/
|
||||
else if w//2==0 & recur then call solve w, h, 0
|
||||
return #
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
walk: procedure expose # @. dir. h len next. w wp; parse arg y,x
|
||||
if y==h | x==0 | x==w | y==0 then do; #= # + 2; return; end
|
||||
t= y*wp + x; @.t= @.t + 1; _= len - t
|
||||
@._= @._ + 1
|
||||
do j=0 for 4; _= t + next.j /*try each of 4 directions.*/
|
||||
if @._==0 then call walk y + dir.j.0, x + dir.j.1
|
||||
end /*j*/
|
||||
@.t= @.t - 1
|
||||
_= len - t; @._= @._ - 1; return
|
||||
62
Task/Cut-a-rectangle/REXX/cut-a-rectangle-2.rexx
Normal file
62
Task/Cut-a-rectangle/REXX/cut-a-rectangle-2.rexx
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
/*REXX program cuts rectangles into two symmetric pieces, the rectangles are cut along */
|
||||
/*────────────────────────────────────────────────── unit dimensions and may be rotated.*/
|
||||
numeric digits 40 /*be able to handle some big integers. */
|
||||
parse arg m . /*obtain optional argument from the CL.*/
|
||||
if m=='' | m=="," then m= 9 /*Not specified? Then use the default.*/
|
||||
if m<0 then start= max(2, abs(m) ) /*<0? Then just use this size rectangle*/
|
||||
else start= 2 /*start from two for regular invocation*/
|
||||
dir.= 0; dir.0.1= -1; dir.1.0= -1; dir.2.1= 1; dir.3.0= 1 /*the 4 directions.*/
|
||||
$= '# @. dir. h len next. w wp'
|
||||
/*define the default for memoizations. */
|
||||
do y=start to abs(m); yOdd= y//2; say /*calculate rectangles up to size MxM.*/
|
||||
do x=1 for y; if x//2 then if yOdd then iterate /*X and Y odd? Skip.*/
|
||||
z= solve(y, x, 1); zc= comma(z) /*add commas to the result for SOLVE. */
|
||||
zca= right(zc, max(14,length(zc) ) ) /*align the output for better perusing.*/
|
||||
say right(y, 9) "x" right(x, 2) 'rectangle can be cut' zca "way"s(z).
|
||||
end /*x*/
|
||||
end /*y*/
|
||||
exit 0 /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
comma: procedure; arg ?; do k=length(?)-3 to 1 by -3; ?=insert(',',?,k); end; return ?
|
||||
s: if arg(1)=1 then return arg(3); return word(arg(2) 's', 1) /*pluralizer.*/
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
solve: procedure expose ($); @.= 0 /*zero rectangle coördinates.*/
|
||||
parse arg h,w,recurse /*get values for some args. */
|
||||
if w==3 then do; z= h % 2 + 2; return 2**z - (z + z) + 1
|
||||
end
|
||||
if h//2 then do; t= w; w= h; h= t; if h//2 then return 0
|
||||
end
|
||||
if w==1 then return 1
|
||||
if w==2 then return h
|
||||
if h==2 then return w /* [↓] % is REXX's integer division.*/
|
||||
cy= h % 2; cx= w % 2; wp= w + 1 /*cut the [XY] rectangle in half. */
|
||||
len= (h+1) * wp - 1 /*extend the area of the rectangle. */
|
||||
next.0= '-1'; next.1= -wp; next.2= 1; next.3= wp /*direction & distance*/
|
||||
if recurse then #= 0 /*doing recursion ? */
|
||||
cywp= cy * wp /*shortcut calculation*/
|
||||
do x=cx+1 to w-1; t= cywp + x; @.t= 1
|
||||
__= len - t; @.__= 1; call walk cy - 1, x
|
||||
end /*x*/
|
||||
#= # + 1
|
||||
if h==w then #= # + # /*double the count of rectangle cuts. */
|
||||
else if w//2==0 then if recurse then call solve w, h, 0
|
||||
return #
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
walk: procedure expose ($); parse arg y,x
|
||||
if y==h then do; #= # + 2; return; end /* ◄──┐ REXX short circuit. */
|
||||
if x==0 then do; #= # + 2; return; end /* ◄──┤ " " " */
|
||||
if x==w then do; #= # + 2; return; end /* ◄──┤ " " " */
|
||||
if y==0 then do; #= # + 2; return; end /* ◄──┤ " " " */
|
||||
q= y*wp + x; @.q= @.q + 1; _= len - q /* │ordered by most likely ►──┐*/
|
||||
@._= @._ + 1 /* └──────────────────────────┘*/
|
||||
do j=0 for 4; _= q + next.j /*try each of the four directions.*/
|
||||
if @._==0 then do; yn= y + dir.j.0
|
||||
if yn==h then do; #= # + 2; iterate; end
|
||||
xn= x + dir.j.1
|
||||
if xn==0 then do; #= # + 2; iterate; end
|
||||
if xn==w then do; #= # + 2; iterate; end
|
||||
if yn==0 then do; #= # + 2; iterate; end
|
||||
call walk yn, xn
|
||||
end
|
||||
end /*j*/
|
||||
@.q= @.q - 1; _= len - q; @._= @._ - 1; return
|
||||
Loading…
Add table
Add a link
Reference in a new issue