Problem 68

http://projecteuler.net/index.php?section=problems&id=68
とりあえず一般化してmagic nを求められるようにした。
始めはsの範囲を間違えていて正しい解が出なかった。
ちなみに重複を除くのは面倒なのでやっていない。

import Data.List
import Data.Ord
import Data.Char
import Control.Monad
magic n = do 
  s <- [6..(6*n-3)]
  b0 <- [1..2*n]
  magic' s b0 b0 $[1..2*n]\\[b0]>>=return

magic' s b0 b [a] = [[[a,b,b0]]|s==a+b+b0]
magic' s b0 b xs = do
  a <- xs
  let c = s-a-b
  guard.elem c$xs\\[a]
  magic' s b0 c (xs\\[a,c])>>=return.([a,b,c]:)

normal xs = let m = minimumBy (comparing head) xs
                (ys,zs) = break (==m) xs
            in zs++ys
main =print.maximum.filter((==16).length). map (concatMap show.concat).nub.map normal .magic$5