This commit is contained in:
Ingy döt Net 2013-04-10 16:57:12 -07:00
parent 518da4a923
commit 764da6cbbb
6144 changed files with 83610 additions and 11 deletions

View file

@ -0,0 +1,37 @@
import Data.List (permutations)
import Control.Monad (guard)
dinesman :: [(Int,Int,Int,Int,Int)]
dinesman = do
-- baker, cooper, fletcher, miller, smith are integers representing
-- the floor that each person lives on, from 1 to 5
-- Baker, Cooper, Fletcher, Miller, and Smith live on different floors
-- of an apartment house that contains only five floors.
[baker, cooper, fletcher, miller, smith] <- permutations [1..5]
-- Baker does not live on the top floor.
guard $ baker /= 5
-- Cooper does not live on the bottom floor.
guard $ cooper /= 1
-- Fletcher does not live on either the top or the bottom floor.
guard $ fletcher /= 5 && fletcher /= 1
-- Miller lives on a higher floor than does Cooper.
guard $ miller > cooper
-- Smith does not live on a floor adjacent to Fletcher's.
guard $ abs (smith - fletcher) /= 1
-- Fletcher does not live on a floor adjacent to Cooper's.
guard $ abs (fletcher - cooper) /= 1
-- Where does everyone live?
return (baker, cooper, fletcher, miller, smith)
main :: IO ()
main = do
print $ head dinesman -- print first solution: (3,2,4,5,1)
print dinesman -- print all solutions (only one): [(3,2,4,5,1)]

View file

@ -0,0 +1,2 @@
import Data.List (permutations)
main = print [ (b,c,f,m,s) | [b,c,f,m,s] <- permutations [1..5], b/=5,c/=1,f/=1,f/=5,m>c,abs(s-f)>1,abs(c-f)>1]