Taking advantage of a neat trick for using $ rather than a lambda
[advent-of-code-17.git] / src / Untitled.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 1,
6 "metadata": {},
7 "outputs": [],
8 "source": [
9 "import Control.Monad (guard, mfilter)\n",
10 "import Control.Monad.Trans.State\n",
11 "import Data.List (foldl', delete)"
12 ]
13 },
14 {
15 "cell_type": "code",
16 "execution_count": 2,
17 "metadata": {},
18 "outputs": [],
19 "source": [
20 "select :: [a] -> [(a, [a])]\n",
21 "select [] = []\n",
22 "select (x:xs) = (x,xs) : [(y,x:ys) | (y,ys) <- select xs]"
23 ]
24 },
25 {
26 "cell_type": "code",
27 "execution_count": 3,
28 "metadata": {},
29 "outputs": [],
30 "source": [
31 "selectThis :: (Eq a) => a -> [a] -> [((), [a])]\n",
32 "selectThis x xs \n",
33 " | x `elem` xs = [((), delete x xs)] \n",
34 " | otherwise = []"
35 ]
36 },
37 {
38 "cell_type": "code",
39 "execution_count": 4,
40 "metadata": {},
41 "outputs": [],
42 "source": [
43 "asNumber :: [Int] -> Int\n",
44 "asNumber = foldl' (\\t o -> t*10 + o) 0"
45 ]
46 },
47 {
48 "cell_type": "code",
49 "execution_count": 5,
50 "metadata": {},
51 "outputs": [],
52 "source": [
53 "main :: IO ()\n",
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)"
69 ]
70 },
71 {
72 "cell_type": "code",
73 "execution_count": 6,
74 "metadata": {},
75 "outputs": [
76 {
77 "data": {
78 "text/plain": [
79 "[(9567,1085,10652)]"
80 ]
81 },
82 "metadata": {},
83 "output_type": "display_data"
84 }
85 ],
86 "source": [
87 "main"
88 ]
89 },
90 {
91 "cell_type": "code",
92 "execution_count": 7,
93 "metadata": {},
94 "outputs": [],
95 "source": [
96 "-- a b c\n",
97 "-- d e f g\n",
98 "-- h i j k l\n",
99 "-- m n o p\n",
100 "-- q r s"
101 ]
102 },
103 {
104 "cell_type": "code",
105 "execution_count": 8,
106 "metadata": {},
107 "outputs": [],
108 "source": [
109 "main1 :: IO ()\n",
110 "main1 = print . flip evalStateT [1..19] $ do\n",
111 " a <- StateT select\n",
112 " b <- StateT select\n",
113 " c <- StateT select\n",
114 " guard $ c > a\n",
115 " let rowSum = a + b + c\n",
116 " \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",
122 " \n",
123 " h <- StateT select\n",
124 " guard $ h > a\n",
125 " guard $ h < c\n",
126 " i <- StateT select\n",
127 " j <- StateT select\n",
128 " k <- StateT select\n",
129 " l <- StateT select\n",
130 " guard $ l > a\n",
131 " guard $ h + i + j + k + l == rowSum\n",
132 " guard $ a + d + h == rowSum\n",
133 " guard $ c + g + l == rowSum\n",
134 " \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",
142 " \n",
143 " q <- StateT select\n",
144 " r <- StateT select\n",
145 " s <- StateT select\n",
146 " guard $ q > a\n",
147 " guard $ s > a\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",
155 " \n",
156 " return [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s]"
157 ]
158 },
159 {
160 "cell_type": "code",
161 "execution_count": 9,
162 "metadata": {},
163 "outputs": [],
164 "source": [
165 "main2 :: IO ()\n",
166 "main2 = print . flip evalStateT [1..19] $ do\n",
167 " a <- StateT select\n",
168 " b <- StateT select\n",
169 " c <- StateT select\n",
170 " guard $ c > a\n",
171 " let rowSum = a + b + c\n",
172 " \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",
178 " \n",
179 " let h = rowSum - (a + d)\n",
180 " guard $ h > a\n",
181 " guard $ h < c\n",
182 " StateT (selectThis h)\n",
183 " let l = rowSum - (c + g)\n",
184 " guard $ l > a\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",
190 "\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",
198 " \n",
199 " let q = rowSum - (h + m)\n",
200 " guard $ q > a\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",
206 "\n",
207 " let s = rowSum - (q + r)\n",
208 " guard $ s > a\n",
209 " guard $ a + e + j + o + s == rowSum\n",
210 " guard $ l + p + s == rowSum\n",
211 " StateT (selectThis s)\n",
212 " \n",
213 " return [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s]"
214 ]
215 },
216 {
217 "cell_type": "code",
218 "execution_count": 10,
219 "metadata": {},
220 "outputs": [],
221 "source": [
222 "-- main1"
223 ]
224 },
225 {
226 "cell_type": "code",
227 "execution_count": 11,
228 "metadata": {},
229 "outputs": [
230 {
231 "data": {
232 "text/plain": [
233 "[[3,17,18,19,7,1,11,16,2,5,6,9,12,4,8,14,10,13,15]]"
234 ]
235 },
236 "metadata": {},
237 "output_type": "display_data"
238 }
239 ],
240 "source": [
241 "main2"
242 ]
243 },
244 {
245 "cell_type": "code",
246 "execution_count": null,
247 "metadata": {},
248 "outputs": [],
249 "source": []
250 }
251 ],
252 "metadata": {
253 "kernelspec": {
254 "display_name": "Haskell",
255 "language": "haskell",
256 "name": "haskell"
257 },
258 "language_info": {
259 "codemirror_mode": "ihaskell",
260 "file_extension": ".hs",
261 "name": "haskell",
262 "version": "8.0.2"
263 }
264 },
265 "nbformat": 4,
266 "nbformat_minor": 2
267 }