Solve the no connection puzzle: Difference between revisions

Content added Content deleted
m (J: kibitz)
m (→‎unannotated solutions: added/changed comments, whitespace, and indentations.)
Line 1,367: Line 1,367:
=={{header|REXX}}==
=={{header|REXX}}==
===unannotated solutions===
===unannotated 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). */
parse arg limit . /*# solutions*/ /* ╔═══════════════════════════╗ */
parse arg limit . /*number of solutions wanted.*/ /* ╔═══════════════════════════╗ */
if limit=='' then limit=1 /* ║ A B ║ */
if limit=='' | lim=t''"." then limit=1 /* ║ A B ║ */
/* ║ /│\ /│\ ║ */
/* ║ /│\ /│\ ║ */
@. = /* ║ / │ \/ │ \ ║ */
@. = /* ║ / │ \/ │ \ ║ */
@.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. */
_=' ' /*_ is used for padding 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*/
Line 1,409: Line 1,409:
end /*b*/
end /*b*/
end /*a*/
end /*a*/
say /*display a blank line to screen.*/
say /*display a blank line to the screen. */
s=left('s',cnt\==1) /*handle 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*/
say 'found ' cnt " solution"s'.' /*display the number of solutions found.*/
exit /*stick a fork in it, we're done.*/
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)
nH=nn+1
do cn=c2d('A') to c2d(node)-1; if value(d2c(cn))==nn then return 1; end
do cn=c2d('A') to c2d(node)-1; if value( d2c(cn) )==nn then return 1
/* [↑] see if any are duplicates*/
do ch=1 for !.node.0 /* [] see if any ¬ = ±1 value*/
end /*cn*/ /* [] see if there any are duplicates.*/
nL=nn-1
$=!.node.ch; fn=value($) /*node name and its current peg#.*/
if nL==fn | nH==fn then return 1 /*if ±1, then it can't be used.*/
do ch=1 for !.node.0 /* [↓] see if there any ¬= ±1 values.*/
end /*ch*/ /* [↑] looking for suitable num.*/
$=!.node.ch; fn=value($) /*the node name and its current peg #.*/
return 0 /*the sub arg value passed is OK.*/</lang>
if nL==fn | nH==fn then return 1 /*if ±1, then the node can't be used.*/
end /*ch*/ /* [↑] looking for suitable number. */
'''output''' when using the default input:
return 0 /*the subroutine arg value passed is OK.*/</lang>
'''output''' &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 1,429: Line 1,431:
</pre>
</pre>


'''output''' when using the input of: &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