Advertisement
Guest User

Length Sum Recipes

a guest
Jun 7th, 2021
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.83 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:
  19.   sel, recipe, sum = explore.pop(0)
  20.   if (sum, sel) not in found:
  21.     found[(sum, sel)] = recipe
  22.     for i in range(sel, len(counts)):
  23.       l, c = counts[i]
  24.       explore.append((i + 1, recipe, sum))
  25.       for j in range(1, c + 1):
  26.         explore.append((i + 1, {**recipe, **{l: j}}, sum + l * j))
  27.  
  28. lastsum = 0
  29. line = 1
  30. for sum, sel in sorted(found):
  31.   if sum != lastsum:
  32.     print(f'{line:>4} {sum:>4}: {found[(sum, sel)]}')
  33.     lastsum = sum
  34.     line += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement