public
Created

Stones Riddle (Beginners Haskell), see http://beust.com/weblog/2011/10/30/a-new-coding-challenge/

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8
import Data.List
 
allStones = [[a,b,c,d]| a <- [1..39], b <- [a..39], c <- [b..39], d <- [c..39], a+b+c+d==40]
 
combineWeights [x] = [-x, 0, x]
combineWeights (x:xs) = concat $ map (\w -> [w-x, w, w+x]) (combineWeights xs)
 
result = filter (\x -> ((sort $ filter (>0) $ nub $ map abs (combineWeights x)) == [1..40])) allStones

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.