Life Goes On

まあまあだけど楽しんでる方です

68問目

http://projecteuler.net/index.php?section=problems&id=68
この問題を図なしで説明できる自信はないので、リンク先を見てください。
求める文字列が16桁なので、10は外側にあります。最大の文字列を求めたいので、6から9も外側。あとは並べ替えながら最大値を探します。

import Data.List

main = print euler068

euler068 :: String
euler068 = maximum [ concat $ map (concat . map show) $ solution |
    ext <- map (6 :) $ perm [7,8,9,10], int <- perm [1,2,3,4,5],
    let solution = zipWith (\ e i -> (head e):(take 2 i)) (init $ tails ext) (tails $ cycle int),
    magic solution ]

magic :: [[Int]] -> Bool
magic sol =  all ((== avg) . sum) sol
    where avg = div (sum $ map sum sol) (length sol)

perm :: Eq a => [a] -> [[a]]
perm [] = [[]]
perm digits = [ x : xs | x <- digits, xs <- perm $ delete x digits ]