Skip to content

Instantly share code, notes, and snippets.

@notogawa
Created April 23, 2014 06:40
Show Gist options
  • Save notogawa/11204787 to your computer and use it in GitHub Desktop.
Save notogawa/11204787 to your computer and use it in GitHub Desktop.
ぶどうの房パズル
import Control.Monad
import Data.List
main :: IO ()
main = interact $ format . solve . read
format :: [[Int]] -> String
format = unlines . map (unwords . map show)
solve :: Int -> [[Int]]
solve = head . filter valid . candidates
valid :: [[Int]] -> Bool
valid xs = nub ys == ys where ys = concat xs
candidates :: Int -> [[[Int]]]
candidates n = map (take n . iterate diff) $ replicateM n [1 .. sum [1..n]]
diff :: [Int] -> [Int]
diff xs = map abs $ zipWith (-) xs $ tail xs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment