Jump to content

Solve the no connection puzzle: Difference between revisions

no edit summary
(→‎{{header|Perl 6}}: Syntax fix, "0" is true now. (didn't actually affect this particular problem))
No edit summary
Line 1,286:
</pre>
 
=={{header|M2000 Interpreter}}==
<lang M2000 Interpreter>
Module no_connection_puzzle {
\\ Holes
Inventory Connections="A":="CDE","B":="DEF","C":="ADG", "D":="ABCEGH"
Append Connections, "E":="ABDFGH","F":="HEB", "G":="CDE","H":="DEF"
Inventory Holes
For i=0 to 7 : Append Holes, Chr$(65+i):=i : Next i
CheckValid=lambda Holes, Connections (a$, arr) -> {
val=Array(arr, Holes(a$))
con$=Connections$(a$)
res=true
For i=1 to Len(con$) {
if Abs(Array(Arr, Holes(mid$(con$,i,1)))-val)<2 then res=false: exit
}
=res
}
a=(1,3,2,5,4,6,7,8)
h=(,)
solution=(,)
done=false
Print "Waut..."
P(h, a)
If done then Disp(solution)
Sub P(h, a)
if done then exit sub
if len(a)<=1 then process(cons(h, a)) : exit sub
local b=cons(a)
For i=1 to len(b) {
b=cons(cdr(b),car(b))
P(cons(h,car(b)), cdr(b))
if done then exit
}
End sub
Sub Process(a)
Rem Print a
hole=each(Holes)
done=True
While hole {
if not CheckValid(Eval$(hole, hole^), a) then done=false : exit
}
if done then solution=a
end sub
Sub Disp(a)
Print format$(" {0} {1}", array(a), array(a,1))
Print " /|\ /|\"
Print " / | X | \"
Print " / |/ \| \"
Print Format$("{0} - {1} - {2} - {3}", array(a,2),array(a,3), array(a,4), array(a,5))
Print " \ |\ /| /"
Print " \ | X | /"
Print " \|/ \|/"
Print Format$(" {0} {1}", array(a,6), array(a,7))
End sub
}
no_connection_puzzle
</lang>
{{out}}
<pre>
3 5
/|\ /|\
/ | X | \
/ |/ \| \
7 - 1 - 8 - 2
\ |\ /| /
\ | X | /
\|/ \|/
4 6
</pre >
=={{header|Mathematica}}==
This one simply takes all permutations of the pegs and filters out invalid solutions.
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.