How to resolve the algorithm Dinesman's multiple-dwelling problem step by step in the Picat programming language
How to resolve the algorithm Dinesman's multiple-dwelling problem step by step in the Picat programming language
Table of Contents
Problem Statement
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.
Baker, Cooper, Fletcher, Miller, and Smith live on different floors of an apartment house that contains only five floors.
Where does everyone live?
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Dinesman's multiple-dwelling problem step by step in the Picat programming language
Source code in the picat programming language
import util.
import cp.
dinesman_cp =>
println(dinesman_cp),
N = 5,
X = [Baker, Cooper, Fletcher, Miller, Smith],
X :: 1..N,
all_different(X),
% Baker does not live on the fifth floor.
Baker #!= 5,
% Cooper does not live on the first floor.
Cooper #!= 1,
% Fletcher does not live on either the fifth or the first floor.
Fletcher #!= 5,
Fletcher #!= 1,
% Miller lives on a higher floor than does Cooper.
Miller #> Cooper,
% Smith does not live on a floor adjacent to Fletcher'.
abs(Smith-Fletcher) #> 1,
% Fletcher does not live on a floor adjacent to Cooper's.
abs(Fletcher-Cooper) #> 1,
solve(X),
println([baker=Baker, cooper=Cooper, fletcher=Fletcher, miller=Miller, smith=Smith]).
%
% floors: 1: bottom .. 5: top floor
%
constraints([B,C,F,M,S]) =>
B != 5, % Baker not top floor
C != 1, % Cooper not bottom floor
F != 1, F != 5, % Fletcher not botton nor top floor
M > C, % Miller higher floor than Cooper
not adjacent(S, F), % Smith and Fletcher not adjacent
not adjacent(F, C). % Fletcher and Cooper not adjacent
adjacent(A,B) => abs(A-B) == 1.
dinesman2 =>
println(dinesman2),
foreach([B,C,F,M,S] in permutations(1..5), constraints([B,C,F,M,S]))
println([baker=B, cooper=C, fletcher=F, miller=M, smith=S])
end.
You may also check:How to resolve the algorithm One of n lines in a file step by step in the Swift programming language
You may also check:How to resolve the algorithm Graph colouring step by step in the C++ programming language
You may also check:How to resolve the algorithm Execute Brain step by step in the Limbo programming language
You may also check:How to resolve the algorithm Comma quibbling step by step in the PL/M programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the Forth programming language