RosettaCodeData/Task/Dinesmans-multiple-dwelling-problem/Phix/dinesmans-multiple-dwelling-problem-2.phix
2017-09-25 22:28:19 +02:00

44 lines
1.3 KiB
Text

sequence names = {"Baker","Cooper","Fletcher","Miller","Smith"},
rules = {{"!=","Baker",length(names)},
{"!=","Cooper",1},
{"!=","Fletcher",1},
{"!=","Fletcher",length(names)},
{">","Miller","Cooper"},
-- {"!=",{"abs","Smith","Fletcher"},1},
{"nadj","Smith","Fletcher"},
-- {"!=",{"abs","Fletcher","Cooper"},1},
{"nadj","Fletcher","Cooper"}}
function eval(sequence rule, sequence flats)
{string operand, object op1, object op2} = rule
if string(op1) then
op1 = flats[find(op1,names)]
-- elsif sequence(op1) then
-- op1 = eval(op1,flats)
end if
if string(op2) then
op2 = flats[find(op2,names)]
-- elsif sequence(op2) then
-- op2 = eval(op2,flats)
end if
switch operand do
case "!=": return op1!=op2
case ">": return op1>op2
-- case "abs": return abs(op1-op2)
case "nadj": return abs(op1-op2)!=1
end switch
return 9/0
end function
procedure test(sequence flats)
for i=1 to length(rules) do
if not eval(rules[i],flats) then return end if
end for
for i=1 to length(names) do
?{names[i],flats[i]}
end for
end procedure
for i=1 to factorial(length(names)) do
test(permute(i,tagset(length(names))))
end for