Solve the no connection puzzle: Difference between revisions

(Added Chapel Example)
Line 871:
6 5
44 tries</pre>
 
=={{header|Phix}}==
Brute force solution. I ordered the links highest letter first, then grouped by start letter to eliminate things asap. Nothing
to eliminate when placing A and B, when placing C, check that CA>2, when placing D,
check that DA,DB,DC are all >2, etc.
<lang Phix>
constant txt = """
A B
/|\ /|\
/ | X | \
/ |/ \| \
C - D - E - F
\ |\ /| /
\ | X | /
\|/ \|/
G H"""
--constant links = "CA DA DB DC EA EB ED FB FE GC GD GE HD HE HF"
constant links = {"","","A","ABC","ABD","BE","CDE","DEF"}
 
function solve(sequence s, integer idx, sequence part)
object res
integer v, p
for i=1 to length(s) do
v = s[i]
for j=1 to length(links[idx]) do
p = links[idx][j]-'@'
if abs(v-part[p])<2 then v=0 exit end if
end for
if v then
if length(s)=1 then return part&v end if
res = solve(s[1..i-1]&s[i+1..$],idx+1,part&v)
if sequence(res) then return res end if
end if
end for
return 0
end function
 
printf(1,substitute_all(txt,"ABCDEFGH",solve("12345678",1,"")))</lang>
{{out}}
<pre>
3 4
/|\ /|\
/ | X | \
/ |/ \| \
7 - 1 - 8 - 2
\ |\ /| /
\ | X | /
\|/ \|/
5 6
</pre>
 
=={{header|Prolog}}==
7,820

edits