|
| 1 | +# -------------------------------- Input data -------------------------------- # |
| 2 | +import os |
| 3 | + |
| 4 | +test_data = {} |
| 5 | + |
| 6 | +test = 1 |
| 7 | +test_data[test] = {"input": """Butterscotch: capacity -1, durability -2, flavor 6, texture 3, calories 8 |
| 8 | +Cinnamon: capacity 2, durability 3, flavor -2, texture -1, calories 3""", |
| 9 | + "expected": ['Unknown', 'Unknown'], |
| 10 | + } |
| 11 | + |
| 12 | +test += 1 |
| 13 | +test_data[test] = {"input": """""", |
| 14 | + "expected": ['Unknown', 'Unknown'], |
| 15 | + } |
| 16 | + |
| 17 | +test = 'real' |
| 18 | +input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt')) |
| 19 | +test_data[test] = {"input": open(input_file, "r+").read(), |
| 20 | + "expected": ['21367368', '1766400'], |
| 21 | + } |
| 22 | + |
| 23 | +# -------------------------------- Control program execution -------------------------------- # |
| 24 | + |
| 25 | +case_to_test = 'real' |
| 26 | +part_to_test = 2 |
| 27 | +verbose_level = 1 |
| 28 | + |
| 29 | +# -------------------------------- Initialize some variables -------------------------------- # |
| 30 | + |
| 31 | +puzzle_input = test_data[case_to_test]['input'] |
| 32 | +puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1] |
| 33 | +puzzle_actual_result = 'Unknown' |
| 34 | + |
| 35 | + |
| 36 | +# -------------------------------- Actual code execution -------------------------------- # |
| 37 | +from itertools import combinations_with_replacement |
| 38 | +import re |
| 39 | +from functools import reduce |
| 40 | +from collections import Counter |
| 41 | + |
| 42 | +ingredients = {} |
| 43 | + |
| 44 | +for string in puzzle_input.split('\n'): |
| 45 | + if string == '': |
| 46 | + continue |
| 47 | + |
| 48 | + ingredient, capacity, durability, flavor, texture, calories = re.match('([A-Za-z]*): capacity ([0-9-]*), durability ([0-9-]*), flavor ([0-9-]*), texture ([0-9-]*), calories ([0-9-]*)', string).groups() |
| 49 | + ingredients[ingredient] = (int(capacity), int(durability), int(flavor), int(texture), int(calories)) |
| 50 | +print(ingredients) |
| 51 | + |
| 52 | +combinaisons = list(combinations_with_replacement(ingredients, 100)) |
| 53 | +recipe_max = 0 |
| 54 | +for combinaison in combinaisons: |
| 55 | + recipe = Counter(combinaison) |
| 56 | + |
| 57 | + recipe_score = [0 for i in range (4)] |
| 58 | + for ingredient in recipe: |
| 59 | + recipe_score = [recipe_score[i] + recipe[ingredient] * ingredients[ingredient][i] for i in range (4)] |
| 60 | + |
| 61 | + recipe_score = reduce(lambda x, y: x*y if x > 0 and y > 0 else 0, recipe_score) |
| 62 | + |
| 63 | + if part_to_test == 2: |
| 64 | + recipe_calories = sum([recipe[ingredient] * ingredients[ingredient][4] for ingredient in ingredients]) |
| 65 | + if recipe_calories != 500: |
| 66 | + recipe_score = 0 |
| 67 | + recipe_max = max(recipe_max, recipe_score) |
| 68 | + |
| 69 | +puzzle_actual_result = recipe_max |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +# -------------------------------- Outputs / results -------------------------------- # |
| 74 | + |
| 75 | +if verbose_level >= 3: |
| 76 | + print ('Input : ' + puzzle_input) |
| 77 | +print ('Expected result : ' + str(puzzle_expected_result)) |
| 78 | +print ('Actual result : ' + str(puzzle_actual_result)) |
| 79 | + |
| 80 | +combinations_with_replacement |
| 81 | + |
| 82 | + |
0 commit comments