Solve a Hopido puzzle: Difference between revisions

m
→‎{{header|REXX}}: changed/added comments and whitespace, changed indentations.
m (→‎{{header|Perl 6}}: fixed format of initial solveboard)
m (→‎{{header|REXX}}: changed/added comments and whitespace, changed indentations.)
Line 674:
 
=={{header|REXX}}==
This REXX program is a slightly modified version of the REXX   '''Hidato'''   program.
<lang rexx>/*REXX program solves a Hopido puzzle, it also displays the puzzle and the solution. */
call time 'Reset' /*reset the REXX elapsed timer. to zero.*/
maxrmaxR=0; maxc maxC=0; maxx maxX=0; minr minR=9e9; minc minC=9e9; minx minX=9e9; cells=0; @.=
parse arg xxx; /*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 x=x/1 /*normalize X. */
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; end'is already occupied /*startwith:' 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 3 0 -3 -2 2 2 -2' /*possible row for the next move. */
Nc = '3 0 -3 0 2 -2 2 -2' /* " col column " " " " */
pMoves=words(Nr) /*the number of possible moves. */
do i=1 for pMoves; Nr.i=word(Nr, i); Nc.i=word(Nc,i); end /*fast movesi*/
if \next(2,!r,!c) then call err 'No solution possible for this Hopido puzzle.'
say 'A solution for the Hopido exists.'; say; call showGridshow
etetime= format(time('Elapsed'), , 2) /*getobtain REXXthe elapsed time (in secsseconds).*/
if etetime<.1 then say 'and took less than 1/10 of a second.'
else say 'and took' et etime "seconds."
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────ERR subroutine──────────────────────*/
err: say; say '***error!*** (from Hopido): ' 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 /*This 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; _=
do c=mincminC to maxcmaxC; _=_ right(@.r.c,w); end /*c*/
say _
end /*r*/
say; return</lang>
'''output''' &nbsp; when the input is: <br>
<br><tt> 1 4 1 \2 3 . . . \3 2 . . . . . \4 1 . . . . . . . \5 1 . . . . . . . \6 2 . . \6 5 . . </tt>
<pre>
. . . .