Dinesman's multiple-dwelling problem: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|D}}: they're not functions of course, but delegates)
(Undo revision 111701 by Fwend (talk))
Line 117: Line 117:
}
}


size_t[] solve(T...)(size_t len, T dgs) {
size_t[] solve(T...)(size_t len, T fun) {
auto perms = permutations(len);
auto perms = permutations(len);
outer:
outer:
foreach (p; perms) {
foreach (p; perms) {
foreach (dg; dgs)
foreach (fn; fun)
if (!dg(cast(int[])p))
if (!fn(cast(int[])p))
continue outer;
continue outer;
return p;
return p;

Revision as of 18:16, 25 June 2011

Dinesman's multiple-dwelling problem is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

The task is to solve Dinesman's multiple dwelling problem but in a way that most naturally follows the problem statement given below. Solutions are allowed (but not required) to parse and interpret the problem text, but should remain flexible and should state what changes to the problem text are allowed. Flexibility and ease of expression are valued.

Examples may be be split into "setup", "problem statement", and "output" sections where the ease and naturalness of stating the problem and getting an answer, as well as the ease and flexibility of modifying the problem are the primary concerns.

Example output should be shown here, as well as any comments on the examples flexibility.

The problem
Baker, Cooper, Fletcher, Miller, and Smith live on different floors of an apartment house that contains only five floors. Baker does not live on the top floor. Cooper does not live on the bottom floor. Fletcher does not live on either the top or the bottom floor. Miller lives on a higher floor than does Cooper. Smith does not live on a floor adjacent to Fletcher's. Fletcher does not live on a floor adjacent to Cooper's. Where does everyone live?

C

<lang C>#include <stdio.h>

  1. include <stdlib.h>

int verbose = 0;

  1. define COND(a, b) int a(int *s) { return (b); }

typedef int(*condition)(int *);

/* BEGIN problem specific setup */

  1. define N_FLOORS 5
  2. define TOP (N_FLOORS - 1)

int solution[N_FLOORS] = { 0 }; int occupied[N_FLOORS] = { 0 };

enum tenants { baker = 0, cooper, fletcher, miller, smith, phantom_of_the_opera, };

char *names[] = { "baker", "cooper", "fletcher", "miller", "smith", };

COND(c0, s[baker] != TOP); COND(c1, s[cooper] != 0); COND(c2, s[fletcher] != 0 && s[fletcher] != TOP); COND(c3, s[miller] > s[cooper]); COND(c4, abs(s[smith] - s[fletcher]) != 1); COND(c5, abs(s[cooper] - s[fletcher]) != 1);

  1. define N_CONDITIONS 6

condition cond[] = { c0, c1, c2, c3, c4, c5 };

/* END of problem specific setup */


int solve(int person) { int i, j; if (person == phantom_of_the_opera) { /* check condition */ for (i = 0; i < N_CONDITIONS; i++) { if (cond[i](solution)) continue;

if (verbose) { for (j = 0; j < N_FLOORS; j++) printf("%d %s\n", solution[j], names[j]); printf("cond %d bad\n\n", i); } return 0; }

printf("Found arrangement:\n"); for (i = 0; i < N_FLOORS; i++) printf("%d %s\n", solution[i], names[i]); return 1; }

for (i = 0; i < N_FLOORS; i++) { if (occupied[i]) continue; solution[person] = i; occupied[i] = 1; if (solve(person + 1)) return 1; occupied[i] = 0; } return 0; }

int main() { verbose = 0; if (!solve(0)) printf("Nobody lives anywhere\n"); return 0; }</lang>Output<lang>Found arrangement: 2 baker 1 cooper 3 fletcher 4 miller 0 smith</lang>C, being its compiled self, is not terribly flexible in dynamically changing runtime code content. Parsing some external problem specification would be one way, but for a small problem, it might as well just recompile with conditions hard coded. For this program, to change conditions, one needs to edit content between BEGIN and END of problem specific setup. Those could even be setup in an external file and gets #included if need be.

D

Works with: D version 2

<lang d>import std.stdio, std.range, std.math;

auto permutations(size_t n) {

   auto seq = array(iota(n));
   size_t[][] res;

   void perms(size_t[] s, size_t[] pre = []) {
       if (s.length) {
           foreach (i, c; s) {
              perms(s[0 .. i] ~ s[i + 1 .. $], pre ~ c);
           }
       } else res ~= pre;
   }

   perms(seq);
   return res;

}

size_t[] solve(T...)(size_t len, T fun) {

   auto perms = permutations(len);
   outer:
   foreach (p; perms) {
       foreach (fn; fun)
           if (!fn(cast(int[])p)) 
               continue outer;
       return p;
   }
   return null;

}

void main() {

   enum Floors = 5u;
   enum {Baker, Cooper, Fletcher, Miller, Smith};
   
   auto c1 = (int[] s){ return s[Baker] != s.length-1; }; 
   auto c2 = (int[] s){ return s[Cooper] != 0; }; 
   auto c3 = (int[] s){ return s[Fletcher] != 0 && s[Fletcher] != s.length-1; }; 
   auto c4 = (int[] s){ return s[Miller] > s[Cooper]; }; 
   auto c5 = (int[] s){ return abs(s[Smith] - s[Fletcher]) != 1; };
   auto c6 = (int[] s){ return abs(s[Cooper] - s[Fletcher]) != 1; };
   
   if (auto sol = solve(Floors, c1, c2, c3, c4, c5, c6))
       writeln(sol);

}</lang>

[2, 1, 3, 4, 0]

PureBasic

<lang PureBasic>Prototype cond(Array t(1))

Enumeration #Null

 #Baker
 #Cooper
 #Fletcher
 #Miller
 #Smith 

EndEnumeration

Procedure checkTenands(Array tenants(1), Array Condions.cond(1))

 Protected i, j
 Protected.cond *f 
 j=ArraySize(Condions())
 For i=0 To j
   *f=Condions(i)              ; load the function pointer to the current condition
   If *f(tenants()) = #False
     ProcedureReturn  #False
   EndIf 
 Next
 ProcedureReturn #True

EndProcedure

Procedure C1(Array t(1))

 If Int(Abs(t(#Fletcher)-t(#Cooper)))<>1
   ProcedureReturn #True
 EndIf 

EndProcedure

Procedure C2(Array t(1))

 If t(#Baker)<>5
   ProcedureReturn #True
 EndIf 

EndProcedure

Procedure C3(Array t(1))

 If t(#Cooper)<>1
   ProcedureReturn #True
 EndIf 

EndProcedure

Procedure C4(Array t(1))

 If t(#Miller) >= t(#Cooper)
   ProcedureReturn #True
 EndIf 

EndProcedure

Procedure C5(Array t(1))

 If t(#Fletcher)<>1 And t(#Fletcher)<>5
   ProcedureReturn #True
 EndIf 

EndProcedure

Procedure C6(Array t(1))

 If Int(Abs(t(#Smith)-t(#Fletcher)))<>1
   ProcedureReturn #True
 EndIf 

EndProcedure

Procedure main()

 If OpenConsole()
   Dim People(4)
   Dim Conditions(5)
   Protected  a, b, c, d, e, i
   ;
   ;- Load pointers to all conditions
   Conditions(i)=@C1(): i+1
   Conditions(i)=@C2(): i+1
   Conditions(i)=@C3(): i+1
   Conditions(i)=@C4(): i+1
   Conditions(i)=@C5(): i+1
   Conditions(i)=@C6()
   ;
   ; generate and the all legal combinations
   For a=1 To 5
     For b=1 To 5
       If a=b: Continue: EndIf
       For c=1 To 5
         If a=c Or b=c: Continue: EndIf
         For d=1 To 5
           If d=a Or d=b Or d=c : Continue: EndIf
           For e=1 To 5 
             If e=a Or e=b Or e=c Or e=d: Continue: EndIf
             People(#Baker)=a
             People(#Cooper)=b
             People(#Fletcher)=c
             People(#Miller)=d
             People(#Smith)=e
             If checkTenands(People(), Conditions())
               PrintN("Solution found;")
               PrintN("Baker="+Str(a)+#CRLF$+"Cooper="+Str(b)+#CRLF$+"Miller="+Str(c))
               PrintN("Fletcher="+Str(d)+#CRLF$+"Smith="+Str(e)+#CRLF$) 
             EndIf
           Next
         Next
       Next
     Next
   Next
   Print("Press ENTER to exit"): Input()
 EndIf

EndProcedure

main()</lang>

Solution found;
Baker=3
Cooper=2
Miller=4
Fletcher=5
Smith=1