Multiple Dwellings
February 20, 2009
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?
In an appartment house?
P!
Haskell (assuming 0 = bottom floor):
import Data.Map (fromList, (!))
import Data.List
data Person = Baker | Cooper | Fletcher | Miller | Smith deriving (Enum, Eq, Ord, Show)
main = print . filter conditions . map (fromList . flip zip [0 :: Int ..]) $ permutations [Baker ..]
conditions xs | xs ! Baker == 4 = False
| xs ! Cooper == 0 = False
| xs ! Fletcher == 0 = False
| xs ! Fletcher == 4 = False
| xs ! Miller < xs ! Cooper = False | abs (xs ! Smith - xs ! Fletcher) == 1 = False | abs (xs ! Cooper - xs ! Fletcher) == 1 = False | otherwise = True [/sourcecode]
[…] Multiple Dwellings 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? […]
After some false starts, I came up with Haskell solution that isn’t half bad. Here’s mine:
Clojure code using only a list comprehension for this particular problem.
nb: in my country floors start at zero
https://github.com/ftt/programming-praxis/blob/master/20090220-multiple-dwellings/multiple-dwellings.py
Great problem! I used to do these logic puzzles as a kid by marking off squares on a grid. Here’s my solution in Scala:
Answer:
1.Miller
2.Fletcher
3.Baker
4.Cooper
5.Smith
Here,1. represents Top floor and so on.