Dinesman's multiple-dwelling problem: Difference between revisions

Improved D version
m ({{omit from|GUISS}})
(Improved D version)
Line 98:
 
=={{header|D}}==
<lang d>import std.stdio, std.range, std.math, std.traits;
{{works with|D|2}}
<lang d>import std.stdio, std.range, std.math;
 
// From http://rosettacode.org/wiki/Permutations#D
autoT[][] permutations(size_tT)(T[] nitems) {
auto seq = array(iota(n));
size_tT[][] resresult;
 
void perms(size_tT[] s, size_tT[] pre prefix= []) {
if (s.length) {
foreach (i, c; s) {
perms(s[0 .. i] ~ s[i + 1 .. $], preprefix ~ c);
}else
} else res result ~= preprefix;
}
 
perms(seqitems);
return resresult;
}
 
size_tT[] solve(T, F...)(size_tT[] lenitems, TF funpredicates) {
foreach auto perms =(perm; permutations(lenitems);) {
foreach (ppred; permspredicates) {
outer:
if (!pred(perm))
foreach (p; perms) {
foreach (fnbreak; fun)
return nullperm;
if (!fn(cast(int[])p))
}
continue outer;
return p[];
}
return null;
}
 
void main() {
enum P { Baker, Cooper, Fletcher, Miller, Smith };
enum Floors = 5u;
 
enum {Baker, Cooper, Fletcher, Miller, Smith};
auto c6 c1= (intP[] s){ return abs(s[CooperP.Baker] - s[Fletcher]) != s.length-1; };
auto c1 c2= (intP[] s){ return s[BakerP.Cooper] != s.length-10; };
auto c2 c3= (intP[] s){ return s[CooperP.Fletcher] && s[P.Fletcher] != 0s.length-1; };
auto c3 c4= (intP[] s){ return s[FletcherP.Miller] != 0 &&> s[FletcherP.Cooper] != s.length-1; };
auto c4 c5= (intP[] s){ return abs(s[MillerP.Smith] >- s[CooperP.Fletcher]) != 1; };
auto c5 c6= (intP[] s){ return abs(s[SmithP.Cooper] - s[P.Fletcher]) != 1; };
 
auto c6 = (int[] s){ return abs(s[Cooper] - s[Fletcher]) != 1; };
// c1, c2, ... can't be an array
if (auto solsolution = solve(Floors[EnumMembers!P], c1, c2, c3, c4, c5, c6));
if (solution.length)
writeln(solsolution);
}</lang>
Output:
<pre>[2Baker, 1Cooper, 3Fletcher, 4Miller, 0Smith]</pre>
 
=={{header|Haskell}}==
Anonymous user