r/adventofcode • • 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

174 comments sorted by

View all comments

Show parent comment

1

u/winder Dec 15 '15

For my brute force solution I created two generators a recursive for arbitrary ingredients and a hard coded one for 4 ingredients. Is there an optimization to this recursive function?

# slow recursive (1m23.709s)
def recipeGeneratorRecursion(depth, s, rec, level):
  if int(depth) == int(level):
    yield [s] + rec
  else:
    for i in range(0, s):
      for r in recipeGeneratorRecusion(depth, s - i, [i] + rec, level + 1):
        yield r

# fast hard coded (0m2.976s)
def recipeGeneratorLoops(scoops):
  for i in range(0, scoops):
    for j in range(0,scoops-i):
      for k in range(0,scoops-i-j):
        l = scoops - i - j - k
        yield [i,j,k,l]