Towers of Hanoi: Difference between revisions

m
→‎simple text moves: added/changed comments and whitespace, simplified the program, used a template for the output section.
m (→‎Python recursive: (separating composition of the solution from its display))
m (→‎simple text moves: added/changed comments and whitespace, simplified the program, used a template for the output section.)
Line 3,574:
parse arg N . /*get optional number of disks from CL.*/
if N=='' | N=="," then N=3 /*Not specified? Then use the default.*/
#=0 0 /*#: the number of disk moves (so far)*/
z= 2**N - 1 1 /*Z: " " " minimum # of moves.*/
call mov 1, 3, N /*move the top disk, then recurse ··· */
dsk:say #=#+1 /*bump the[↓] (disk) moveDisplay counterthe byminimum one.# of moves.*/
say
say 'The minimum number of moves to solve a ' N"-disk─disk Tower of Hanoi is " z
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
mov: procedure expose # z; parse arg @1,@2,@3; L= length(z)
dsk: #=#+1 /*bump the (disk) move counter by one. */
if say@3==1 'step' then do; right(#,= length(z))":# + move1 disk on tower" arg(1) '───►' arg /*bump the (2disk) move counter by one. */
return say 'step' right(#, L)": move disk on tower" @1 '───►' /* [↑] display the move message (text)*/@2
end
/*──────────────────────────────────────────────────────────────────────────────────────*/
mov: procedure expose # z else do; call mov @1, parse arg 6 -@1, -@2, @3 -1
if @3==1 then call dskmov @1, @2, 1
else do; call mov 6 - @1, - @2, 6-@1-@2, @3 -1
call mov @1, @2, 1end
return call mov 6-@1-@2, @2, @3-1 /* [↑] this subroutine uses recursion.*/</lang>
'''{{out|output''' |text=&nbsp; when using the default input:}}
end
return</lang>
'''output''' &nbsp; when using the default input:
<pre>
step 1: move disk on tower 1 ───► 3