Created
September 15, 2015 15:46
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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