Solve the no connection puzzle: Difference between revisions

m (added whitespace and highlighting, minimized the word "to", added two section header (;Objective: and ;Example:), used better glyphs for the two diagrams,)
Line 1,291:
 
=={{header|M2000 Interpreter}}==
Final Version, print all solutions (16 from 40320 permutations)
Addition a reducing algorithm for double connections, if A has D then from D we remove A unless D has only A so we remove D from A, unless A has only D.
 
Press space bar to see solutions so far.
<lang M2000 Interpreter>
Module no_connection_puzzle {
Line 1,297 ⟶ 1,299:
Inventory Connections="A":="CDE","B":="DEF","C":="ADG", "D":="ABCEGH"
Append Connections, "E":="ABDFGH","F":="HEB", "G":="CDE","H":="DEF"
Inventory ToDelete, Solutions
\\ eliminate double connectionsconnnections
con=each(Connections)
While con {
m$=eval$(con, con^)
c$=eval$(con)
if donelen(c$)=0 then exitcontinue
for i=1 to len(C$) {
d$=mid$(c$,i,1)
if d$="*" then continue
r$=Filter$(Connections$(d$), m$)
if r$=<>"" then {
c1$=filter$(c$ Return connections, d$) :=r$
} else if c1$<>"" then Return connections, m$:=c1$: c$=c1${
} else if m$=connections$(d$) then {
Return connections, d$:=r"*" : if not exist(todelete, d$) then Append todelete, d$
}
}
}
}
con=each(todelete)
While con {
Delete Connections, eval$(con)
}
Inventory Holes
Line 1,324 ⟶ 1,334:
=res
}
a=(1,3,2,53,4,5,6,7,8)
h=(,)
solution=(,)
done=false
counter=0
Print "Waut..."
P(h, a)
If done then Dispsol=Each(solutionSolutions)
While sol {
Print "Solution:";sol^+1
Disp(Eval(Solutions))
aa$=key$
}
Sub P(h, a)
if done then exit sub
if len(a)<=1 then process(cons(h, a)) : exit sub
local b=cons(a)
Line 1,338 ⟶ 1,353:
b=cons(cdr(b),car(b))
P(cons(h,car(b)), cdr(b))
if done then exit
}
End sub
Sub Process(a)
Rem Print acounter++
hole=each(Holes)Print counter
if keypress(32) then {
local sol=Each(Solutions)
While sol {
Print "Solution:";sol^+1
Disp(Eval(Solutions))
aa$=key$
}
}
hole=each(Connections)
done=True
While hole {
if not CheckValid(Eval$(hole, hole^), a) then done=false : exit
}
if done then solutionAppend Solutions, Len(Solutions):=a : Print a
end sub
Sub Disp(a)
Line 1,363 ⟶ 1,386:
}
no_connection_puzzle
 
</lang>
{{out}}
Anonymous user