module Problem265 where

import Data.Array

n :: Int
n = 5

type Seq = [Int]
type State = (Seq, Array Int Bool)

main :: IO ()
main =
    print
        $ sum
        $ map (sum . (zipWith (*) (iterate (* 2) 1) . drop (n - 1)) . fst)
        $ (!! (2 ^ n - 1))
        $ iterate
            (concatMap go)
            [ ( [0, 0, 0]
              , array (0, 2 ^ n - 1) ((0, True) : [ (x, False) | x <- [1 .. 2 ^ n - 1] ])
              )
            ]

go :: State -> [State]
go (s, arr) =
    map (\x' -> (x', arr // [(getVal x', True)]))
        $ filter (\x' -> not $ arr ! getVal x')
        $ map (: s) [0, 1]

getVal :: Seq -> Int
getVal xs = sum $ zipWith (*) (take n xs) (iterate (* 2) 1)
