X-Git-Url: https://git.njae.me.uk/?p=advent-of-code-15.git;a=blobdiff_plain;f=advent15.ipynb;fp=advent15.ipynb;h=06fbcaef1fdf8886077b88bdcfcff9247ed600f5;hp=51524906ff80e5165d2f07621b83c24591ac188f;hb=56e1be9ad16bcdf72f063083e7955f0145df4c2e;hpb=10366b26bcbe0ccda6a9e65cc13eae60c4ad1508 diff --git a/advent15.ipynb b/advent15.ipynb index 5152490..06fbcae 100644 --- a/advent15.ipynb +++ b/advent15.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 6, + "execution_count": 3, "metadata": { "collapsed": true }, @@ -14,7 +14,7 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 4, "metadata": { "collapsed": false }, @@ -28,7 +28,7 @@ " 'Sugar: capacity 0, durability 0, flavor -2, texture 2, calories 1']" ] }, - "execution_count": 39, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -52,7 +52,7 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": 5, "metadata": { "collapsed": false }, @@ -82,7 +82,7 @@ " 'texture': 2}}" ] }, - "execution_count": 40, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -103,7 +103,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 6, "metadata": { "collapsed": false }, @@ -211,6 +211,18 @@ " range(capacity+1), range(capacity+1))))" ] }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def recipe(t, capacity):\n", + " return [('Frosting', t[0]), ('Butterscotch', t[1]), ('Candy', t[2]), ('Sugar', capacity-sum(t))]" + ] + }, { "cell_type": "code", "execution_count": 32, @@ -238,6 +250,33 @@ " range(capacity+1))))" ] }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "18965440" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "capacity = 100\n", + "max(score(recipe(t, capacity), ingredients) \n", + " for t 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, @@ -290,7 +329,7 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 10, "metadata": { "collapsed": false }, @@ -346,7 +385,7 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": 11, "metadata": { "collapsed": false, "scrolled": true @@ -358,16 +397,15 @@ "15862900" ] }, - "execution_count": 42, + "execution_count": 11, "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", + "max(score(recipe(t, capacity), ingredients) \n", + " for t in filter(lambda t: calories(recipe(t, capacity), ingredients) == 500,\n", " filter(lambda t: sum(t) <= capacity,\n", " itertools.product(range(capacity+1), range(capacity+1), \n", " range(capacity+1)))))"