Jump to content

Solve the no connection puzzle: Difference between revisions

m
→‎unannotated solutions: added/changed comments, whitespace, and indentations.
m (J: kibitz)
m (→‎unannotated solutions: added/changed comments, whitespace, and indentations.)
Line 1,367:
=={{header|REXX}}==
===unannotated solutions===
<lang rexx>/*REXX program solves the "no-connection" puzzle (withthe puzzle has eight pegs). */
parse arg limit . /*#number of solutions wanted.*/ /* ╔═══════════════════════════╗ */
if limit=='' | lim=t''"." then limit=1 /* ║ A B ║ */
/* ║ /│\ /│\ ║ */
@. = /* ║ / │ \/ │ \ ║ */
@.1 = 'A C D E' /* ║ / │ /\ │ \ ║ */
@.2 = 'B D E F' /* ║ / │/ \│ \ ║ */
@.3 = 'C A D G' /* ║ C────D────E────F ║ */
@.4 = 'D A B C E G' /* ║ \ │\ /│ / ║ */
@.5 = 'E A B D F H' /* ║ \ │ \/ │ / ║ */
@.6 = 'F B E G' /* ║ \ │ /\ │ / ║ */
@.7 = 'G C D E' /* ║ \│/ \│/ ║ */
@.8 = 'H D E F' /* ║ G H ║ */
cnt=0 /* ╚═══════════════════════════╝ */
do nodes=1 while @.nodes\==''; _=word(@.nodes,1)
subs=0 /* [↓] create list of node paths*/
do #=1 for words(@.nodes)-1 /*create list of node paths.*/
__=word(@.nodes,#+1); if __>_ then iterate
subs=subs + 1; !._.subs=__
end /*#*/
!._.0=subs /*assign the number of the node paths. */
end /*nodes*/
pegs=nodes-1 /*the number of pegs to be seated. */
_=' ' /*_ is used for paddingindenting the output. */
do a=1 for pegs; if ?('A') then iterate
do b=1 for pegs; if ?('B') then iterate
do c=1 for pegs; if ?('C') then iterate
do d=1 for pegs; if ?('D') then iterate
do e=1 for pegs; if ?('E') then iterate
do f=1 for pegs; if ?('F') then iterate
do g=1 for pegs; if ?('G') 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
cnt=cnt+1; if cnt==limit then leave a
end /*h*/
end /*g*/
Line 1,409:
end /*b*/
end /*a*/
say /*display a blank line to the screen. */
s=left('s',cnt\==1) /*handle the case of plurals (or not). */
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
nH=nn+1
do cn=c2d('A') to c2d(node)-1; if value( d2c(cn) )==nn then return 1; end
/* [↑] see if any are duplicates*/
do ch=1 for !.node.0 end /*cn*/ /* [] see if there any are ¬ = ±1 valueduplicates.*/
nL=nn-1
$=!.node.ch; fn=value($) /*node name and its current peg#.*/
if nL==fn | nH==fn then return do ch=1 for !.node.0 /* [↓] see if there ±1,any then it¬= can't±1 be usedvalues.*/
end /*ch*/ $=!.node.ch; fn=value($) /*the node name /* [↑]and lookingits forcurrent suitablepeg num#.*/
return 0 if nL==fn | nH==fn then return 1 /*if ±1, then /*the subnode argcan't valuebe passed is OKused.*/</lang>
end /*ch*/ /* [↑] seelooking for suitable number. if any are duplicates*/
'''output''' when using the default input:
return 0 /*the subroutine arg value passed is OK.*/</lang>
'''output''' &nbsp; when using the default input:
<pre>
a=3 b=4 c=7 d=1 e=8 f=2 g=5 h=6
Line 1,429 ⟶ 1,431:
</pre>
 
'''output''' &nbsp; when using the input of: &nbsp; <tt> 999 </tt>
<pre>
a=3 b=4 c=7 d=1 e=8 f=2 g=5 h=6
Cookies help us deliver our services. By using our services, you agree to our use of cookies.