Anonymous user
Dinesman's multiple-dwelling problem: Difference between revisions
Dinesman's multiple-dwelling problem (view source)
Revision as of 03:43, 14 February 2012
, 12 years agoDon't-Repeat-Yourself D entry
(Shorter D entry) |
(Don't-Repeat-Yourself D entry) |
||
Line 128:
=={{header|D}}==
This code uses second lazy permutations function of '''[[Permutations#Lazy_version]]'''.
As for flexibility: the solve code works with an arbitrary number of people and of function predicates.▼
<lang d>import std.stdio, std.range, std.algorithm, std.math, std.exception;▼
▲As for flexibility: the solve code works with an arbitrary number of people and
import perms: permutations; // from rosettacode
void main() {
enum N { Baker, Cooper, Fletcher, Miller, Smith } // names▼
▲ enum N { Baker, Cooper, Fletcher, Miller, Smith }
alias /*immutable*/ bool function(in
P p1 = s => s[N.Baker] != s.length-1;
P p2 = s => s[N.Cooper] != 0;
Line 195 ⟶ 145:
P p6 = s => abs(cast(int)(s[N.Cooper] - s[N.Fletcher])) != 1;
immutable predicates = [p1, p2, p3, p4, p5, p6];
foreach (sol;
if (!canFind!(p => !p(sol))(predicates))
writeln
}</lang>
Output:
|