From 10366b26bcbe0ccda6a9e65cc13eae60c4ad1508 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Tue, 15 Dec 2015 10:55:43 +0000 Subject: [PATCH] Done day 15 --- advent-of-code-15.html | 167 +++++++++++ advent-of-code-15_files/css | 16 ++ advent-of-code-15_files/style.css | 453 ++++++++++++++++++++++++++++++ advent15.ipynb | 210 ++++++++++++-- 4 files changed, 816 insertions(+), 30 deletions(-) create mode 100644 advent-of-code-15.html create mode 100644 advent-of-code-15_files/css create mode 100644 advent-of-code-15_files/style.css diff --git a/advent-of-code-15.html b/advent-of-code-15.html new file mode 100644 index 0000000..fa2a1cc --- /dev/null +++ b/advent-of-code-15.html @@ -0,0 +1,167 @@ + + + + +Day 15 - Advent of Code + + + + + + +

Advent of Code

Neil Smith 30*
+ + + +
+

--- Day 15: Science for Hungry People ---

Today, you set out on the task of perfecting your milk-dunking cookie recipe. All you have to do is find the right balance of ingredients.

+

Your recipe leaves room for exactly 100 teaspoons of ingredients. You make a list of the remaining ingredients you could use to finish the recipe (your puzzle input) and their properties per teaspoon:

+
    +
  • capacity (how well it helps the cookie absorb milk)
  • +
  • durability (how well it keeps the cookie intact when full of milk)
  • +
  • flavor (how tasty it makes the cookie)
  • +
  • texture (how it improves the feel of the cookie)
  • +
  • calories (how many calories it adds to the cookie)
  • +
+

You can only measure ingredients in whole-teaspoon amounts accurately, and you have to be accurate so you can reproduce your results in the future. The total score of a cookie can be found by adding up each of the properties (negative totals become 0) and then multiplying together everything except calories.

+

For instance, suppose you have these two ingredients:

+
Butterscotch: capacity -1, durability -2, flavor 6, texture 3, calories 8
+Cinnamon: capacity 2, durability 3, flavor -2, texture -1, calories 3
+
+

Then, choosing to use 44 teaspoons of butterscotch and 56 teaspoons of cinnamon (because the amounts of each ingredient must add up to 100) would result in a cookie with the following properties:

+
    +
  • A capacity of 44*-1 + 56*2 = 68
  • +
  • A durability of 44*-2 + 56*3 = 80
  • +
  • A flavor of 44*6 + 56*-2 = 152
  • +
  • A texture of 44*3 + 56*-1 = 76
  • +
+

Multiplying these together (68 * 80 * 152 * 76, ignoring calories for now) results in a total score of 62842880, which happens to be the best score possible given these ingredients. If any properties had produced a negative total, it would have instead become zero, causing the whole score to multiply to zero.

+

Given the ingredients in your kitchen and their properties, what is the total score of the highest-scoring cookie you can make?

+
+

Your puzzle answer was 18965440.

--- Part Two ---

Your cookie recipe becomes wildly popular! Someone asks if you can make another recipe that has exactly 500 calories per cookie (so they can use it as a meal replacement). Keep the rest of your award-winning process the same (100 teaspoons, same ingredients, same scoring system).

+

For example, given the ingredients above, if you had instead selected 40 teaspoons of butterscotch and 60 teaspoons of cinnamon (which still adds to 100), the total calorie count would be 40*8 + 60*3 = 500. The total score would go down, though: only 57600000, the best you can do in such trying circumstances.

+

Given the ingredients in your kitchen and their properties, what is the total score of the highest-scoring cookie you can make with a calorie total of 500?

+
+

Your puzzle answer was 15862900.

Both parts of this puzzle are complete! They provide two gold stars: **

+

At this point, you should return to your advent calendar and try another puzzle.

+

If you still want to see it, you can get your puzzle input.

+

You can also + this puzzle.

+
+ + + + + + + \ No newline at end of file diff --git a/advent-of-code-15_files/css b/advent-of-code-15_files/css new file mode 100644 index 0000000..8bd1d77 --- /dev/null +++ b/advent-of-code-15_files/css @@ -0,0 +1,16 @@ +/* latin-ext */ +@font-face { + font-family: 'Source Code Pro'; + font-style: normal; + font-weight: 300; + src: local('Source Code Pro Light'), local('SourceCodePro-Light'), url(https://fonts.gstatic.com/s/sourcecodepro/v6/leqv3v-yTsJNC7nFznSMqTRVvBvQIc1z78c__uoBcyI.woff2) format('woff2'); + unicode-range: U+0100-024F, U+1E00-1EFF, U+20A0-20AB, U+20AD-20CF, U+2C60-2C7F, U+A720-A7FF; +} +/* latin */ +@font-face { + font-family: 'Source Code Pro'; + font-style: normal; + font-weight: 300; + src: local('Source Code Pro Light'), local('SourceCodePro-Light'), url(https://fonts.gstatic.com/s/sourcecodepro/v6/leqv3v-yTsJNC7nFznSMqeode0-EuMkY--TSyExeINg.woff2) format('woff2'); + unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2212, U+2215, U+E0FF, U+EFFD, U+F000; +} diff --git a/advent-of-code-15_files/style.css b/advent-of-code-15_files/style.css new file mode 100644 index 0000000..466036c --- /dev/null +++ b/advent-of-code-15_files/style.css @@ -0,0 +1,453 @@ +body { + background: #0f0f23; /*337 x 5*/ + color: #cccccc; + font-family: "Source Code Pro", monospace; + font-size: 14pt; + min-width: 48em; +} +img { border:0; } +a { outline:0; } +main, figure, figcaption { display:block; } +pre, code { font-family: "Source Code Pro", monospace; } +header, main { + -webkit-text-size-adjust: none; +} + +a { + text-decoration: none; + color: #009900; +} +a:hover { + color: #99ff99; +} +h1, h2 { + font-size: 1em; + font-weight: normal; +} +code { + border: 1px solid #333340; + background: #10101a; + display: inline-block; + margin: 1px -2px 1px -2px; + padding: 0 1px 0 1px; +} + +#ad { + float: right; + margin: 0 15px 2em 2em; +} + +header { + white-space: nowrap; + cursor: default; + z-index: 100; + margin-bottom: 1em; +} +header h1 { + display: inline-block; + margin: 0; + padding-right: 1em; +} +header h1 a { + display: inline-block; + text-decoration: none; + color: #00cc00; + text-shadow: 0 0 2px #00cc00, 0 0 5px #00cc00; +} +header h1 a:hover { + color: #99ff99; + text-shadow: 0 0 2px #99ff99, 0 0 5px #99ff99; +} +header .user { + display: inline-block; + padding: 0 .5em; +} +header .user .star-count { + color: #ffff66; +} +header nav { + display: inline-block; + margin-bottom: 1em; +} +header nav ul { + list-style-type: none; + padding: 0; + margin: 0; + display: inline-block; +} +header nav li { + display: inline-block; + padding: 0 .5em; +} +header nav a { + display: inline-block; + text-decoration: none; + outline: none; +} + +input[type="text"] { + background: transparent; + color: inherit; + border: 1px solid #666666; + background: #10101a; + padding: 0 2px; + font-family: inherit; + font-size: inherit; + margin: 0; +} + +input[type="submit"] { + background: transparent; + border: 0; + font-family: inherit; + font-size: inherit; + margin: 0; + padding: 0; + color: #009900; + cursor: pointer; +} +input[type="submit"]:hover { + color: #99ff99; +} +*::-moz-focus-inner { + padding: 0; + border: 0 +} + +article { + width: 45em; + margin-bottom: 2em; + margin-top: 2em; +} +article:first-child { + margin-top: 0; +} +article h2 { + color: #ffffff; + margin-top: 1em; + margin-bottom: 1em; + white-space: nowrap; +} +article h2 + * { + margin-top: 0; +} +article em { + color: #ffffff; + font-style: normal; + text-shadow: 0 0 5px #ffffff; +} +article em.star { + color: #ffff66; + font-style: normal; + text-shadow: 0 0 5px #ffff66; +} +article a { + white-space: nowrap; +} +article .aside { + opacity: .7; +} +article ul { + list-style-type: none; + padding: 0; +} +article li { + padding-left: 2.5em; + position: relative; +} +article li:before { + content: "\00a0\00a0-\00a0"; + position: absolute; + left: 0em; + top: .15em; +} +.day-success { + color: #ffff66; + text-shadow: 0 0 5px #ffff66; +} + +form#settings label img { + vertical-align: bottom; + position: relative; + top: -3px; + margin-right: .3em; +} +form#settings input[type="radio"] { display: none; } +form#settings input[type="radio"] ~ span { + cursor: pointer; + display: inline-block; + min-width: 30em; +} +form#settings input[type="radio"] ~ span:before { + content: "( ) "; +} +form#settings input[type="radio"] ~ span:hover { + background-color: #19193b; +} +form#settings input[type="radio"]:checked ~ span { + color: #ffffff; +} +form#settings input[type="radio"]:checked ~ span:before { + content: "(O) "; +} +form#settings input[type="checkbox"] { display: none; } +form#settings input[type="checkbox"] ~ span { + cursor: pointer; + display: inline-block; + min-width: 30em; +} +form#settings input[type="checkbox"] ~ span:before { + content: "[ ] "; +} +form#settings input[type="checkbox"] ~ span:hover { + background-color: #19193b; +} +form#settings input[type="checkbox"]:checked ~ span { + color: #ffffff; +} +form#settings input[type="checkbox"]:checked ~ span:before { + content: "[X] "; +} +form#settings input[type="checkbox"]:disabled ~ span { + opacity: .3; + cursor: default; +} +form#settings input[type="checkbox"]:disabled ~ span:before { + content: "[-] "; +} +form#settings input[type="checkbox"]:disabled ~ span:hover { + background-color: transparent; +} + +.share { + color: #009900; + cursor: default; + transition: color .2s 1s; + /*position: relative;*/ +} +.share:hover { + color: #aaffaa; + transition: color .2s 0s; +} +.share .share-content { + /*position: absolute; background: #0f0f23;*/ + display: inline-block; + vertical-align: text-bottom; + white-space: nowrap; + overflow: hidden; + max-width: 0; + transition: max-width .2s 1s; +} +.share .share-content:before { + content: "\00a0"; +} +.share .share-content:after { + /*content: "]";*/ +} +.share:hover .share-content { + max-width: 45em; + transition: max-width .2s 0s; +} + +.puzzle-input { + border: 1px solid #999999; + background: #333333; + color: #ffffff; + text-shadow: 0 0 5px #ffffff; +} + +.tree { + cursor: default; + -webkit-user-select: none; + -khtml-user-select: none; + -moz-user-select: -moz-none; + -o-user-select: none; + user-select: none; +} +.tree span { + color: #333333; +} +.tree .tree-trunk { + color: #cccccc; +} +.tree a { + text-decoration: none; + color: #666666; + outline: none; + cursor: default; +} +.tree a:hover { + background-color: #1e1e46; + background-color: rgba(119,119,165,.2); + cursor: pointer; +} +#tree-countdown { + padding-left: .5em; +} +.tree .tree-day { color: #666666; } +.tree a .tree-day { color: #cccccc; } +.tree a.tree-complete, .tree a.tree-verycomplete { + color: #009900; +} +.tree a .tree-mark-complete, +.tree a .tree-mark-verycomplete { visibility: hidden; } +.tree a.tree-complete .tree-mark-complete, +.tree a.tree-verycomplete .tree-mark-complete { visibility: visible; color: #ffff66; } +.tree a.tree-verycomplete .tree-mark-verycomplete { visibility: visible; color: #ffff66; } +.tree a .tree-ornament0 { color: inherit; } +.tree a .tree-ornament1 { color: inherit; } +.tree a .tree-ornament2 { color: inherit; } +.tree a .tree-ornament3 { color: inherit; } +.tree a.tree-verycomplete .tree-ornament0 { color: #0066ff; text-shadow: 0 0 5px #0066ff; } +.tree a.tree-verycomplete .tree-ornament1 { color: #ff9900; text-shadow: 0 0 5px #ff9900; } +.tree a.tree-verycomplete .tree-ornament2 { color: #ff0000; text-shadow: 0 0 5px #ff0000; } +.tree a.tree-verycomplete .tree-ornament3 { color: #ffff66; text-shadow: 0 0 5px #ffff66; } +.tree .tree-lightbeam { visibility: hidden; color: #666666; } +.tree a.tree-day25.tree-complete, +.tree a.tree-day25.tree-verycomplete{ color: #ffff66; } +.tree a.tree-day25.tree-verycomplete{ text-shadow: 0 0 5px #ffff66; } +.tree a.tree-complete .tree-lightbeam, +.tree a.tree-verycomplete .tree-lightbeam { visibility: visible; } +.tree a.tree-verycomplete .tree-lightbeam { color: #ffff66; text-shadow: 0 0 5px #ffff66; } + +.tree.tree-beckon .tree-day1 { animation: anim-beckon 3s infinite -2.875s; } +.tree.tree-beckon .tree-day2 { animation: anim-beckon 3s infinite -2.750s; } +.tree.tree-beckon .tree-day3 { animation: anim-beckon 3s infinite -2.625s; } +.tree.tree-beckon .tree-day4 { animation: anim-beckon 3s infinite -2.500s; } +.tree.tree-beckon .tree-day5 { animation: anim-beckon 3s infinite -2.375s; } +.tree.tree-beckon .tree-day6 { animation: anim-beckon 3s infinite -2.250s; } +.tree.tree-beckon .tree-day7 { animation: anim-beckon 3s infinite -2.125s; } +.tree.tree-beckon .tree-day8 { animation: anim-beckon 3s infinite -2.000s; } +.tree.tree-beckon .tree-day9 { animation: anim-beckon 3s infinite -1.875s; } +.tree.tree-beckon .tree-day10 { animation: anim-beckon 3s infinite -1.750s; } +.tree.tree-beckon .tree-day11 { animation: anim-beckon 3s infinite -1.625s; } +.tree.tree-beckon .tree-day12 { animation: anim-beckon 3s infinite -1.500s; } +.tree.tree-beckon .tree-day13 { animation: anim-beckon 3s infinite -1.375s; } +.tree.tree-beckon .tree-day14 { animation: anim-beckon 3s infinite -1.250s; } +.tree.tree-beckon .tree-day15 { animation: anim-beckon 3s infinite -1.125s; } +.tree.tree-beckon .tree-day16 { animation: anim-beckon 3s infinite -1.000s; } +.tree.tree-beckon .tree-day17 { animation: anim-beckon 3s infinite -0.875s; } +.tree.tree-beckon .tree-day18 { animation: anim-beckon 3s infinite -0.750s; } +.tree.tree-beckon .tree-day19 { animation: anim-beckon 3s infinite -0.625s; } +.tree.tree-beckon .tree-day20 { animation: anim-beckon 3s infinite -0.500s; } +.tree.tree-beckon .tree-day21 { animation: anim-beckon 3s infinite -0.375s; } +.tree.tree-beckon .tree-day22 { animation: anim-beckon 3s infinite -0.250s; } +.tree.tree-beckon .tree-day23 { animation: anim-beckon 3s infinite -0.125s; } +.tree.tree-beckon .tree-day24 { animation: anim-beckon 3s infinite -0.000s; } +.tree.tree-beckon > span { animation-name: anim-beckon-dark ! important; } +@keyframes anim-beckon { + 0% { color: #666666; } + 80% { color: #666666; } + 90% { color: #00ff00; } + 100% { color: #666666; } +} +@keyframes anim-beckon-dark { + 0% { color: #333333; } + 80% { color: #333333; } + 90% { color: #009900; } + 100% { color: #333333; } +} +.tree .tree-day-new { animation: anim-day-new 5s; } +.tree .tree-day-new .tree-day { animation: anim-day-new-day 5s; } +@keyframes anim-day-new { + 0% { color: #333333; text-shadow: 0 0 5px transparent; } + 25% { color: #ffffff; text-shadow: 0 0 5px #ffffff; } + 100% { color: #666666; text-shadow: 0 0 5px transparent; } +} +@keyframes anim-day-new-day { + 0% { color: #666666; text-shadow: 0 0 5px transparent; } + 25% { color: #ffffff; text-shadow: 0 0 5px #ffffff; } + 100% { color: #cccccc; text-shadow: 0 0 5px transparent; } +} + +.tree-bkg { + position: fixed; + left: 0; + right: 0; + bottom: 0; + z-index: -1; + color: #ffffff; + cursor: default; + -webkit-user-select: none; + -khtml-user-select: none; + -moz-user-select: -moz-none; + -o-user-select: none; + user-select: none; +} +.tree-bkg div:nth-child(1) { opacity: .04; } +.tree-bkg div:nth-child(2) { opacity: .08; } +.tree-bkg div:nth-child(3) { opacity: .12; } +.tree-bkg div:nth-child(4) { opacity: .16; } +.tree-bkg div { + text-align: justify; + margin-bottom: -1.2em; +} +.tree-bkg div::after { + content: "_"; + display: inline-block; + visibility: hidden; + width: 100%; +} + +.stats > span, .stats > span .stats-firstonly, .stats > span .stats-both { + color: #666666; +} +.stats > a { + color: #cccccc; + min-width: 35em; + display: inline-block; +} +.stats > a:hover { + background-color: #1e1e46; +} +.stats-firstonly { + color: #666699; +} +.stats-both { + color: #ffff66; +} + +.leaderboard-entry { + white-space: pre; +} +.leaderboard-entry .star-count { + color: #ffff66; +} +.leaderboard-anon { + opacity: .6; +} +.leaderboard-userphoto { + display: inline-block; + height: 20px; + width: 20px; + margin: 0 .5em; + text-align: center; +} +.leaderboard-userphoto img { + height: 20px; + max-width: 20px; + vertical-align: middle; + position: relative; + top: -2px; +} +.leaderboard-time { + opacity: .5; +} + +.privboard-row { + white-space: pre; +} +.privboard-row .star-count { + color: #ffff66; +} +.privboard-name { + display: inline-block; + overflow-x: hidden; + vertical-align: text-bottom; + width: 14em; +} +.privboard-day-locked { color: #333333; } +.privboard-star-locked { visibility: hidden; } +.privboard-star-unlocked { color: #333333; } +.privboard-star-firstonly { color: #666699; } +.privboard-star-both { color: #ffff66; } 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", -- 2.34.1