Solve a Hidato puzzle: Difference between revisions

m
→‎{{header|REXX}}: changed/added comments and whitespace, changed indentations.
m (added whitespace before the TOC.)
m (→‎{{header|REXX}}: changed/added comments and whitespace, changed indentations.)
Line 2,800:
<br>If ''any'' marker is negative, then it's assumed to be a Numbrix puzzle (and the absolute value is used).
<br>Over half of the REXX program deals with validating the input and displaying the puzzle.
<br><br>''Hidato'' and ''Numbrix'' are registered trademarks.
<lang rexx>/*REXX pgm solves a Hidato or Numbrix puzzle, displays puzzle & solution*/
maxr=0; maxc=0; maxx=0; minr=9e9; minc=9e9; minx=9e9; cells=0; @.=
parse arg xxx; PZ='Hidato puzzle' /*get cell definitions from C.L. */
xxx=translate(xxx, , "/\;:_", ',') /*also allow other chars as comma*/
 
<br><br>''Hidato'' &nbsp; and &nbsp; ''Numbrix'' &nbsp; are registered trademarks.
do while xxx\=''; parse var xxx r c marks ',' xxx
<lang rexx>/*REXX pgmprogram solves a Hidato or Numbrix (R) puzzle, it also displays the puzzle &and solution. */
do while marks\=''; _=@.r.c
maxrmaxR=0; maxc maxC=0; maxx maxX=0; minr minR=9e9; minc minC=9e9; minx minX=9e9; cells=0; @.=
parse arg xxx; PZ='Hidato puzzle' /*get the cell definitions from C.L.the CL.*/
xxx=translate(xxx, , "/\;:_", ',') /*also allow other charscharacters as comma.*/
 
do while xxx\=''; parse var xxx r c marks ',' xxx
do while marks\=''; _=@.r.c
parse var marks x marks
if datatype(x,'N') then do; x=x/1 /*normalize X*/
if x<0 then PZ= 'Numbrix puzzle'
x=abs(x) /*use │x│ */
end
minrminR=min(minrminR,r); maxR=max(maxR,r); minC=min(minC,c); maxrmaxC=max(maxrmaxC,rc)
mincif x=min(minc,c)=1 then do; !r=r; maxc!c=max(maxc,c); end /*the START cell. */
if x_\==1'' then call thenerr do;"cell at" !r=r; !c=c; 'is endalready occupied with:' /*start cell.*/_
if@.r.c=x; _\ c=c+1; cells=''cells+1 then call err "cell at" r c 'is/*assign alreadya occupiedmark. with:' _*/
@.r.c=if x;==. then iterate c=c+1; cells=cells+1 /*assignis a hole? markSkip*/
if x==. then iterate /*hole? Skip.*/
if \datatype(x,'W') then call err 'illegal marker specified:' x
minxminX=min(minxminX,x); maxxmaxX=max(maxxmaxX,x) /*min &and max X. */
end /*while marks¬='' */
end /*while xxx ¬='' */
call showGridshow /* [↓] is used for making fast moves. */
Nr = '0 1 0 -1 -1 1 1 -1' /*possible row for the next move. */
Nc = '1 0 -1 0 1 -1 1 -1' /* " col column " " " " */
pMoves=words(Nr) -4*(left(PZ,1)=='N') /*is this to be a Numbrix puzzle ? */
do i=1 for pMoves; Nr.i=word(Nr,i); Nc.i=word(Nc,i); end /*for fast moves. */
if \next(2,!r,!c) then saycall err 'No solution possible for this' PZ "puzzle."
say 'A solution for the' PZ "exists."; say; else say 'A solution for the' PZ "exists." call show
say; call showGrid
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────ERR subroutine──────────────────────*/
err: say; say '***error!*** (from' PZ"): " arg(1); say; exit 13
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────NEXT subroutine─────────────────────*/
next: procedure expose @. Nr. Nc. cells pMoves; parse arg #,r,c; ##=#+1
do t=1 for pMoves /* [↓] try some moves. */
parse value r+Nr.t c+Nc.t with nr nc /*next move coördinates.*/
if @.nr.nc==. then do; @.nr.nc=# /*alet's try this move. */
if #==cells then leave /*is this the last 1move?*/
if next(##,nr,nc) then return 1
@.nr.nc=. /*undo the above move. */
iterate /*go & try another move.*/
end
if @.nr.nc==# then do /*is this a fill-in ?move ? */
if #==cells then return 1 /*this is the last 1move.*/
if next(##,nr,nc) then return 1 /*a fill-in move. */
end
end /*t*/
return 0 /*Thisthis ain't working. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────SHOWGRID subroutine─────────────────*/
showGridshow: if maxrmaxR<1 | maxcmaxC<1 then call err 'no legal cell was specified.'
if minxminX<1 then call err 'no 1 was specified for the puzzle start'
w=max(2,length(cells)); do r=maxrmaxR to minrminR by -1; _=
if maxx\==cells then call err 'no' cells "was specified for the puzzle end"
if x==. thendo iteratec=minC to maxC; /*hole? Skip _=_ right(@.r.c,w); end /*c*/
w=length(cells); do r=maxr to minr by -1; _=
do c=minc to maxc; _=_ right(@.r.c,w); endsay /*c*/_
say _ end /*r*/
say; end /*r*return</lang>
'''output''' &nbsp; when using the following as input:
say; return</lang>
'''output''' using the following as input:
<br> <tt> 1 7 5 .\2 5 . 7 . .\3 3 . . 18 . .\4 1 27 . . . 9 . 1\5 1 . 26 . 13 40 11\6 1 . . . 21 . .\7 1 . . 24 22 .\8 1 . 33 35 . .</tt>
<pre>