Dinesman's multiple-dwelling problem: Difference between revisions
Content added Content deleted
(Updated D entry) |
(Shorter D entry) |
||
Line 128: | Line 128: | ||
=={{header|D}}== |
=={{header|D}}== |
||
As for flexibility: the solve |
As for flexibility: the solve code works with an arbitrary number of people and of function predicates. |
||
<lang d>import std.stdio, std. |
<lang d>import std.stdio, std.range, std.algorithm, std.math, std.exception; |
||
⚫ | |||
struct |
struct LazyPermutations { |
||
private immutable size_t num; |
private immutable size_t num; |
||
private uint[] seq; |
private uint[] seq; |
||
Line 181: | Line 180: | ||
} |
} |
||
} |
} |
||
} |
|||
const(uint[]) solve(T : size_t, F...)(in T len, in F predicates) |
|||
/*pure*/ nothrow { |
|||
outer: |
|||
foreach (p; Permutations(len)) { |
|||
⚫ | |||
if (!pred(p)) |
|||
continue outer; |
|||
return p; |
|||
} |
|||
return null; |
|||
} |
} |
||
void main() { |
void main() { |
||
//import std.traits: EnumMembers; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
alias bool function(in uint[]) pure nothrow |
alias /*immutable*/ bool function(in uint[]) pure nothrow P; |
||
alias immutable(MutablePredicate) P; |
|||
P p1 = s => s[N.Baker] != s.length-1; |
P p1 = s => s[N.Baker] != s.length-1; |
||
P p2 = s => s[N.Cooper] != 0; |
P p2 = s => s[N.Cooper] != 0; |
||
Line 207: | Line 195: | ||
P p6 = s => abs(cast(int)(s[N.Cooper] - s[N.Fletcher])) != 1; |
P p6 = s => abs(cast(int)(s[N.Cooper] - s[N.Fletcher])) != 1; |
||
enum |
enum nNames = EnumMembers!N.length; |
||
immutable predicates = [p1, p2, p3, p4, p5, p6]; |
|||
⚫ | |||
foreach (sol; LazyPermutations(nNames)) |
|||
⚫ | |||
⚫ | |||
}</lang> |
}</lang> |
||
Output: |
Output: |