Solve the no connection puzzle: Difference between revisions

Content added Content deleted
m (→‎unannotated solutions: added/changed comments and whitespace.)
m (→‎unannotated solutions: added/changed comments, whitespace, and indentations.)
Line 1,454: Line 1,454:


===annotated solutions===
===annotated solutions===
<lang rexx>/*REXX program solves the "no-connection" puzzle (with eight pegs). */
<lang rexx>/*REXX program solves the "no-connection" puzzle (the puzzle has eight pegs). */
@abc='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
@abc='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
parse arg limit . /*# solutions*/ /* ╔═══════════════════════════╗ */
parse arg limit . /*number of solutions wanted.*/ /* ╔═══════════════════════════╗ */
if limit=='' then limit=1 /* ║ A B ║ */
if limit=='' | limit=="." then limit=1 /* ║ A B ║ */
oLimit=limit; limit=abs(limit) /* ║ /│\ /│\ ║ */
oLimit=limit; limit=abs(limit) /* ║ /│\ /│\ ║ */
@. = /* ║ / │ \/ │ \ ║ */
@. = /* ║ / │ \/ │ \ ║ */
@.1 = 'A C D E' /* ║ / │ /\ │ \ ║ */
@.1 = 'A C D E' /* ║ / │ /\ │ \ ║ */
@.2 = 'B D E F' /* ║ / │/ \│ \ ║ */
@.2 = 'B D E F' /* ║ / │/ \│ \ ║ */
@.3 = 'C A D G' /* ║ C────D────E────F ║ */
@.3 = 'C A D G' /* ║ C────D────E────F ║ */
@.4 = 'D A B C E G' /* ║ \ │\ /│ / ║ */
@.4 = 'D A B C E G' /* ║ \ │\ /│ / ║ */
@.5 = 'E A B D F H' /* ║ \ │ \/ │ / ║ */
@.5 = 'E A B D F H' /* ║ \ │ \/ │ / ║ */
@.6 = 'F B E G' /* ║ \ │ /\ │ / ║ */
@.6 = 'F B E G' /* ║ \ │ /\ │ / ║ */
@.7 = 'G C D E' /* ║ \│/ \│/ ║ */
@.7 = 'G C D E' /* ║ \│/ \│/ ║ */
@.8 = 'H D E F' /* ║ G H ║ */
@.8 = 'H D E F' /* ║ G H ║ */
cnt=0 /* ╚═══════════════════════════╝ */
cnt=0 /* ╚═══════════════════════════╝ */
do nodes=1 while @.nodes\==''; _=word(@.nodes,1)
do nodes=1 while @.nodes\==''; _=word(@.nodes,1)
subs=0 /* [↓] create list of node paths*/
subs=0
do #=1 for words(@.nodes)-1
do #=1 for words(@.nodes)-1 /*create list of node paths.*/
__=word(@.nodes,#+1); if __>_ then iterate
__=word(@.nodes,#+1); if __>_ then iterate
subs=subs+1; !._.subs=__
subs=subs + 1; !._.subs=__
end /*#*/
end /*#*/
!._.0=subs /*assign the number of node paths*/
!._.0=subs /*assign the number of the node paths. */
end /*nodes*/
end /*nodes*/
pegs=nodes-1 /*number of pegs to be seated. */
pegs=nodes-1 /*the number of pegs to be seated. */
do a=1 for pegs; if ?('A') then iterate
_=' ' /*_ is used for indenting the output. */
do b=1 for pegs; if ?('B') then iterate
do a=1 for pegs; if ?('A') then iterate
do c=1 for pegs; if ?('C') then iterate
do b=1 for pegs; if ?('B') then iterate
do d=1 for pegs; if ?('D') then iterate
do c=1 for pegs; if ?('C') then iterate
do e=1 for pegs; if ?('E') then iterate
do d=1 for pegs; if ?('D') then iterate
do f=1 for pegs; if ?('F') then iterate
do e=1 for pegs; if ?('E') then iterate
do g=1 for pegs; if ?('G') then iterate
do f=1 for pegs; if ?('F') then iterate
do h=1 for pegs; if ?('H') then iterate
do g=1 for pegs; if ?('G') then iterate
call showNodes
do h=1 for pegs; if ?('H') then iterate
cnt=cnt+1; if cnt==limit then leave a
call showNodes
end /*h*/
cnt=cnt+1; if cnt==limit then leave a
end /*g*/
end /*h*/
end /*f*/
end /*g*/
end /*e*/
end /*f*/
end /*d*/
end /*e*/
end /*c*/
end /*d*/
end /*b*/
end /*c*/
end /*a*/
end /*b*/
say /*display a blank line to screen.*/
end /*a*/
s=left('s',cnt\==1) /*handle case of plurals (or not)*/
say /*display a blank line to the terminal.*/
say 'found ' cnt " solution"s'.' /*display the number of solutions*/
s=left('s',cnt\==1) /*handle the case of plurals (or not).*/
exit /*stick a fork in it, we're done.*/
say 'found ' cnt " solution"s'.' /*display the number of solutions found*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────? subroutine────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
?: parse arg node; nn=value(node); nL=nn-1; nH=nn+1
?: parse arg node; nn=value(node)
do cn=c2d('A') to c2d(node)-1; if value(d2c(cn))==nn then return 1; end
nH=nn+1
/* [↑] see if any are duplicates*/
do ch=1 for !.node.0 /* [↓] see if any ¬ = ±1 value*/
do cn=c2d('A') to c2d(node)-1; if value( d2c(cn) )==nn then return 1
$=!.node.ch; fn=value($) /*node name and its current peg#.*/
end /*cn*/ /* [↑] see if there're any duplicates.*/
nL=nn-1
if nL==fn | nH==fn then return 1 /*if ≡ ±1, then it can't be used.*/
end /*ch*/ /* [] looking for suitable num.*/
do ch=1 for !.node.0 /* [] see if there any ¬= ±1 values.*/
return 0 /*the sub arg value passed is OK.*/
$=!.node.ch; fn=value($) /*the node name and its current peg #.*/
if nL==fn | nH==fn then return 1 /*if ≡ ±1, then the node can't be used.*/
/*──────────────────────────────────SHOWNODES subroutine────────────────*/
showNodes: _=' ' /*_ is used for padding output.*/
end /*ch*/ /* [↑] looking for suitable number. */
show=0 /*indicates graph not found yet. */
return 0 /*the subroutine arg value passed is OK*/
/*──────────────────────────────────────────────────────────────────────────────────────*/

showNodes: _=' ' /*_ is used for padding the output. */
do box=1 for sourceline() while oLimit<0 /*Negative? Then show it*/
xw=sourceline(box) /*get a line of this REXX program*/
show=0 /*indicates no graph has been found yet*/
do box=1 for sourceline() while oLimit<0 /*Negative? Then display the diagram. */
p2=lastpos('*',xw) /*position of last asterisk.*/
p1=lastpos('*',xw,max(1,p2-1)) /* " " penultimate " */
xw=sourceline(box) /*get a source line of this program. */
if pos('', xw)\==0 then show=1 /*Found the top-left box corner? */
p2=lastpos('*', xw) /*the position of last asterisk.*/
if \show then iterate /*Not found? Then skip this line*/
p1=lastpos('*', xw, max(1, p2-1) ) /* " " " penultimate " */
xb=substr(xw, p1+1, p2-p1-2) /*extract the "box" part of line.*/
if pos('╔', xw)\==0 then show=1 /*Have found the top-left box corner ? */
xt=xb /*get a working copy of the box. */
if \show then iterate /*Not found? Then skip this line. */
do jx=1 for pegs /*do a substitution for all pegs.*/
xb=substr(xw, p1+1, p2-p1-2) /*extract the "box" part of line. */
aa=substr(@abc,jx,1) /*get the name of the peg (A──►Z)*/
xt=xb /*get a working copy of the box. */
do jx=1 for pegs /*do a substitution for all the pegs. */
xt=translate(xt,value(aa),aa) /*substitute peg name with value.*/
end /*jx*/ /* [↑] graph limited to 26 nodes*/
@=substr(@abc, jx, 1) /*get the name of the peg (A ──► Z). */
say _ xb _ _ xt /*display one line of the graph. */
xt=translate(xt,value(@),@) /*substitute the peg name with a value.*/
if pos('╝',xw)\==0 then return /*Last line of graph? Then stop.*/
end /*jx*/ /* [↑] graph is limited to 26 nodes.*/
say _ xb _ _ xt /*display one line of the graph. */
end /*box*/
/* [↓] show a simple solution. */
if pos('╝', xw)\==0 then return /*Is this last line of graph? Then stop*/
end /*box*/
say _ 'a='a _ 'b='||b _ 'c='c _ 'd='d _ 'e='e _ 'f='f _ 'g='g _ 'h='h</lang>
say _ 'a='a _ 'b='||b _ 'c='c _ 'd='d _ ' e='e _ 'f='f _ 'g='g _ 'h='h
'''output''' when the input is: &nbsp; <tt> -1 </tt>
return</lang>
'''output''' when the input used is: &nbsp; <tt> -1 </tt>
<pre>
<pre>
╔═══════════════════════════╗ ╔═══════════════════════════╗
╔═══════════════════════════╗ ╔═══════════════════════════╗