Created
April 23, 2014 06:40
-
-
Save notogawa/11204787 to your computer and use it in GitHub Desktop.
ぶどうの房パズル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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