Anonymous user
Dinesman's multiple-dwelling problem: Difference between revisions
Dinesman's multiple-dwelling problem (view source)
Revision as of 02:45, 14 February 2012
, 12 years agoUpdated D entry
(→{{header|D}}: fix overflow issue) |
(Updated D entry) |
||
Line 129:
=={{header|D}}==
As for flexibility: the solve function is parameterized, accepting a length argument and a variable number of function predicates.
<lang d>import std.stdio, std.exception, std.range, std.algorithm,
std.math, std.traits;
struct Permutations {
result *= i;▼
return result;▼
private uint[] seq;
private ulong tot;
this (in size_t
in {
} body {
this.tot = fact(num);▼
static ulong factorial(in uint n) pure nothrow {
this.num = num;▼
ulong result = 1;
foreach (i; 2 .. n + 1)
▲ result *= i;
▲ return result;
this.seq = array(iota(n)); // not pure
}
@property const(uint[]) front() const pure nothrow {
return seq;
}
@property bool empty() const pure nothrow {
return tot == 0;
}
void popFront() pure nothrow {
size_t j = num - 2;
while (seq[j] > seq[j + 1])
j--;
▲ }
size_t k = num - 1;
while (seq[j] > seq[k])
k--;
▲ }
swap(seq[k], seq[j]);
Line 181 ⟶ 183:
}
/*pure*/ nothrow {
outer:
foreach (p;
foreach (pred; predicates)
if (!pred(p))
Line 193 ⟶ 196:
void main() {
▲ enum {Baker, Cooper, Fletcher, Miller, Smith};
alias immutable(MutablePredicate) P;
P p5 = s => abs(cast(int)(s[N.Smith] - s[N.Fletcher])) != 1;
P p6 = s => abs(cast(int)(s[N.Cooper] - s[N.Fletcher])) != 1;
enum nFloors = EnumMembers!N.length;
if (auto sol = solve(
writeln(map!(p => [EnumMembers!N][p])(sol));
}</lang>
Output:
<pre>[
=={{header|Haskell}}==
|