Jump to content

Solve the no connection puzzle: Difference between revisions

m
→‎unannotated solutions: added/changed comments, whitespace, and indentations.
m (→‎unannotated solutions: added/changed comments and whitespace.)
m (→‎unannotated solutions: added/changed comments, whitespace, and indentations.)
Line 1,454:
 
===annotated solutions===
<lang rexx>/*REXX program solves the "no-connection" puzzle (withthe puzzle has eight pegs). */
@abc='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
parse arg limit . /*#number of solutions wanted.*/ /* ╔═══════════════════════════╗ */
if limit=='' | limit=="." then limit=1 /* ║ A B ║ */
oLimit=limit; limit=abs(limit) /* ║ /│\ /│\ ║ */
@. = /* ║ / │ \/ │ \ ║ */
@.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. */
_=' ' do a=1 for pegs; if ?('A') then iterate /*_ is used for indenting the output. */
do do ba=1 for pegs; if ?('BA') then iterate
do do cb=1 for pegs; if ?('CB') then iterate
do do dc=1 for pegs; if ?('DC') then iterate
do do ed=1 for pegs; if ?('ED') then iterate
do do fe=1 for pegs; if ?('FE') then iterate
do do gf=1 for pegs; if ?('GF') then iterate
do do hg=1 for pegs; if ?('HG') then iterate
do h=1 for pegs; call showNodes if ?('H') then iterate
call cnt=cnt+1; if cnt==limit then leave ashowNodes
cnt=cnt+1; end /*h*/if cnt==limit then leave a
end /*gh*/
end /*fg*/
end /*ef*/
end /*de*/
end /*cd*/
end /*bc*/
end /*ab*/
say end /*display a blank line to screen.*/
s=left('s',cnt\==1)say /*handledisplay a caseblank ofline pluralsto (orthe not)terminal.*/
say s=left('found s',cnt\==1) cnt " solution"s'.' /*displayhandle the numbercase of solutionsplurals (or not).*/
exit say 'found ' cnt " solution"s'.' /*stickdisplay athe forknumber inof it,solutions we're done.found*/
exit /*stick a fork [↑]in it, see ifwe're anyall aredone. duplicates*/
/*──────────────────────────────────? subroutine────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
?: parse arg node; nn=value(node); nL=nn-1; nH=nn+1
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 do cn=c2d('A') to c2d(node)-1; if value( d2c(cn) /*)==nn [↓] then seereturn if any ¬ = ±1 value*/
$=!.node.ch; fn=value($) end /*nodecn*/ /* [↑] namesee andif itsthere're currentany peg#duplicates.*/
nL=nn-1
if nL==fn | nH==fn then return 1 /*if ≡ ±1, then it can't be used.*/
end /*ch*/ do ch=1 for !.node.0 /* [] lookingsee forif there any ¬= ±1 suitable numvalues.*/
return 0 $=!.node.ch; fn=value($) /*the subnode argname value passedand its current ispeg OK#.*/
if nL==fn | nH==fn then return 1 /*if ≡ ±1, then itthe node can't be used.*/
/*──────────────────────────────────SHOWNODES subroutine────────────────*/
showNodes: _=' ' end /*ch*/ /*_ [↑] is usedlooking for paddingsuitable outputnumber. */
show= return 0 /*indicatesthe subroutine grapharg notvalue foundpassed yet.is OK*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
 
showNodes: _=' ' /*_ is used for padding the output. */
do box=1 for sourceline() while oLimit<0 /*Negative? Then show it*/
show=0 xw=sourceline(box) /*getindicates ano linegraph ofhas thisbeen REXXfound programyet*/
do box=1 for sourceline() while oLimit<0 /*Negative? Then showdisplay the diagram. it*/
p2=lastpos('*',xw) /*position of last asterisk.*/
p1 xw=lastpossourceline('*',xw,max(1,p2-1)box) /* " " penultimate " /*get a source line of this program. */
if pos p2=lastpos('*', xw)\==0 then show=1 /*Found the top-leftposition of last box corner? asterisk.*/
if \show p1=lastpos('*', xw, max(1, p2-1) ) then iterate /*Not found?" " " penultimate " Then skip this line*/
xb=substr if pos(xw'╔', p1+xw)\==0 then show=1, p2-p1-2) /*extractHave found the "top-left box" partcorner of? line.*/
xt=xb if \show then iterate /*getNot afound? working copyThen ofskip thethis boxline. */
do jxxb=substr(xw, p1+1, p2-p1-2) for pegs /*doextract athe substitution for"box" all pegspart of line. */
xt=xb aa=substr(@abc,jx,1) /*get thea nameworking copy of the pegbox. (A──►Z)*/
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*/ @=substr(@abc, jx, 1) /*get the name of the [↑]peg graph(A limited──► toZ). 26 nodes*/
say _ xb _ _ xt xt=translate(xt,value(@),@) /*displaysubstitute onethe linepeg ofname thewith grapha value. */
if pos('╝',xw)\==0 then return end /*Lastjx*/ /* [↑] line of graph? is limited to Then26 stopnodes.*/
say _ xb _ _ xt /*display one line of the graph. */
end /*box*/
if pos('╝', xw)\==0 then return /*Is [↓] this last showline aof simplegraph? solution.Then stop*/
end /*box*/
say _ 'a='a _ 'b='||b _ 'c='c _ 'd='d _ ' e='e _ 'f='f _ 'g='g _ 'h='h</lang>
'''output''' when the input is: &nbsp; <tt> -1 </tt>
return</lang>
'''output''' when the input used is: &nbsp; <tt> -1 </tt>
<pre>
╔═══════════════════════════╗ ╔═══════════════════════════╗
Cookies help us deliver our services. By using our services, you agree to our use of cookies.