{ "cells": [ { "cell_type": "code", "execution_count": 212, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Grenadine', 'Bourbon', 'White rum', 'Campari', 'Strawberry syrup', 'Angostura bitters', 'Gin', 'Lime juice', 'Vodka', 'Lillet Blonde', 'Sweet vermouth', 'Dry vermouth', 'Kirsch']\n" ] } ], "source": [ "import json\n", "skip = [\n", " 'Lime juice',\n", " 'Syrup',\n", " 'Lemon juice',\n", " 'Orange juice',\n", " 'Cream',\n", " 'Soda water',\n", " 'Cola',\n", " 'Hot coffee',\n", " 'Egg yolk',\n", " 'Coconut milk',\n", " 'Ginger beer',\n", " 'Pineapple juice',\n", " 'Grapefruit juice',\n", " 'Cranberry juice',\n", " 'Tomato juice',\n", " 'Olive juice',\n", " 'Ginger Ale',\n", "]\n", "skip = []\n", "recipesj = json.load(open('recipes-fix.json'))\n", "ingredients = set()\n", "for recipe in recipesj:\n", " for ingredient in recipe['ingredients']:\n", " if 'special' in ingredient:\n", " continue\n", " if ingredient['ingredient'] in skip:\n", " continue\n", " ingredients.add(ingredient['ingredient'])\n", "ingredientsl = list(ingredients)\n", "ingredientsm = dict(zip(ingredientsl, range(len(ingredientsl))))\n", "recipes = []\n", "for recipe in recipesj:\n", " x = set()\n", " for ingredient in recipe['ingredients']:\n", " if 'special' in ingredient:\n", " continue\n", " if ingredient['ingredient'] in skip:\n", " continue\n", " y = ingredientsm[ingredient['ingredient']]\n", " x.add(y)\n", " recipes.append(x)\n", "n_ingredients = len(ingredientsl)\n", "print(ingredientsl)" ] }, { "cell_type": "code", "execution_count": 213, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "13" ] }, "execution_count": 213, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n_ingredients" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 214, "metadata": {}, "outputs": [ { "ename": "FileNotFoundError", "evalue": "[Errno 2] No such file or directory: 'sol.2'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mlinesf\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'sol.2'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mlines\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlinesf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mread\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msplitlines\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mlinesf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mclose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0msol\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mseen\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mset\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'sol.2'" ] } ], "source": [ "linesf = open('sol.2')\n", "lines = linesf.read().splitlines()\n", "linesf.close()\n", "sol = []\n", "seen = set()\n", "batch = None\n", "batches = []\n", "oldstep = None\n", "for line in lines:\n", " if 'y' in line:\n", " y = line.split()[1].split('_')[-1]\n", " if int(y) in sol:\n", " continue\n", " sol.append(int(y))\n", " if 'r' in line:\n", " step, r = line.split()[1][1:].split('_')\n", " step = int(step)\n", " r = int(r)\n", " if step != oldstep:\n", " if batch is not None:\n", " batches.append((oldstep, batch))\n", " batch = []\n", " oldstep = step\n", " if r not in seen:\n", " batch.append(r)\n", " seen.add(r)\n", "batches.append((oldstep, batch))\n", "len(batches), len(sol)" ] }, { "cell_type": "code", "execution_count": 193, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1. Vodka | Sea Breeze, Screwdriver, Bloody Mary, Moscow Mule\n", "2. White rum | Mojito, Cuba Libre, Pina Colada, Daiquiri\n", "3. Triple Sec | Lemon Drop Martini, Cosmopolitan, Kamikaze\n", "4. Tequila | Margarita, Tommy's Margarita, Vampiro\n", "5. Gin | Long Island Iced Tea, Gin Fizz, John Collins, White Lady\n", "6. Cognac | Horse's Neck, Sidecar, Between the Sheets\n", "7. Galliano | Yellow Bird, Golden Dream, Harvey Wallbanger\n", "8. Grenadine | Bacardi, Tequila Sunrise\n", "9. Cherry liqueur | Aviation, Mary Pickford, Hemingway Special\n", "10. Bourbon | Whiskey Sour, Mint Julep\n", "11. Champagne | Mimosa, French 75\n", "12. Angostura bitters | Old Fashioned, Champagne Cocktail\n", "13. Dry vermouth | Dirty Martini, Dry Martini\n", "14. DiSaronno | God Mother, French Connection\n", "15. Dark rum | Planter's Punch, Dark 'n' Stormy\n", "16. Coffee liqueur | Espresso Martini, Black Russian\n", "17. Créme de Menthe | Stinger\n", "18. Créme de Cacao | Grasshopper, Alexander\n", "19. Pisco | Pisco Sour\n", "20. Créme de Cassis | Russian Spring Punch\n", "21. Dry white wine | Kir\n", "22. Cachaca | Caipirinha\n", "23. Peach bitters | Derby\n", "24. Peach schnapps | Sex on the Beach\n", "25. Absinthe | Monkey Gland\n", "26. Apricot brandy | Paradise\n", "27. Prosecco | Barracuda\n", "28. Raspberry syrup | Clover Club\n", "29. Campari | \n", "30. Sweet vermouth | Negroni, Americano\n", "31. Rye | Manhattan\n", "32. Red Port | Porto Flip\n", "33. Blackberry liqueur | Bramble\n", "34. Irish whiskey | Irish Coffee\n", "35. Peach puree | Bellini\n", "36. Orgeat | Mai-tai\n", "37. Scotch | God Father\n", "38. Orange bitters | Casino\n", "39. Drambuie | Rusty Nail\n", "40. Peychaud's bitters | Sazerac\n", "41. Calvados | Angel Face\n", "42. Aperol | Spritz Veneziano\n", "43. DOM Bénédictine | Singapore Sling\n", "44. Cream liqueur | B52\n", "45. Lillet Blonde | Vesper\n", "46. Maraschino | Tuxedo\n", "47. Orange flower water | Ramos Fizz\n", "48. Raspberry liqueur | French Martini\n", "49. Strawberry syrup | \n", "50. Kirsch | Rose\n" ] } ], "source": [ "def pad(s, n):\n", " return s + (n - len(s))*' '\n", "for i, (y, (step, batch)) in enumerate(zip(sol, batches)):\n", " drinks = [recipesj[r]['name'] for r in batch]\n", " print(pad(str(i+1) + '. ' + ingredientsl[y], 23) + ' | ' + ', '.join(drinks))" ] }, { "cell_type": "code", "execution_count": 169, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "White rum\n", "Vodka\n", "Triple Sec\n", "Cognac\n", "Gin\n", "Tequila\n", "Galliano\n", "Grenadine\n", "Cherry liqueur\n", "Champagne\n", "Bourbon\n", "Angostura bitters\n", "Dark rum\n", "Coffee liqueur\n", "DiSaronno\n", "Dry vermouth\n", "Lillet Blonde\n", "Pisco\n", "Raspberry syrup\n", "Red Port\n", "Cream liqueur\n", "Créme de Menthe\n", "Créme de Cacao\n", "Blackberry liqueur\n", "Raspberry liqueur\n", "Orgeat\n", "Prosecco\n", "Peach puree\n", "Aperol\n", "Peach schnapps\n", "Absinthe\n", "Peach bitters\n", "Irish whiskey\n", "Peychaud's bitters\n", "DOM Bénédictine\n", "Apricot brandy\n", "Calvados\n", "Orange flower water\n", "Créme de Cassis\n", "Dry White Wine\n", "Scotch\n", "Drambuie\n", "Orange Bitters\n", "Maraschino\n", "Cachaca\n", "Campari\n", "Sweet vermouth\n", "Rye\n", "Kirsch\n", "Strawberry syrup\n" ] } ], "source": [ "from collections import Counter\n", "recipesc = recipes.copy()\n", "greedysol = []\n", "\n", "for step in range(n_ingredients):\n", " c = Counter()\n", " for recipe in recipesc:\n", " if len(recipe) == 1:\n", " c.update([list(recipe)[0]])\n", " ingrs = c.most_common()\n", " if len(ingrs) == 0:\n", " ingr = list(set.union(*recipesc))[0]\n", " else:\n", " ingr = ingrs[0][0]\n", " greedysol.append(ingr)\n", " for i, recipe in enumerate(recipesc):\n", " recipesc[i] = recipe - {ingr}\n", "for y in greedysol:\n", " print(ingredientsl[y])" ] }, { "cell_type": "code", "execution_count": 174, "metadata": {}, "outputs": [], "source": [ "def score(recipes, sol):\n", " t = 0\n", " recipesc = recipes.copy()\n", " for i, y in enumerate(sol):\n", " for recipe in recipesc:\n", " if recipe == {y}:\n", " t += (len(ingredientsl)-i)\n", " for i, recipe in enumerate(recipesc):\n", " recipesc[i] = recipe - {y}\n", " return t" ] }, { "cell_type": "code", "execution_count": 175, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2501, 2495)" ] }, "execution_count": 175, "metadata": {}, "output_type": "execute_result" } ], "source": [ "score(recipes, sol), score(recipes, greedysol)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "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.9.5" } }, "nbformat": 4, "nbformat_minor": 4 }