Solve the no connection puzzle: Difference between revisions

Content added Content deleted
(→‎{{header|REXX}}: add/changed whitespace and comments, align statements, used templates for the output sections.)
Line 3,253: Line 3,253:
=={{header|REXX}}==
=={{header|REXX}}==
===unannotated solutions===
===unannotated solutions===
<lang rexx>/*REXX program solves the "no-connection" puzzle (the puzzle has eight pegs). */
<lang rexx>/*REXX program solves the "no─connection" puzzle (the puzzle has eight pegs). */
parse arg limit . /*number of solutions wanted.*/ /* ╔═══════════════════════════╗ */
parse arg limit . /*number of solutions wanted.*/ /* ╔═══════════════════════════╗ */
if limit=='' | limit=="." then limit=1 /* ║ A B ║ */
if limit=='' | limit=="," then limit= 1 /* ║ A B ║ */
/* ║ /│\ /│\ ║ */
/* ║ /│\ /│\ ║ */
@. = /* ║ / │ \/ │ \ ║ */
@. = /* ║ / │ \/ │ \ ║ */
Line 3,266: Line 3,266:
@.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 pegs=1 while @.pegs\==''; _=word(@.pegs,1)
do pegs=1 while @.pegs\==''; _= word(@.pegs, 1)
subs=0
subs= 0
do #=1 for words(@.pegs) -1 /*create list of node paths.*/
do #=1 for words(@.pegs) -1 /*create list of node paths.*/
__=word(@.pegs, # + 1); if __>_ then iterate
__= word(@.pegs, # + 1); if __>_ then iterate
subs=subs + 1; !._.subs=__
subs= subs + 1; !._.subs= __
end /*#*/
end /*#*/
!._.0=subs /*assign the number of the node paths. */
!._.0= subs /*assign the number of the node paths. */
end /*pegs*/
end /*pegs*/
pegs=pegs-1 /*the number of pegs to be seated. */
pegs= pegs - 1 /*the number of pegs to be seated. */
_=' ' /*_ is used for indenting the output.*/
_= ' ' /*_ is used for indenting the output.*/
do a=1 for pegs; if ?('A') then iterate
do a=1 for pegs; if ?('A') then iterate
do b=1 for pegs; if ?('B') then iterate
do b=1 for pegs; if ?('B') then iterate
do c=1 for pegs; if ?('C') then iterate
do c=1 for pegs; if ?('C') then iterate
do d=1 for pegs; if ?('D') then iterate
do d=1 for pegs; if ?('D') then iterate
do e=1 for pegs; if ?('E') then iterate
do e=1 for pegs; if ?('E') then iterate
do f=1 for pegs; if ?('F') then iterate
do f=1 for pegs; if ?('F') then iterate
do g=1 for pegs; if ?('G') then iterate
do g=1 for pegs; if ?('G') then iterate
do h=1 for pegs; if ?('H') then iterate
do h=1 for pegs; if ?('H') then iterate
say _ 'a='a _ 'b='||b _ 'c='c _ 'd='d _ 'e='e _ 'f='f _ 'g='g _ 'h='h
say _ 'a='a _ "b="||b _ 'c='c _ "d="d _ 'e='e _ "f="f _ 'g='g _ "h="h
cnt=cnt+1; if cnt==limit then leave a
cnt= cnt + 1; if cnt==limit then leave a
end /*h*/
end /*h*/
end /*g*/
end /*g*/
end /*f*/
end /*f*/
end /*e*/
end /*e*/
end /*d*/
end /*d*/
end /*c*/
end /*c*/
end /*b*/
end /*b*/
end /*a*/
end /*a*/
say /*display a blank line to the terminal.*/
say /*display a blank line to the terminal.*/
s= left('s', cnt\==1) /*handle the case of plurals (or not).*/
s= left('s', cnt\==1) /*handle the case of plurals (or not).*/
say 'found ' cnt " solution"s'.' /*display the number of solutions found*/
say 'found ' cnt " solution"s'.' /*display the number of solutions found*/
exit /*stick a fork in it, we're all done. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
?: parse arg node; nn=value(node)
?: parse arg node; nn= value(node)
nH=nn+1
nH= nn+1
do cn=c2d('A') to c2d(node) - 1; if value( d2c(cn) )==nn then return 1
do cn=c2d('A') to c2d(node)-1; if value( d2c(cn) )==nn then return 1
end /*cn*/ /* [↑] see if there any are duplicates.*/
end /*cn*/ /* [↑] see if there any are duplicates.*/
nL=nn-1
nL= nn-1
do ch=1 for !.node.0 /* [↓] see if there any ¬= ±1 values.*/
do ch=1 for !.node.0 /* [↓] see if there any ¬= ±1 values.*/
$=!.node.ch; fn=value($) /*the node name and its current peg #.*/
$= !.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.*/
if nL==fn | nH==fn then return 1 /*if ≡ ±1, then the node can't be used.*/
end /*ch*/ /* [↑] looking for suitable number. */
end /*ch*/ /* [↑] looking for suitable number. */
return 0 /*the subroutine arg value passed is OK.*/</lang>
return 0 /*the subroutine arg value passed is OK.*/</lang>
'''output''' &nbsp; when using the default input:
{{out|output|text=&nbsp; when using the default input:}}
<pre>
<pre>
a=3 b=4 c=7 d=1 e=8 f=2 g=5 h=6
a=3 b=4 c=7 d=1 e=8 f=2 g=5 h=6
Line 3,316: Line 3,316:
found 1 solution.
found 1 solution.
</pre>
</pre>
{{out|output|text=&nbsp; when using the default input of: &nbsp; &nbsp; <tt> 999 </tt>}}

'''output''' &nbsp; when using the input of: &nbsp; <tt> 999 </tt>
<pre>
<pre>
a=3 b=4 c=7 d=1 e=8 f=2 g=5 h=6
a=3 b=4 c=7 d=1 e=8 f=2 g=5 h=6
Line 3,341: Line 3,340:
===annotated solutions===
===annotated solutions===
Usage note: &nbsp; if the &nbsp; '''limit''' &nbsp; (the 1<sup>st</sup> argument) &nbsp; is negative, a diagram (node graph) is shown.
Usage note: &nbsp; if the &nbsp; '''limit''' &nbsp; (the 1<sup>st</sup> argument) &nbsp; is negative, a diagram (node graph) is shown.
<lang rexx>/*REXX program solves the "no-connection" puzzle (the puzzle has eight pegs). */
<lang rexx>/*REXX program solves the "no─connection" puzzle (the puzzle has eight pegs). */
@abc='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
@abc= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
parse arg limit . /*number of solutions wanted.*/ /* ╔═══════════════════════════╗ */
parse arg limit . /*number of solutions wanted.*/ /* ╔═══════════════════════════╗ */
if limit=='' | 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' /* ║ / │ /\ │ \ ║ */
Line 3,355: Line 3,354:
@.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 pegs=1 while @.pegs\==''; _=word(@.pegs, 1)
do pegs=1 while @.pegs\==''; _= word(@.pegs, 1)
subs=0
subs= 0
do #=1 for words(@.pegs) -1 /*create list of node paths.*/
do #=1 for words(@.pegs) -1 /*create list of node paths.*/
__=word(@.pegs, #+1); if __>_ then iterate
__= word(@.pegs, #+1); if __>_ then iterate
subs=subs + 1; !._.subs=__
subs= subs + 1; !._.subs= __
end /*#*/
end /*#*/
!._.0=subs /*assign the number of the node paths. */
!._.0= subs /*assign the number of the node paths. */
end /*pegs*/
end /*pegs*/
pegs=pegs - 1 /*the number of pegs to be seated. */
pegs= pegs - 1 /*the number of pegs to be seated. */
_=' ' /*_ is used for indenting the output. */
_= ' ' /*_ is used for indenting the output. */
do a=1 for pegs; if ?('A') then iterate
do a=1 for pegs; if ?('A') then iterate
do b=1 for pegs; if ?('B') then iterate
do b=1 for pegs; if ?('B') then iterate
do c=1 for pegs; if ?('C') then iterate
do c=1 for pegs; if ?('C') then iterate
do d=1 for pegs; if ?('D') then iterate
do d=1 for pegs; if ?('D') then iterate
do e=1 for pegs; if ?('E') then iterate
do e=1 for pegs; if ?('E') then iterate
do f=1 for pegs; if ?('F') then iterate
do f=1 for pegs; if ?('F') then iterate
do g=1 for pegs; if ?('G') then iterate
do g=1 for pegs; if ?('G') then iterate
do h=1 for pegs; if ?('H') then iterate
do h=1 for pegs; if ?('H') then iterate
call showNodes
call showNodes
cnt=cnt+1; if cnt==limit then leave a
cnt= cnt + 1; if cnt==limit then leave a
end /*h*/
end /*h*/
end /*g*/
end /*g*/
end /*f*/
end /*f*/
end /*e*/
end /*e*/
end /*d*/
end /*d*/
end /*c*/
end /*c*/
end /*b*/
end /*b*/
end /*a*/
end /*a*/
say /*display a blank line to the terminal.*/
say /*display a blank line to the terminal.*/
s=left('s', cnt\==1) /*handle the case of plurals (or not).*/
s= left('s', cnt\==1) /*handle the case of plurals (or not).*/
say 'found ' cnt " solution"s'.' /*display the number of solutions found*/
say 'found ' cnt " solution"s'.' /*display the number of solutions found*/
exit /*stick a fork in it, we're all done. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
?: parse arg node; nn=value(node)
?: parse arg node; nn= value(node)
nH=nn+1
nH= nn+1
do cn=c2d('A') to c2d(node)-1; if value( d2c(cn) )==nn then return 1
do cn=c2d('A') to c2d(node)-1; if value( d2c(cn) )==nn then return 1
end /*cn*/ /* [↑] see if there're any duplicates.*/
end /*cn*/ /* [↑] see if there're any duplicates.*/
nL=nn-1
nL= nn-1
do ch=1 for !.node.0 /* [↓] see if there any ¬= ±1 values.*/
do ch=1 for !.node.0 /* [↓] see if there any ¬= ±1 values.*/
$=!.node.ch; fn=value($) /*the node name and its current peg #.*/
$= !.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.*/
if nL==fn | nH==fn then return 1 /*if ≡ ±1, then the node can't be used.*/
end /*ch*/ /* [↑] looking for suitable number. */
end /*ch*/ /* [↑] looking for suitable number. */
return 0 /*the subroutine arg value passed is OK*/
return 0 /*the subroutine arg value passed is OK*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
showNodes: _=left('', 5) /*_ is used for padding the output. */
showNodes: _= left('', 5) /*_ is used for padding the output. */
show=0 /*indicates no graph has been found yet*/
show= 0 /*indicates no graph has been found yet*/
do box=1 for sourceline() while oLimit<0 /*Negative? Then display the diagram. */
do box=1 for sourceline() while oLimit<0 /*Negative? Then display the diagram. */
xw=sourceline(box) /*get a source line of this program. */
xw= sourceline(box) /*get a source line of this program. */
p2=lastpos('*', xw) /*the position of last asterisk.*/
p2= lastpos('*', xw) /*the position of last asterisk.*/
p1=lastpos('*', xw, max(1, p2-1) ) /* " " " penultimate " */
p1= lastpos('*', xw, max(1, p2-1) ) /* " " " penultimate " */
if pos('╔', xw)\==0 then show=1 /*Have found the top-left box corner ? */
if pos('╔', xw)\==0 then show= 1 /*Have found the top-left box corner ? */
if \show then iterate /*Not found? Then skip this line. */
if \show then iterate /*Not found? Then skip this line. */
xb=substr(xw, p1+1, p2-p1-2) /*extract the "box" part of line. */
xb= substr(xw, p1+1, p2-p1-2) /*extract the "box" part of line. */
xt=xb /*get a working copy of the box. */
xt= xb /*get a working copy of the box. */
do jx=1 for pegs /*do a substitution for all the pegs. */
do jx=1 for pegs /*do a substitution for all the pegs. */
@=substr(@abc, jx, 1) /*get the name of the peg (A ──► Z). */
@= substr(@abc, jx, 1) /*get the name of the peg (A ──► Z). */
xt=translate(xt,value(@),@) /*substitute the peg name with a value.*/
xt= translate(xt, value(@), @) /*substitute the peg name with a value.*/
end /*jx*/ /* [↑] graph is limited to 26 nodes.*/
end /*jx*/ /* [↑] graph is limited to 26 nodes.*/
say _ xb _ _ xt /*display one line of the graph. */
say _ xb _ _ xt /*display one line of the graph. */
if pos('╝', xw)\==0 then return /*Is this last line of graph? Then stop*/
if pos('╝', xw)\==0 then return /*Is this last line of graph? Then stop*/
end /*box*/
end /*box*/
say _ 'a='a _ 'b='||b _ 'c='c _ 'd='d _ ' e='e _ 'f='f _ 'g='g _ 'h='h
say _ 'a='a _ "b="||b _ 'c='c _ "d="d _ ' e='e _ "f="f _ 'g='g _ "h="h
return</lang>
return</lang>
'''output''' when using the input of: &nbsp; <tt> -3 </tt>
{{out|output|text=&nbsp; when using the default inputs of: &nbsp; &nbsp; <tt> -1 </tt>}}
<pre>
<pre>
╔═══════════════════════════╗ ╔═══════════════════════════╗
╔═══════════════════════════╗ ╔═══════════════════════════╗