Skip to content

Instantly share code, notes, and snippets.

@paulmr
Created September 15, 2015 15:46
Show Gist options
  • Select an option

  • Save paulmr/3834d621d6d204b84f1b to your computer and use it in GitHub Desktop.

Select an option

Save paulmr/3834d621d6d204b84f1b to your computer and use it in GitHub Desktop.
After reading (this article)[http://blog.plover.com/prog/haskell/monad-search.html] about `do` notation in Haskell, I had a quick go at solving the problem with Scala list comprehensions.
val digits = (0 to 9).toList
def without[A](l: List[A], r: List[A]) =
l.filterNot(r.contains(_))
def makeNum(l: List[Int]): Int =
l.reduce(_ * 10 + _)
val res =
for {
s <- without(digits, 0 :: Nil)
e <- without(digits, s :: Nil)
n <- without(digits, s :: e :: Nil)
d <- without(digits, s :: e :: n :: Nil)
send = makeNum(s :: e :: n :: d :: Nil)
m <- without(digits, 0 :: s :: e :: n :: d :: Nil)
o <- without(digits, s :: e :: n :: d :: m :: Nil)
r <- without(digits, s :: e :: n :: d :: m :: o :: Nil)
more = makeNum(m :: o :: r :: e :: Nil)
y <- without(digits, s :: e :: n :: d :: m :: o :: r :: Nil)
money = makeNum(m :: o :: n :: e :: y :: Nil)
if send + more == money
} yield ((send, more, money))
println(res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment