Dinesman's multiple-dwelling problem: Difference between revisions
Content added Content deleted
Line 1,304: | Line 1,304: | ||
=={{header|D}}== |
=={{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]]'''. |
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. |
As for flexibility: the solve code works with an arbitrary number of people and predicates. |
||
<syntaxhighlight lang="d"> |
<syntaxhighlight lang="d"> |
||
import std.stdio, std.math, std.algorithm, std.traits, std.array, permutations2:permutations; |
|||
void main() { |
void main() { |
||
enum Names { Baker, Cooper, Fletcher, Miller, Smith } |
enum Names { Baker, Cooper, Fletcher, Miller, Smith } |
||
immutable(bool function(in Names[]) pure nothrow)[] predicates = [ |
immutable(bool function(in Names[]) pure nothrow)[] predicates = [ |
||
s => s.countUntil(Names.Baker) != 4 && s.countUntil(Names.Cooper) != 0, |
|||
s => s.countUntil(Names.Fletcher) != 4 && s.countUntil(Names.Fletcher) != 0, |
|||
s => s.countUntil(Names.Miller) > s.countUntil(Names.Cooper), |
|||
s => abs(s.countUntil(Names.Smith) - s.countUntil(Names.Fletcher)) != 1, |
|||
s => abs(s.countUntil(Names.Cooper) - s.countUntil(Names.Fletcher)) != 1 |
|||
]; |
|||
s => abs(s[Names.Cooper] - s[Names.Fletcher]) != 1]; |
|||
permutations([EnumMembers!Names]) |
permutations([EnumMembers!Names]).filter!(solution => predicates.all!(pred => pred(solution))) |
||
.filter!(solution => predicates.all!(pred => pred(solution))) |
|||
.writeln; |
.writeln; |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |