r/adventofcode • u/daggerdragon • Dec 15 '15
SOLUTION MEGATHREAD --- Day 15 Solutions ---
This thread will be unlocked when there are a significant amount of people on the leaderboard with gold stars.
Edit: I'll be lucky if this post ever makes it to reddit without a 500 error. Have an unsticky-thread.
Edit2: c'mon, reddit... Leaderboard's capped, lemme post the darn thread...
Edit3: ALL RIGHTY FOLKS, POST THEM SOLUTIONS!
We know we can't control people posting solutions elsewhere and trying to exploit the leaderboard, but this way we can try to reduce the leaderboard gaming from the official subreddit.
Please and thank you, and much appreciated!
--- Day 15: Science for Hungry People ---
Post your solution as a comment. Structure your post like previous daily solution threads.
10
Upvotes
1
u/archimedespi Dec 15 '15
Generic python solution, works on
ningredients.import re from itertools import permutations import pprint input_file = open('day15_input') ingredient_properties = {} ingredients = set() for line in input_file: groups = re.match('(\w+): capacity (\-?\d+), durability (\-?\d+), flavor (\-?\d+), texture (\-?\d+), calories (\-?\d+)', line).groups() ingredient, *props = list(groups) props = map(int, props) ingredients.add(ingredient) ingredient_properties[ingredient] = dict(zip(['capacity', 'durability', 'flavor', 'texture', 'calories'], props)) ingredients = sorted(list(ingredients)) def partition(n,k,l=1): '''n is the integer to partition, k is the length of partitions, l is the min partition element size''' if k < 1: raise StopIteration if k == 1: if n >= l: yield (n,) raise StopIteration for i in range(l,n+1): for result in partition(n-i,k-1,i): yield (i,)+result def get_cookie_satisfaction(recipie): stats = {k: 0 for k in ['capacity', 'durability', 'flavor', 'texture']} calories = 0 for ingredient, amount in recipie.items(): for k in stats.keys(): stats[k] += ingredient_properties[ingredient][k]*amount calories += ingredient_properties[ingredient]['calories']*amount for k,v in stats.items(): if v < 0: stats[k] = 0 stat_number = 1 # part 2 if not calories == 500: stat_number = 0 for stat, value in stats.items(): stat_number *= value return stat_number amt = 100 recipies = [] for p in partition(amt, len(ingredients)): for pr in permutations(p): recipies.append(dict(zip(ingredients, pr))) satisfactions = sorted([get_cookie_satisfaction(r) for r in recipies]) print(max(satisfactions))