cd6ab100cae55f85e6129323d40044805f878014
[advent-of-code-17.git] / adventofcode1701 / app / advent01.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 1,
6 "metadata": {},
7 "outputs": [],
8 "source": [
9 "{-# LANGUAGE NegativeLiterals #-}\n",
10 "{-# LANGUAGE FlexibleContexts #-}"
11 ]
12 },
13 {
14 "cell_type": "code",
15 "execution_count": 2,
16 "metadata": {},
17 "outputs": [],
18 "source": [
19 "import Data.List (tails)"
20 ]
21 },
22 {
23 "cell_type": "code",
24 "execution_count": 4,
25 "metadata": {},
26 "outputs": [],
27 "source": [
28 "digits = \"1122\""
29 ]
30 },
31 {
32 "cell_type": "code",
33 "execution_count": null,
34 "metadata": {},
35 "outputs": [],
36 "source": []
37 },
38 {
39 "cell_type": "code",
40 "execution_count": 50,
41 "metadata": {},
42 "outputs": [],
43 "source": [
44 "sum_valid_pairs :: [String] -> Integer\n",
45 "sum_valid_pairs possibles = sum $ map (read . take 1) \n",
46 " $ filter (\\(x:y:_) -> x == y) \n",
47 " $ filter (\\p -> length p == 2) possibles"
48 ]
49 },
50 {
51 "cell_type": "code",
52 "execution_count": 80,
53 "metadata": {},
54 "outputs": [],
55 "source": [
56 "part1_extract :: String -> [String] \n",
57 "part1_extract digits = map (take 2) $ tails (digits ++ [head digits])"
58 ]
59 },
60 {
61 "cell_type": "code",
62 "execution_count": 86,
63 "metadata": {},
64 "outputs": [],
65 "source": [
66 "part1 :: String -> Integer \n",
67 "part1 = sum_valid_pairs . part1_extract"
68 ]
69 },
70 {
71 "cell_type": "code",
72 "execution_count": 87,
73 "metadata": {},
74 "outputs": [
75 {
76 "data": {
77 "text/plain": [
78 "3"
79 ]
80 },
81 "metadata": {},
82 "output_type": "display_data"
83 }
84 ],
85 "source": [
86 "part1 \"1122\""
87 ]
88 },
89 {
90 "cell_type": "code",
91 "execution_count": 88,
92 "metadata": {},
93 "outputs": [
94 {
95 "data": {
96 "text/plain": [
97 "4"
98 ]
99 },
100 "metadata": {},
101 "output_type": "display_data"
102 }
103 ],
104 "source": [
105 "part1 \"1111\""
106 ]
107 },
108 {
109 "cell_type": "code",
110 "execution_count": 89,
111 "metadata": {},
112 "outputs": [
113 {
114 "data": {
115 "text/plain": [
116 "0"
117 ]
118 },
119 "metadata": {},
120 "output_type": "display_data"
121 }
122 ],
123 "source": [
124 "part1 \"1234\""
125 ]
126 },
127 {
128 "cell_type": "code",
129 "execution_count": 90,
130 "metadata": {},
131 "outputs": [
132 {
133 "data": {
134 "text/plain": [
135 "9"
136 ]
137 },
138 "metadata": {},
139 "output_type": "display_data"
140 }
141 ],
142 "source": [
143 "part1 \"91212129\""
144 ]
145 },
146 {
147 "cell_type": "code",
148 "execution_count": 91,
149 "metadata": {},
150 "outputs": [],
151 "source": [
152 "part2 :: String -> Integer \n",
153 "part2 = sum_valid_pairs . part2_extract"
154 ]
155 },
156 {
157 "cell_type": "code",
158 "execution_count": 92,
159 "metadata": {},
160 "outputs": [],
161 "source": [
162 "part2_extract :: String -> [String]\n",
163 "part2_extract digits = map (\\ds -> (take 1 ds) ++ (take 1 $ drop offset ds)) \n",
164 " $ take (length digits) \n",
165 " $ tails (digits ++ digits)\n",
166 " where offset = length digits `div` 2"
167 ]
168 },
169 {
170 "cell_type": "code",
171 "execution_count": 93,
172 "metadata": {},
173 "outputs": [
174 {
175 "data": {
176 "text/plain": [
177 "[\"12\",\"12\",\"21\",\"21\"]"
178 ]
179 },
180 "metadata": {},
181 "output_type": "display_data"
182 }
183 ],
184 "source": [
185 "part2_extract \"1122\""
186 ]
187 },
188 {
189 "cell_type": "code",
190 "execution_count": 94,
191 "metadata": {},
192 "outputs": [
193 {
194 "data": {
195 "text/plain": [
196 "0"
197 ]
198 },
199 "metadata": {},
200 "output_type": "display_data"
201 }
202 ],
203 "source": [
204 "part2 \"1122\""
205 ]
206 },
207 {
208 "cell_type": "code",
209 "execution_count": 95,
210 "metadata": {},
211 "outputs": [
212 {
213 "data": {
214 "text/plain": [
215 "[\"11\",\"22\",\"11\",\"22\"]"
216 ]
217 },
218 "metadata": {},
219 "output_type": "display_data"
220 }
221 ],
222 "source": [
223 "part2_extract \"1212\""
224 ]
225 },
226 {
227 "cell_type": "code",
228 "execution_count": 96,
229 "metadata": {},
230 "outputs": [
231 {
232 "data": {
233 "text/plain": [
234 "6"
235 ]
236 },
237 "metadata": {},
238 "output_type": "display_data"
239 }
240 ],
241 "source": [
242 "part2 \"1212\""
243 ]
244 },
245 {
246 "cell_type": "code",
247 "execution_count": 97,
248 "metadata": {},
249 "outputs": [
250 {
251 "data": {
252 "text/plain": [
253 "[\"12\",\"21\",\"21\",\"12\"]"
254 ]
255 },
256 "metadata": {},
257 "output_type": "display_data"
258 }
259 ],
260 "source": [
261 "part2_extract \"1221\""
262 ]
263 },
264 {
265 "cell_type": "code",
266 "execution_count": 98,
267 "metadata": {},
268 "outputs": [
269 {
270 "data": {
271 "text/plain": [
272 "0"
273 ]
274 },
275 "metadata": {},
276 "output_type": "display_data"
277 }
278 ],
279 "source": [
280 "part2 \"1221\""
281 ]
282 },
283 {
284 "cell_type": "code",
285 "execution_count": 99,
286 "metadata": {},
287 "outputs": [
288 {
289 "data": {
290 "text/plain": [
291 "[\"14\",\"22\",\"35\",\"41\",\"22\",\"53\"]"
292 ]
293 },
294 "metadata": {},
295 "output_type": "display_data"
296 }
297 ],
298 "source": [
299 "part2_extract \"123425\""
300 ]
301 },
302 {
303 "cell_type": "code",
304 "execution_count": 100,
305 "metadata": {},
306 "outputs": [
307 {
308 "data": {
309 "text/plain": [
310 "4"
311 ]
312 },
313 "metadata": {},
314 "output_type": "display_data"
315 }
316 ],
317 "source": [
318 "part2 \"123425\""
319 ]
320 },
321 {
322 "cell_type": "code",
323 "execution_count": 70,
324 "metadata": {},
325 "outputs": [
326 {
327 "data": {
328 "text/plain": [
329 "[\"11\",\"22\",\"33\",\"11\",\"22\",\"33\"]"
330 ]
331 },
332 "metadata": {},
333 "output_type": "display_data"
334 }
335 ],
336 "source": [
337 "part2_extract \"123123\""
338 ]
339 },
340 {
341 "cell_type": "code",
342 "execution_count": 77,
343 "metadata": {},
344 "outputs": [
345 {
346 "data": {
347 "text/plain": [
348 "12"
349 ]
350 },
351 "metadata": {},
352 "output_type": "display_data"
353 }
354 ],
355 "source": [
356 "part2 \"123123\""
357 ]
358 },
359 {
360 "cell_type": "code",
361 "execution_count": 71,
362 "metadata": {},
363 "outputs": [
364 {
365 "data": {
366 "text/plain": [
367 "[\"11\",\"24\",\"11\",\"35\",\"11\",\"42\",\"11\",\"53\"]"
368 ]
369 },
370 "metadata": {},
371 "output_type": "display_data"
372 }
373 ],
374 "source": [
375 "part2_extract \"12131415\""
376 ]
377 },
378 {
379 "cell_type": "code",
380 "execution_count": 78,
381 "metadata": {},
382 "outputs": [
383 {
384 "data": {
385 "text/plain": [
386 "4"
387 ]
388 },
389 "metadata": {},
390 "output_type": "display_data"
391 }
392 ],
393 "source": [
394 "part2 \"12131415\""
395 ]
396 },
397 {
398 "cell_type": "code",
399 "execution_count": 101,
400 "metadata": {},
401 "outputs": [],
402 "source": [
403 "main :: IO ()\n",
404 "main = do \n",
405 " digits <- readFile \"../../data/advent01.txt\"\n",
406 " print $ part1 digits\n",
407 " print $ part2 digits"
408 ]
409 },
410 {
411 "cell_type": "code",
412 "execution_count": 102,
413 "metadata": {},
414 "outputs": [
415 {
416 "data": {
417 "text/plain": [
418 "1158\n",
419 "1132"
420 ]
421 },
422 "metadata": {},
423 "output_type": "display_data"
424 }
425 ],
426 "source": [
427 "main"
428 ]
429 },
430 {
431 "cell_type": "code",
432 "execution_count": null,
433 "metadata": {},
434 "outputs": [],
435 "source": []
436 }
437 ],
438 "metadata": {
439 "kernelspec": {
440 "display_name": "Haskell",
441 "language": "haskell",
442 "name": "haskell"
443 },
444 "language_info": {
445 "codemirror_mode": "ihaskell",
446 "file_extension": ".hs",
447 "name": "haskell",
448 "version": "8.0.2"
449 }
450 },
451 "nbformat": 4,
452 "nbformat_minor": 2
453 }