From a787587eaa81d39848f839f98878a14ca9034499 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Mon, 14 Dec 2015 10:10:52 +0000 Subject: [PATCH] Day 14 --- SIGNED.md | 28 ++- advent14.ipynb | 669 +++++++++++++++++++++++++++++++++++++++++++++++++ advent14.txt | 9 + 3 files changed, 693 insertions(+), 13 deletions(-) create mode 100644 advent14.ipynb create mode 100644 advent14.txt diff --git a/SIGNED.md b/SIGNED.md index 8164edc..f17a89c 100644 --- a/SIGNED.md +++ b/SIGNED.md @@ -3,19 +3,19 @@ -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 -iQIcBAABCAAGBQJWbUbDAAoJEJPB2e07Pgbqq3MP/iuE0wAquzTghllO0Bh8Zk33 -y2/JLPlQ/WHV082bsP4tiMj/roMA/P6+D8oCf9AW95fdioPq7C9HMpu3l3qXY/7Y -XTIGN/AXkJX9RAiE1vdne7uinC1jx50OlyvLDiPv3dBgjZp1OfEY5iNzZ8XlYFOb -RcmpYr+0OX06oGdB7ydBQKhiSeWsASwmlL8Lhb6V4YikaUnD7ig4UkKD52srOMTH -2v/RJs/RVNtA/fSiCoXt65Lkfo/dYAkPJF6WMYeJi+s8P/y+Mp37FcJh7NyvPboM -AZ5ravIlS6SMihXBKjpgYUf/w5oTiL9EpTwk8RPsNHylY3QxpUfdd8I4IVqsm86T -FDrv/aL2CyzxnnGLcg73GiX3TI9+A+lg5gupjgkKqQk9i2JK6I9Jrv1+01BwijN7 -NOQ0dpb4UefJ0JS/2iHrPhVhGNsgNAaVuztyrAsFN3cZ+/qDjtsenWk7NLbn7/LL -pTgnmqeV1VZ8KEbAW51kM6JNnxQIYU4GxoPDj7nLLmd34qLW/AwvfIBrV9TCn54r -vwfVmfFfSVPCxbko1bQWAXJBMffkMGplRKLjVqJXK45VAcv2jzLXSJl/6afV1D31 -1x+2sYETRbhANJn2RYsePZWH18j5mASHweJkRakVkeMfV7VBgwcFvIX8PqsEbGnu -vQbRn1r9Z/UEdZzjJ+XC -=k7v/ +iQIcBAABCAAGBQJWbpWyAAoJEJPB2e07PgbqSqYP/2jlLEjZHz3y6q+inpV1kLHX +sM0B2SkvvV6KrQPL8s1jac0q0EQqgaQvy/M/dr8vPwfnw7ZCwEE3U4Jxpive7P8q +0QUL3Pg9fPqvwBBc81CT6xZIR+foKSEmZ4Di+w6//1gZvseb0X6PfGmwCB9lnLaW +AdGJEIa9pbTExiQzvuZamfnC4VFuYlCU82VsJIclG6UP3QdQ4sdh6JBC4jz+rHKx +CjmABv8N5G9Lib1kYSNvNYw+Ofbwv9CnA0YjlhTWz+ThXa6g9FA5LkF2Q9L96sA8 +YpA3AR9S7PQZutmpByB8nScRtdAr+r6G4aAML7a/VKT56PG+pEu8wOvVpfWEUtqG +TSb7IzLtd6TClT8HV2C+8x7ZDU0JbzzNVs+A8hepwGtP/xXuXjpuD25yzYHN5AGL +B25FxPKi1FD9CKOBnNb3NIoyD0QsLwn67ceXNRKJf6wt+cGLfSH0VZzU6hkvqihE +C+O9PeZzSEKZe7DYVqvZUHJQMHnlwhw36CCd3h6LzJALhtUmYqAe8TDcW1Dergl4 +Jg7oL7OEOpDZej+x5bqUm/n1EEDeO3UjUZenEohETLFTRp2c/KbrMsar3b/E2QU7 +dfOeMSx1ssINg2xDVX2J6fmSS5Z0UVXf+hQR/guvTnc3eTckS4oxcoIhO3h8exdV +HRxc6U+uv/9jnSRBOqJp +=KJpf -----END PGP SIGNATURE----- ``` @@ -68,6 +68,8 @@ size exec file contents 42142 advent12.json f7b94a7f5aa11f94edea6a1727fcf81613306c53a9a84078ff557d216cfbb6c9 10185 advent13.ipynb 30e1bfd714003c6ee5a4e23f8e1d5874658be3fae51b1e709451d1d55086469e 3465 advent13.txt a8610959b8c1388f53a7720828c8a1cad08b1c95661d198e01157fc83ef08605 +14202 advent14.ipynb faf4ce12b0317e0c9218869f980861d4fd902c5bdb2072a0a2d28aadb9506919 +656 advent14.txt 686da937ed08c3039b8782c7fd3e4b2cf45fcf1cfeada8bca4007e5020d3bd55 ``` #### Ignore diff --git a/advent14.ipynb b/advent14.ipynb new file mode 100644 index 0000000..9d34015 --- /dev/null +++ b/advent14.ipynb @@ -0,0 +1,669 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[['Vixen',\n", + " 'can',\n", + " 'fly',\n", + " '8',\n", + " 'km/s',\n", + " 'for',\n", + " '8',\n", + " 'seconds,',\n", + " 'but',\n", + " 'then',\n", + " 'must',\n", + " 'rest',\n", + " 'for',\n", + " '53',\n", + " 'seconds.'],\n", + " ['Blitzen',\n", + " 'can',\n", + " 'fly',\n", + " '13',\n", + " 'km/s',\n", + " 'for',\n", + " '4',\n", + " 'seconds,',\n", + " 'but',\n", + " 'then',\n", + " 'must',\n", + " 'rest',\n", + " 'for',\n", + " '49',\n", + " 'seconds.'],\n", + " ['Rudolph',\n", + " 'can',\n", + " 'fly',\n", + " '20',\n", + " 'km/s',\n", + " 'for',\n", + " '7',\n", + " 'seconds,',\n", + " 'but',\n", + " 'then',\n", + " 'must',\n", + " 'rest',\n", + " 'for',\n", + " '132',\n", + " 'seconds.'],\n", + " ['Cupid',\n", + " 'can',\n", + " 'fly',\n", + " '12',\n", + " 'km/s',\n", + " 'for',\n", + " '4',\n", + " 'seconds,',\n", + " 'but',\n", + " 'then',\n", + " 'must',\n", + " 'rest',\n", + " 'for',\n", + " '43',\n", + " 'seconds.'],\n", + " ['Donner',\n", + " 'can',\n", + " 'fly',\n", + " '9',\n", + " 'km/s',\n", + " 'for',\n", + " '5',\n", + " 'seconds,',\n", + " 'but',\n", + " 'then',\n", + " 'must',\n", + " 'rest',\n", + " 'for',\n", + " '38',\n", + " 'seconds.'],\n", + " ['Dasher',\n", + " 'can',\n", + " 'fly',\n", + " '10',\n", + " 'km/s',\n", + " 'for',\n", + " '4',\n", + " 'seconds,',\n", + " 'but',\n", + " 'then',\n", + " 'must',\n", + " 'rest',\n", + " 'for',\n", + " '37',\n", + " 'seconds.'],\n", + " ['Comet',\n", + " 'can',\n", + " 'fly',\n", + " '3',\n", + " 'km/s',\n", + " 'for',\n", + " '37',\n", + " 'seconds,',\n", + " 'but',\n", + " 'then',\n", + " 'must',\n", + " 'rest',\n", + " 'for',\n", + " '76',\n", + " 'seconds.'],\n", + " ['Prancer',\n", + " 'can',\n", + " 'fly',\n", + " '9',\n", + " 'km/s',\n", + " 'for',\n", + " '12',\n", + " 'seconds,',\n", + " 'but',\n", + " 'then',\n", + " 'must',\n", + " 'rest',\n", + " 'for',\n", + " '97',\n", + " 'seconds.'],\n", + " ['Dancer',\n", + " 'can',\n", + " 'fly',\n", + " '37',\n", + " 'km/s',\n", + " 'for',\n", + " '1',\n", + " 'seconds,',\n", + " 'but',\n", + " 'then',\n", + " 'must',\n", + " 'rest',\n", + " 'for',\n", + " '36',\n", + " 'seconds.']]" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pi14 = [l.strip().split() for l in open('advent14.txt').readlines()]\n", + "pi14" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[('Vixen', 8, 8, 53),\n", + " ('Blitzen', 13, 4, 49),\n", + " ('Rudolph', 20, 7, 132),\n", + " ('Cupid', 12, 4, 43),\n", + " ('Donner', 9, 5, 38),\n", + " ('Dasher', 10, 4, 37),\n", + " ('Comet', 3, 37, 76),\n", + " ('Prancer', 9, 12, 97),\n", + " ('Dancer', 37, 1, 36)]" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "speeds = [(l[0], int(l[3]), int(l[6]), int(l[13])) for l in pi14]\n", + "speeds" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "import itertools" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "128" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "vixen = itertools.cycle(itertools.chain([speeds[0][1]] * speeds[0][1], [0] * speeds[0][3]))\n", + "v100 = itertools.islice(itertools.accumulate(vixen), 100)\n", + "list(v100)[-1]" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def nth(iterable, n, default=None):\n", + " \"Returns the nth item or a default value\"\n", + " return next(itertools.islice(iterable, n, None), default)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "128" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "vixen = itertools.accumulate(itertools.cycle(itertools.chain([speeds[0][1]] * speeds[0][1], [0] * speeds[0][3])))\n", + "nth(vixen, 100)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "1120" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "comet = itertools.accumulate(itertools.cycle(itertools.chain([14] * 10, [0] * 127)))\n", + "nth(comet, 1000)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "1056" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dancer = itertools.accumulate(itertools.cycle(itertools.chain([16] * 11, [0] * 162)))\n", + "nth(dancer, 1000)" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def reindeer_distance(spec):\n", + " return itertools.accumulate(itertools.cycle(itertools.chain([spec[1]] * spec[2], \n", + " [0] * spec[3])))\n", + "\n", + "def reindeer_distance_at_time(spec, time):\n", + " return nth(reindeer_distance(spec), time-1)" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[('Vixen', 2640),\n", + " ('Blitzen', 2496),\n", + " ('Rudolph', 2540),\n", + " ('Cupid', 2592),\n", + " ('Donner', 2655),\n", + " ('Dasher', 2460),\n", + " ('Comet', 2493),\n", + " ('Prancer', 2484),\n", + " ('Dancer', 2516)]" + ] + }, + "execution_count": 57, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[(r[0], reindeer_distance_at_time(r, 2503)) for r in speeds]" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "2655" + ] + }, + "execution_count": 58, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "max(reindeer_distance_at_time(r, 2501) for r in speeds)" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[('Vixen', 1.0491803278688525),\n", + " ('Blitzen', 0.9811320754716981),\n", + " ('Rudolph', 1.0071942446043165),\n", + " ('Cupid', 1.0212765957446808),\n", + " ('Donner', 1.0465116279069768),\n", + " ('Dasher', 0.975609756097561),\n", + " ('Comet', 0.9823008849557522),\n", + " ('Prancer', 0.9908256880733946),\n", + " ('Dancer', 1.0)]" + ] + }, + "execution_count": 59, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[(s[0], (s[1] * s[2]) / (s[2] + s[3])) for s in speeds]" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def rd2(spec, t):\n", + " return spec[1] * spec[2] * (t // (spec[2] + spec[3])) + spec[1] * min(spec[2], t % (spec[2] + spec[3]))" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[('Vixen', 2640),\n", + " ('Blitzen', 2496),\n", + " ('Rudolph', 2540),\n", + " ('Cupid', 2592),\n", + " ('Donner', 2655),\n", + " ('Dasher', 2460),\n", + " ('Comet', 2493),\n", + " ('Prancer', 2484),\n", + " ('Dancer', 2516)]" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[(r[0], rd2(r, 2503)) for r in speeds]" + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[(8, 13, 20, 12, 9, 10, 3, 9, 37),\n", + " (16, 26, 40, 24, 18, 20, 6, 18, 37),\n", + " (24, 39, 60, 36, 27, 30, 9, 27, 37)]" + ] + }, + "execution_count": 73, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(zip(* (list(itertools.islice(reindeer_distance(r), 3)) for r in speeds)))" + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def score(positions):\n", + " return [1 if p == max(positions) else 0 for p in positions]" + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[[0, 0, 0, 0, 0, 0, 0, 0, 1],\n", + " [0, 0, 1, 0, 0, 0, 0, 0, 0],\n", + " [0, 0, 1, 0, 0, 0, 0, 0, 0]]" + ] + }, + "execution_count": 81, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(score(t) for t in zip(* (list(itertools.islice(reindeer_distance(r), 3)) for r in speeds)))" + ] + }, + { + "cell_type": "code", + "execution_count": 79, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 0, 0, 0, 0, 0, 0, 0, 1]" + ] + }, + "execution_count": 79, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "score((8, 13, 20, 12, 9, 10, 3, 9, 37))" + ] + }, + { + "cell_type": "code", + "execution_count": 85, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[(0, 0, 0),\n", + " (0, 0, 0),\n", + " (0, 1, 1),\n", + " (0, 0, 0),\n", + " (0, 0, 0),\n", + " (0, 0, 0),\n", + " (0, 0, 0),\n", + " (0, 0, 0),\n", + " (1, 0, 0)]" + ] + }, + "execution_count": 85, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(zip(* list(score(t) \n", + " for t in zip(* (list(itertools.islice(reindeer_distance(r), 3)) \n", + " for r in speeds)))))" + ] + }, + { + "cell_type": "code", + "execution_count": 84, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 0, 2, 0, 0, 0, 0, 0, 1]" + ] + }, + "execution_count": 84, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(sum(ps) \n", + " for ps in zip(* list(score(t) \n", + " for t in zip(* (list(itertools.islice(reindeer_distance(r), 3)) \n", + " for r in speeds)))))" + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[1059, 5, 887, 13, 414, 0, 22, 153, 1]" + ] + }, + "execution_count": 86, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(sum(ps) \n", + " for ps in zip(* list(score(t) \n", + " for t in zip(* (list(itertools.islice(reindeer_distance(r), 2503)) \n", + " for r in speeds)))))" + ] + }, + { + "cell_type": "code", + "execution_count": 87, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "1059" + ] + }, + "execution_count": 87, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "max(list(sum(ps) \n", + " for ps in zip(* list(score(t) \n", + " for t in zip(* (list(itertools.islice(reindeer_distance(r), 2503)) \n", + " for r in speeds))))))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.4.3" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/advent14.txt b/advent14.txt new file mode 100644 index 0000000..1532578 --- /dev/null +++ b/advent14.txt @@ -0,0 +1,9 @@ +Vixen can fly 8 km/s for 8 seconds, but then must rest for 53 seconds. +Blitzen can fly 13 km/s for 4 seconds, but then must rest for 49 seconds. +Rudolph can fly 20 km/s for 7 seconds, but then must rest for 132 seconds. +Cupid can fly 12 km/s for 4 seconds, but then must rest for 43 seconds. +Donner can fly 9 km/s for 5 seconds, but then must rest for 38 seconds. +Dasher can fly 10 km/s for 4 seconds, but then must rest for 37 seconds. +Comet can fly 3 km/s for 37 seconds, but then must rest for 76 seconds. +Prancer can fly 9 km/s for 12 seconds, but then must rest for 97 seconds. +Dancer can fly 37 km/s for 1 seconds, but then must rest for 36 seconds. -- 2.34.1