Skip to content

Instantly share code, notes, and snippets.

@tokiwoousaka
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tokiwoousaka/11398245 to your computer and use it in GitHub Desktop.
Save tokiwoousaka/11398245 to your computer and use it in GitHub Desktop.
ブドウの房問題
module Main where
import Control.Monad
import Data.List
step :: Int
step = 5
clusterCount :: Int -> Int
clusterCount n = n * (n + 1) `div` 2
--全ての組み合わせを列挙
allPattern :: Int -> [[Int]]
allPattern n = let
f m n = replicateM n [1..m]
in do
x <- f (clusterCount n) n
guard $ length (nub x) == n
return x
--2段目以降の房を計算
sloveSteps :: [Int] -> [[Int]]
sloveSteps xs = take (length xs) $ iterate nextStep xs
where
nextStep :: [Int] -> [Int]
nextStep ys = zipWith (\a b -> abs $ a - b) ys $ tail ys
--房の印字
printAns :: [[Int]] -> IO ()
printAns xs = do
forM_ xs $ \ys -> do
replicateM_ (length xs - length ys) $ putStr " "
mapM_ (putStr . fill) ys
putStrLn ""
where
fill :: Show a => a -> String
fill val = take 4 $ show val ++ " "
-----
main :: IO ()
main = mapM_ (\ans -> printAns ans >> putStrLn "") $ do
xs <- map sloveSteps $ allPattern step
guard $ length (nub $ concat xs) == clusterCount step
return xs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment