{ "cells": [ { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from collections import defaultdict\n", "import itertools" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['Frosting: capacity 4, durability -2, flavor 0, texture 0, calories 5',\n", " 'Candy: capacity 0, durability 5, flavor -1, texture 0, calories 8',\n", " 'Butterscotch: capacity -1, durability 0, flavor 5, texture 0, calories 6',\n", " 'Sugar: capacity 0, durability 0, flavor -2, texture 2, calories 1']" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pi15 = [l.strip() for l in open('advent15.txt').readlines()]\n", "pi15" ] }, { "cell_type": "code", "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": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{'Butterscotch': {'calories': 6,\n", " 'capacity': -1,\n", " 'durability': 0,\n", " 'flavor': 5,\n", " 'texture': 0},\n", " 'Candy': {'calories': 8,\n", " 'capacity': 0,\n", " 'durability': 5,\n", " 'flavor': -1,\n", " 'texture': 0},\n", " 'Frosting': {'calories': 5,\n", " 'capacity': 4,\n", " 'durability': -2,\n", " 'flavor': 0,\n", " 'texture': 0},\n", " 'Sugar': {'calories': 1,\n", " 'capacity': 0,\n", " 'durability': 0,\n", " 'flavor': -2,\n", " 'texture': 2}}" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ingredients = {}\n", "for l in pi15:\n", " ls = l.split(': ')\n", " name = ls[0]\n", " props = ls[1].split(', ')\n", " properties = {}\n", " for p in props:\n", " ps = p.split(' ')\n", " properties[ps[0].strip()] = int(ps[1].strip())\n", " ingredients[name] = properties\n", "ingredients" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def score(recipe, ingredients):\n", " property_scores = defaultdict(int)\n", " for ingredient, quantity in recipe:\n", " for p in ingredients[ingredient]:\n", " property_scores[p] += ingredients[ingredient][p] * quantity\n", " total = 1\n", " for p in property_scores:\n", " if p != 'calories':\n", " total *= max(property_scores[p], 0)\n", " return total" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "[(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": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "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": 13, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "[(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": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "capacity = 3\n", "list(filter(lambda t: sum(t) == capacity,\n", " itertools.product(range(capacity+1), range(capacity+1), \n", " 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, "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": 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, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "57600000" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "capacity = 100\n", "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": 28, "metadata": { "collapsed": false }, "outputs": [ { "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": [ "score([('Cinnamon', 56), ('Butterscotch', 44)], ingredients)" ] }, { "cell_type": "code", "execution_count": 10, "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": 11, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "15862900" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "capacity = 100\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)))))" ] }, { "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 }