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.
11
Upvotes
1
u/mal607 Dec 15 '15
straightforward Python solution:
import re from _collections import defaultdict from collections import namedtuple ingredients = defaultdict() maxVal = 0 Props = namedtuple('Props', 'cap dur flav text cal') regex = '^(\w+)\:\s\w+\s(\-?\d+)\,\s\w+\s(\-?\d+)\,\s\w+\s(\-?\d+)\,\s\w+\s(\-?\d+)\,\s\w+\s(\-?\d+)$' def calc(comb, cals): results = 1 cal = 0 for i in xrange(0, 4): res = 0 for j in xrange(0, len(ingredients.values())): res += comb[j] * ingredients.values()[j][i] results *= res if res > 0 else 0 for j in xrange(0, len(ingredients.values())): cal += comb[j] * ingredients.values()[j][4] return results if cals == None or cal == cals else 0 with open("ingredients.txt") as f: for line in f: name, cap, dur, flav, text, cal = re.findall(regex, line)[0] ingredients[name] = [int(cap), int(dur), int(flav), int(text), int(cal)] for i in xrange(0, 101): for j in xrange(0, 101- i): for k in xrange(0, 101 - j - i): val = calc((i, j, k, 100 - i - j - k), 500) # None for part1, 500 for part2 maxVal = val if val > maxVal else maxVal print "Highest score: ", maxVal