150 "execution_count": 2,
152 "output_type": "execute_result"
156 "pi14 = [l.strip().split() for l in open('advent14.txt').readlines()]\n",
162 "execution_count": 6,
170 "[('Vixen', 8, 8, 53),\n",
171 " ('Blitzen', 13, 4, 49),\n",
172 " ('Rudolph', 20, 7, 132),\n",
173 " ('Cupid', 12, 4, 43),\n",
174 " ('Donner', 9, 5, 38),\n",
175 " ('Dasher', 10, 4, 37),\n",
176 " ('Comet', 3, 37, 76),\n",
177 " ('Prancer', 9, 12, 97),\n",
178 " ('Dancer', 37, 1, 36)]"
181 "execution_count": 6,
183 "output_type": "execute_result"
187 "speeds = [(l[0], int(l[3]), int(l[6]), int(l[13])) for l in pi14]\n",
193 "execution_count": 4,
204 "execution_count": 23,
216 "execution_count": 23,
218 "output_type": "execute_result"
222 "vixen = itertools.cycle(itertools.chain([speeds[0][1]] * speeds[0][1], [0] * speeds[0][3]))\n",
223 "v100 = itertools.islice(itertools.accumulate(vixen), 100)\n",
229 "execution_count": 13,
235 "def nth(iterable, n, default=None):\n",
236 " \"Returns the nth item or a default value\"\n",
237 " return next(itertools.islice(iterable, n, None), default)"
242 "execution_count": 15,
253 "execution_count": 15,
255 "output_type": "execute_result"
259 "vixen = itertools.accumulate(itertools.cycle(itertools.chain([speeds[0][1]] * speeds[0][1], [0] * speeds[0][3])))\n",
265 "execution_count": 17,
276 "execution_count": 17,
278 "output_type": "execute_result"
282 "comet = itertools.accumulate(itertools.cycle(itertools.chain([14] * 10, [0] * 127)))\n",
288 "execution_count": 18,
299 "execution_count": 18,
301 "output_type": "execute_result"
305 "dancer = itertools.accumulate(itertools.cycle(itertools.chain([16] * 11, [0] * 162)))\n",
311 "execution_count": 55,
317 "def reindeer_distance(spec):\n",
318 " return itertools.accumulate(itertools.cycle(itertools.chain([spec[1]] * spec[2], \n",
319 " [0] * spec[3])))\n",
321 "def reindeer_distance_at_time(spec, time):\n",
322 " return nth(reindeer_distance(spec), time-1)"
327 "execution_count": 57,
335 "[('Vixen', 2640),\n",
336 " ('Blitzen', 2496),\n",
337 " ('Rudolph', 2540),\n",
338 " ('Cupid', 2592),\n",
339 " ('Donner', 2655),\n",
340 " ('Dasher', 2460),\n",
341 " ('Comet', 2493),\n",
342 " ('Prancer', 2484),\n",
346 "execution_count": 57,
348 "output_type": "execute_result"
352 "[(r[0], reindeer_distance_at_time(r, 2503)) for r in speeds]"
357 "execution_count": 58,
368 "execution_count": 58,
370 "output_type": "execute_result"
374 "max(reindeer_distance_at_time(r, 2501) for r in speeds)"
379 "execution_count": 59,
387 "[('Vixen', 1.0491803278688525),\n",
388 " ('Blitzen', 0.9811320754716981),\n",
389 " ('Rudolph', 1.0071942446043165),\n",
390 " ('Cupid', 1.0212765957446808),\n",
391 " ('Donner', 1.0465116279069768),\n",
392 " ('Dasher', 0.975609756097561),\n",
393 " ('Comet', 0.9823008849557522),\n",
394 " ('Prancer', 0.9908256880733946),\n",
398 "execution_count": 59,
400 "output_type": "execute_result"
404 "[(s[0], (s[1] * s[2]) / (s[2] + s[3])) for s in speeds]"
409 "execution_count": 48,
415 "def rd2(spec, t):\n",
416 " return spec[1] * spec[2] * (t // (spec[2] + spec[3])) + spec[1] * min(spec[2], t % (spec[2] + spec[3]))"
421 "execution_count": 49,
429 "[('Vixen', 2640),\n",
430 " ('Blitzen', 2496),\n",
431 " ('Rudolph', 2540),\n",
432 " ('Cupid', 2592),\n",
433 " ('Donner', 2655),\n",
434 " ('Dasher', 2460),\n",
435 " ('Comet', 2493),\n",
436 " ('Prancer', 2484),\n",
440 "execution_count": 49,
442 "output_type": "execute_result"
446 "[(r[0], rd2(r, 2503)) for r in speeds]"
451 "execution_count": 73,
459 "[(8, 13, 20, 12, 9, 10, 3, 9, 37),\n",
460 " (16, 26, 40, 24, 18, 20, 6, 18, 37),\n",
461 " (24, 39, 60, 36, 27, 30, 9, 27, 37)]"
464 "execution_count": 73,
466 "output_type": "execute_result"
470 "list(zip(* (list(itertools.islice(reindeer_distance(r), 3)) for r in speeds)))"
475 "execution_count": 78,
481 "def score(positions):\n",
482 " return [1 if p == max(positions) else 0 for p in positions]"
487 "execution_count": 81,
495 "[[0, 0, 0, 0, 0, 0, 0, 0, 1],\n",
496 " [0, 0, 1, 0, 0, 0, 0, 0, 0],\n",
497 " [0, 0, 1, 0, 0, 0, 0, 0, 0]]"
500 "execution_count": 81,
502 "output_type": "execute_result"
506 "list(score(t) for t in zip(* (list(itertools.islice(reindeer_distance(r), 3)) for r in speeds)))"
511 "execution_count": 79,
519 "[0, 0, 0, 0, 0, 0, 0, 0, 1]"
522 "execution_count": 79,
524 "output_type": "execute_result"
528 "score((8, 13, 20, 12, 9, 10, 3, 9, 37))"
533 "execution_count": 85,
552 "execution_count": 85,
554 "output_type": "execute_result"
558 "list(zip(* list(score(t) \n",
559 " for t in zip(* (list(itertools.islice(reindeer_distance(r), 3)) \n",
560 " for r in speeds)))))"
565 "execution_count": 84,
573 "[0, 0, 2, 0, 0, 0, 0, 0, 1]"
576 "execution_count": 84,
578 "output_type": "execute_result"
583 " for ps in zip(* list(score(t) \n",
584 " for t in zip(* (list(itertools.islice(reindeer_distance(r), 3)) \n",
585 " for r in speeds)))))"
590 "execution_count": 86,
598 "[1059, 5, 887, 13, 414, 0, 22, 153, 1]"
601 "execution_count": 86,
603 "output_type": "execute_result"
608 " for ps in zip(* list(score(t) \n",
609 " for t in zip(* (list(itertools.islice(reindeer_distance(r), 2503)) \n",
610 " for r in speeds)))))"
615 "execution_count": 87,
626 "execution_count": 87,
628 "output_type": "execute_result"
632 "max(list(sum(ps) \n",
633 " for ps in zip(* list(score(t) \n",
634 " for t in zip(* (list(itertools.islice(reindeer_distance(r), 2503)) \n",
635 " for r in speeds))))))"
640 "execution_count": null,
650 "display_name": "Python 3",
651 "language": "python",
659 "file_extension": ".py",
660 "mimetype": "text/x-python",
662 "nbconvert_exporter": "python",
663 "pygments_lexer": "ipython3",