Advertisement
Guest User

Length Sum Recipes

a guest
Jun 6th, 2021
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.82 KB | None | 0 0
  1. lengths = ( (20,) * 2 +
  2.             (25,) * 2 +
  3.             (30,) * 2 +
  4.             (35,) * 2 +
  5.             (40,) * 2 +
  6.            (100,) * 4 +
  7.            (200,) * 2 +
  8.            (515,) * 4)
  9.  
  10. counts = {}
  11. for l in lengths:
  12.   counts[l] = counts.get(l, 0) + 1
  13. counts = tuple(sorted(counts.items(), reverse=True))
  14.  
  15. explore = [(0, {}, 0)]
  16. found = {}
  17.  
  18. while explore[0][0] < len(counts):
  19.   sel, recipe, sum = explore.pop(0)
  20.   if (sum, sel) not in found:
  21.     for i in range(sel, len(counts)):
  22.       l, c = counts[i]
  23.       explore.append((sel + 1, recipe, sum))
  24.       for j in range(1, c + 1):
  25.         explore.append((sel + 1, {**recipe, **{l: j}}, sum + l * j))
  26.     found[(sum, sel)] = recipe
  27.  
  28. lastsum = 0
  29. for sum, sel in sorted(found):
  30.   if sum != lastsum:
  31.     print(f'{sum}: {found[(sum, sel)]}')
  32.     lastsum = sum
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement