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:
import Data.List import Data.Map (fromList, toList, (!)) people = ["Baker", "Cooper", "Fletcher", "Miller", "Smith"] assignments = [ fromList (zip people floors) | floors <- permutations [0,1,2,3,4] ] -- Return True if the given floor assignments obey the constraints constrain assign = let b = assign ! "Baker" c = assign ! "Cooper" f = assign ! "Fletcher" m = assign ! "Miller" s = assign ! "Smith" in b /= 4 -- Baker is not on top floor && c /= 0 -- Cooper is not on bottom floor && f `notElem` [0, 4] -- Fletcher is not on top or bottom floor && m > c -- Miller is above Cooper && s `notElem` [f-1, f+1] -- Smith is not adjacent to Fletcher && f `notElem` [c-1, c+1] -- Fletcher is not adjacent to Cooper -- Return all floor assignments that fit the constraints solutions = head $ filter constrain assignments main = putStrLn $ show $ toList solutions -- [("Baker",2),("Cooper",1),("Fletcher",3),("Miller",4),("Smith",0)]Clojure code using only a list comprehension for this particular problem.
nb: in my country floors start at zero
;; list comprehension with all constraints in the :when clause (for [b (range 5) c (range 5) f (range 5) m (range 5) s (range 5) :let [sf (- s f) fc (- f c)] :when (and (= 5 (count (distinct [b c f m s]))) (not= b 4) (not= c 0) (< 0 f 4) (> m c) (not= 1 (* sf sf)) (not= 1 (* fc fc)))] [:Baker b :Cooper c :Fletcher f :Miller m :Smith s]) ;; result: ([:Baker 2 :Cooper 1 :Fletcher 3 :Miller 4 :Smith 0])package main import "fmt" func pass(b, c, f, m, s int) bool { return b < 5 && c > 1 && f > 1 && f < 5 && m > c && f-s != 1 && f-s != -1 && f-c != 1 && f-c != -1 && b != c && b != f && b != m && b != s && c != f && c != m && c != s && f != m && f != s && m != s && b >= 1 && c >= 1 && f >= 1 && m >= 1 && s >= 1 && b <= 5 && c <= 5 && f <= 5 && m <= 5 && s <= 5 } func main() { for l := 0; l < 55555; l++ { if pass(l%10,l/10%10,l/100%10,l/1000%10,l/10000%10) { fmt.Printf("lives %d\n", l) } } }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:
def conditions(combo: Seq[String]) = { def floor(person: String) = combo.indexOf(person) def adjacent(person1: String, person2: String) = math.abs(floor(person1) - floor(person2)) == 1 floor("Baker") != 4 && floor("Cooper") != 0 && floor("Fletcher") != 0 && floor("Fletcher") != 4 && floor("Miller") > floor("Cooper") && !adjacent("Smith", "Fletcher") && !adjacent("Cooper", "Fletcher") } val people = Seq("Baker", "Cooper", "Fletcher", "Miller", "Smith") val result = people.permutations filter conditions result foreach printlnAnswer:
1.Miller
2.Fletcher
3.Baker
4.Cooper
5.Smith
Here,1. represents Top floor and so on.