9 "import Control.Monad (guard, mfilter)\n",
10 "import Control.Monad.Trans.State\n",
11 "import Data.List (foldl', delete)"
20 "select :: [a] -> [(a, [a])]\n",
22 "select (x:xs) = (x,xs) : [(y,x:ys) | (y,ys) <- select xs]"
31 "selectThis :: (Eq a) => a -> [a] -> [((), [a])]\n",
33 " | x `elem` xs = [((), delete x xs)] \n",
43 "asNumber :: [Int] -> Int\n",
44 "asNumber = foldl' (\\t o -> t*10 + o) 0"
54 "main = print . flip evalStateT [0..9] $ do\n",
55 " s <- StateT select\n",
56 " e <- StateT select\n",
57 " n <- StateT select\n",
58 " d <- StateT select\n",
59 " m <- StateT select\n",
60 " o <- StateT select\n",
61 " r <- StateT select\n",
62 " y <- StateT select\n",
63 " guard $ s /= 0 && m /= 0\n",
64 " let send = asNumber [s,e,n,d]\n",
65 " more = asNumber [m,o,r,e]\n",
66 " money = asNumber [m,o,n,e,y]\n",
67 " guard $ send + more == money\n",
68 " return (send, more, money)"
83 "output_type": "display_data"
105 "execution_count": 8,
110 "main1 = print . flip evalStateT [1..19] $ do\n",
111 " a <- StateT select\n",
112 " b <- StateT select\n",
113 " c <- StateT select\n",
115 " let rowSum = a + b + c\n",
117 " d <- StateT select\n",
118 " e <- StateT select\n",
119 " f <- StateT select\n",
120 " g <- StateT select\n",
121 " guard $ d + e + f + g == rowSum\n",
123 " h <- StateT select\n",
126 " i <- StateT select\n",
127 " j <- StateT select\n",
128 " k <- StateT select\n",
129 " l <- StateT select\n",
131 " guard $ h + i + j + k + l == rowSum\n",
132 " guard $ a + d + h == rowSum\n",
133 " guard $ c + g + l == rowSum\n",
135 " m <- StateT select\n",
136 " n <- StateT select\n",
137 " o <- StateT select\n",
138 " p <- StateT select\n",
139 " guard $ m + n + o + p == rowSum\n",
140 " guard $ b + e + i + m == rowSum\n",
141 " guard $ b + f + k + p == rowSum\n",
143 " q <- StateT select\n",
144 " r <- StateT select\n",
145 " s <- StateT select\n",
148 " guard $ c + f + j + n + q == rowSum\n",
149 " guard $ a + e + j + o + s == rowSum\n",
150 " guard $ g + k + o + r == rowSum\n",
151 " guard $ d + i + n + r == rowSum\n",
152 " guard $ h + m + q == rowSum\n",
153 " guard $ l + p + s == rowSum\n",
154 " guard $ q + r + s == rowSum\n",
156 " return [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s]"
161 "execution_count": 9,
166 "main2 = print . flip evalStateT [1..19] $ do\n",
167 " a <- StateT select\n",
168 " b <- StateT select\n",
169 " c <- StateT select\n",
171 " let rowSum = a + b + c\n",
173 " d <- StateT select\n",
174 " e <- StateT select\n",
175 " f <- StateT select\n",
176 " let g = rowSum - (d + e + f)\n",
177 " StateT (selectThis g)\n",
179 " let h = rowSum - (a + d)\n",
182 " StateT (selectThis h)\n",
183 " let l = rowSum - (c + g)\n",
185 " StateT (selectThis l)\n",
186 " i <- StateT select\n",
187 " j <- StateT select\n",
188 " k <- StateT select\n",
189 " guard $ h + i + j + k + l == rowSum\n",
191 " let m = rowSum - (b + e + i)\n",
192 " StateT (selectThis m)\n",
193 " n <- StateT select\n",
194 " o <- StateT select\n",
195 " let p = rowSum - (b + f + k)\n",
196 " guard $ m + n + o + p == rowSum\n",
197 " StateT (selectThis p)\n",
199 " let q = rowSum - (h + m)\n",
201 " guard $ c + f + j + n + q == rowSum\n",
202 " StateT (selectThis q)\n",
203 " let r = rowSum - (g + k + o)\n",
204 " guard $ d + i + n + r == rowSum\n",
205 " StateT (selectThis r)\n",
207 " let s = rowSum - (q + r)\n",
209 " guard $ a + e + j + o + s == rowSum\n",
210 " guard $ l + p + s == rowSum\n",
211 " StateT (selectThis s)\n",
213 " return [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s]"
218 "execution_count": 10,
227 "execution_count": 11,
233 "[[3,17,18,19,7,1,11,16,2,5,6,9,12,4,8,14,10,13,15]]"
237 "output_type": "display_data"
246 "execution_count": null,
254 "display_name": "Haskell",
255 "language": "haskell",
259 "codemirror_mode": "ihaskell",
260 "file_extension": ".hs",