Dinesman's multiple-dwelling problem: Difference between revisions

Content added Content deleted
Line 1,304:
 
=={{header|D}}==
 
 
{{incorrect|D| <br><br> The output is incorrect: <br><br>
it has Fletcher on the bottom floor, <br>
Baker on the top, <br>
and Cooper and Fletcher adjacent. <br><br>}}
 
 
This code uses the second lazy permutations function of '''[[Permutations#Lazy_version]]'''.
 
As for flexibility: the solve code works with an arbitrary number of people and predicates.
<syntaxhighlight lang="d">import std.stdio, std.math, std.algorithm, std.traits, permutations2;
import std.stdio, std.math, std.algorithm, std.traits, std.array, permutations2:permutations;
 
void main() {
 
enum Names { Baker, Cooper, Fletcher, Miller, Smith }
 
immutable(bool function(in Names[]) pure nothrow)[] predicates = [
s => s[.countUntil(Names.Baker]) != 4 && s.lengthcountUntil(Names.Cooper) - 1!= 0,
s => s.countUntil(Names.Fletcher) != 4 && s => s[.countUntil(Names.Cooper]Fletcher) != 0,
s => s[.countUntil(Names.Fletcher]Miller) != 0 &&> s[.countUntil(Names.Fletcher] != s.length-1Cooper),
s => abs(s[.countUntil(Names.Miller]Smith) >- s[.countUntil(Names.Cooper]Fletcher)) != 1,
s => abs(s[.countUntil(Names.Smith]Cooper) - s[.countUntil(Names.Fletcher])) != 1,
];
s => abs(s[Names.Cooper] - s[Names.Fletcher]) != 1];
 
permutations([EnumMembers!Names]).filter!(solution => predicates.all!(pred => pred(solution)))
.filter!(solution => predicates.all!(pred => pred(solution)))
.writeln;
}</syntaxhighlight>
{{out}}