Skip to content

Instantly share code, notes, and snippets.

@filippovitale
Created December 19, 2016 22:20
Show Gist options
  • Save filippovitale/3be9498257d6c98876672d5b46b38f92 to your computer and use it in GitHub Desktop.
Save filippovitale/3be9498257d6c98876672d5b46b38f92 to your computer and use it in GitHub Desktop.
SEND + MORE = MONEY
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Send More Money – StateT List"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"val scalazV = \"7.2.7\"\n",
"classpath.add(\"org.scalaz\" %% \"scalaz-core\" % scalazV)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import scalaz._, Scalaz._"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def select[A](xs: List[A]): List[(List[A], A)] = \n",
" xs.map(x => (xs.filterNot(_ == x), x))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"\u001b[36msel\u001b[0m: \u001b[32mStateT\u001b[0m[\u001b[32mList\u001b[0m, \u001b[32mList\u001b[0m[\u001b[32mInt\u001b[0m], \u001b[32mInt\u001b[0m] = scalaz.IndexedStateT$$anon$10@45a3a565\n",
"\u001b[36msolution\u001b[0m: \u001b[32mIndexedStateT\u001b[0m[\u001b[32mList\u001b[0m[\u001b[32mA\u001b[0m], \u001b[32mList\u001b[0m[\u001b[32mInt\u001b[0m], \u001b[32mList\u001b[0m[\u001b[32mInt\u001b[0m], (\u001b[32mInt\u001b[0m, \u001b[32mInt\u001b[0m, \u001b[32mInt\u001b[0m, \u001b[32mInt\u001b[0m, \u001b[32mInt\u001b[0m, \u001b[32mInt\u001b[0m, \u001b[32mInt\u001b[0m, \u001b[32mInt\u001b[0m)] = scalaz.IndexedStateT$$anon$11@2841cc2f"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"val sel = StateT(select[Int])\n",
"\n",
"val solution = for {\n",
" s <- sel if s != 0\n",
" e <- sel\n",
" n <- sel\n",
" d <- sel\n",
" m <- sel if m != 0\n",
" o <- sel\n",
" r <- sel\n",
" y <- sel\n",
" send = List(s, e, n, d).reduce(_ * 10 + _)\n",
" more = List(m, o, r, e).reduce(_ * 10 + _)\n",
" money = List(m, o, n, e, y).reduce(_ * 10 + _)\n",
" if send + more == money\n",
"} yield (s, e, n, d, m, o, r, y)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"\u001b[36mres6\u001b[0m: \u001b[32mList\u001b[0m[(\u001b[32mInt\u001b[0m, \u001b[32mInt\u001b[0m, \u001b[32mInt\u001b[0m, \u001b[32mInt\u001b[0m, \u001b[32mInt\u001b[0m, \u001b[32mInt\u001b[0m, \u001b[32mInt\u001b[0m, \u001b[32mInt\u001b[0m)] = \u001b[33mList\u001b[0m(\u001b[33m\u001b[0m(\u001b[32m9\u001b[0m, \u001b[32m5\u001b[0m, \u001b[32m6\u001b[0m, \u001b[32m7\u001b[0m, \u001b[32m1\u001b[0m, \u001b[32m0\u001b[0m, \u001b[32m8\u001b[0m, \u001b[32m2\u001b[0m))"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"solution.eval(0 |-> 9)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Scala 2.11",
"language": "scala211",
"name": "scala211"
},
"language_info": {
"codemirror_mode": "text/x-scala",
"file_extension": ".scala",
"mimetype": "text/x-scala",
"name": "scala211",
"pygments_lexer": "scala",
"version": "2.11.8"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment