Skip to content

Instantly share code, notes, and snippets.

Created May 8, 2015 22:34
Show Gist options
  • Save anonymous/f7fd4013fe3c13e88279 to your computer and use it in GitHub Desktop.
Save anonymous/f7fd4013fe3c13e88279 to your computer and use it in GitHub Desktop.
data Op = Plus | Minus | Concat deriving Show
f l = concatMap (\r -> map (: r) l)
g = f [Plus, Minus, Concat]
allOps = iterate g [[Plus], [Minus], [Concat]] !! 7
step :: Int -> Op -> (Int, Int) -> (Int, Int)
step prev op (carry, sum) = case op of
Concat -> (read (show prev ++ show carry) :: Int, sum)
Plus -> (prev, carry + sum)
Minus -> (prev, sum - carry)
output = zipWith (,)
(map
(\ops -> uncurry (+) $
foldr (uncurry step) (9, 0) (zipWith (,) [1 ..] ops))
allOps)
allOps
answers = filter ((== 100) . fst) output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment