X-Git-Url: https://git.njae.me.uk/?p=advent-of-code-15.git;a=blobdiff_plain;f=advent15.ipynb;h=51524906ff80e5165d2f07621b83c24591ac188f;hp=ebc895fe62e840125435724a3e193659fcb58c6f;hb=10366b26bcbe0ccda6a9e65cc13eae60c4ad1508;hpb=acab8ea30987c6ee2dcc88b43ca4f2a99af73b77 diff --git a/advent15.ipynb b/advent15.ipynb index ebc895f..5152490 100644 --- a/advent15.ipynb +++ b/advent15.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 24, + "execution_count": 6, "metadata": { "collapsed": true }, @@ -14,7 +14,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 39, "metadata": { "collapsed": false }, @@ -28,7 +28,7 @@ " 'Sugar: capacity 0, durability 0, flavor -2, texture 2, calories 1']" ] }, - "execution_count": 1, + "execution_count": 39, "metadata": {}, "output_type": "execute_result" } @@ -40,7 +40,19 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 36, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "pi15 = ['Butterscotch: capacity -1, durability -2, flavor 6, texture 3, calories 8',\n", + "'Cinnamon: capacity 2, durability 3, flavor -2, texture -1, calories 3']" + ] + }, + { + "cell_type": "code", + "execution_count": 40, "metadata": { "collapsed": false }, @@ -70,7 +82,7 @@ " 'texture': 2}}" ] }, - "execution_count": 9, + "execution_count": 40, "metadata": {}, "output_type": "execute_result" } @@ -91,7 +103,7 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 31, "metadata": { "collapsed": false }, @@ -104,35 +116,59 @@ " property_scores[p] += ingredients[ingredient][p] * quantity\n", " total = 1\n", " for p in property_scores:\n", - " total *= max(property_scores[p], 0)\n", + " if p != 'calories':\n", + " total *= max(property_scores[p], 0)\n", " return total" ] }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 12, "metadata": { - "collapsed": false + "collapsed": false, + "scrolled": true }, "outputs": [ { "data": { "text/plain": [ - "9396000000" + "[(0, 0, 0, 3),\n", + " (0, 0, 1, 2),\n", + " (0, 0, 2, 1),\n", + " (0, 0, 3, 0),\n", + " (0, 1, 0, 2),\n", + " (0, 1, 1, 1),\n", + " (0, 1, 2, 0),\n", + " (0, 2, 0, 1),\n", + " (0, 2, 1, 0),\n", + " (0, 3, 0, 0),\n", + " (1, 0, 0, 2),\n", + " (1, 0, 1, 1),\n", + " (1, 0, 2, 0),\n", + " (1, 1, 0, 1),\n", + " (1, 1, 1, 0),\n", + " (1, 2, 0, 0),\n", + " (2, 0, 0, 1),\n", + " (2, 0, 1, 0),\n", + " (2, 1, 0, 0),\n", + " (3, 0, 0, 0)]" ] }, - "execution_count": 34, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "score([('Frosting', 30), ('Butterscotch', 30), ('Candy', 30), ('Sugar', 10)], ingredients)" + "capacity = 3\n", + "list((a,b,c,(capacity-(a+b+c))) for (a,b,c) in filter(lambda t: sum(t) <= capacity,\n", + " itertools.product(range(capacity+1), range(capacity+1), \n", + " range(capacity+1))))" ] }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 13, "metadata": { "collapsed": false, "scrolled": true @@ -163,7 +199,7 @@ " (3, 0, 0, 0)]" ] }, - "execution_count": 31, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } @@ -175,6 +211,33 @@ " range(capacity+1), range(capacity+1))))" ] }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "18965440" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "capacity = 100\n", + "max(score([('Frosting', f), ('Butterscotch', b), ('Candy', c), ('Sugar', capacity-(f+b+c))], ingredients) \n", + " for b, c, f in filter(lambda t: sum(t) <= capacity,\n", + " itertools.product(range(capacity+1), range(capacity+1), \n", + " range(capacity+1))))" + ] + }, { "cell_type": "code", "execution_count": 38, @@ -186,7 +249,7 @@ { "data": { "text/plain": [ - "10618782720" + "57600000" ] }, "execution_count": 38, @@ -196,32 +259,119 @@ ], "source": [ "capacity = 100\n", - "max(score([('Frosting', f), ('Butterscotch', b), ('Candy', c), ('Sugar', s)], ingredients) \n", - " for b, c, f, s in filter(lambda t: sum(t) == capacity,\n", - " itertools.product(range(capacity+1), range(capacity+1), \n", - " range(capacity+1), range(capacity+1))))" + "max(score([('Cinnamon', c), ('Butterscotch', (capacity-c))], ingredients) \n", + " for (c, ) in filter(lambda t: calories([('Cinnamon', t[0]), ('Butterscotch', (capacity-t[0]))], ingredients) == 500,\n", + " filter(lambda t: sum(t) <= capacity,\n", + " itertools.product(range(capacity+1)))))" ] }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ { - "ename": "TypeError", - "evalue": "unhashable type: 'slice'", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mingredients\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m: unhashable type: 'slice'" - ] + "data": { + "text/plain": [ + "(defaultdict(, {'flavor': 152, 'texture': 76, 'capacity': 68, 'calories': 520, 'durability': 80}),\n", + " 62842880)" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" } ], - "source": [] + "source": [ + "score([('Cinnamon', 56), ('Butterscotch', 44)], ingredients)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "def calories(recipe, ingredients):\n", + " return sum(ingredients[i]['calories'] * quantity for i, quantity in recipe)" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "320" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "calories([('Sugar', 56), ('Butterscotch', 44)], ingredients)" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "320" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "56*1 + 44*6" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "15862900" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "capacity = 100\n", + "max(score([('Frosting', f), ('Butterscotch', b), ('Candy', c), ('Sugar', capacity-(f+b+c))], ingredients) \n", + " for f, b, c in filter(lambda t: calories([('Frosting', t[0]), ('Butterscotch', t[1]), \n", + " ('Candy', t[2]), ('Sugar', capacity-sum(t))], ingredients) == 500,\n", + " filter(lambda t: sum(t) <= capacity,\n", + " itertools.product(range(capacity+1), range(capacity+1), \n", + " range(capacity+1)))))" + ] }, { "cell_type": "code",