Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2021
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. MAXN = 10
  2. INF = 1 << 30
  3. def compute_best(item_name, enchantments):
  4. """
  5. item_name: name of item
  6. enchantments: List of (enchantment_name, enchantment_value)
  7. """
  8. its = 0
  9.  
  10. dp = [[]]
  11. prev = [[]]
  12. root = [[]]
  13. submask_cost = [0]
  14. # item is first element
  15. objs = [(item_name, 0)] + enchantments
  16. for mask in range(1, 1 << len(objs)):
  17. for i in range(len(objs)):
  18. if (mask >> i) & 1:
  19. submask_cost.append(submask_cost[mask^(1<<i)] + objs[i][1])
  20. break
  21.  
  22. if bin(mask).count("1") == 1:
  23. idx = bin(mask).count("0") - 1
  24. dp.append([0 for __ in range(MAXN)])
  25. prev.append([(-1, 0, 0) for __ in range(MAXN)])
  26. root.append([idx for __ in range(MAXN)])
  27. continue
  28.  
  29. submask = (mask - 1) & mask
  30. best = [INF for __ in range(MAXN)]
  31. nprev = [-1 for __ in range(MAXN)]
  32. nroot = [-1 for __ in range(MAXN)]
  33. while submask != 0:
  34. if (mask & 1) == 1 and (submask & 1) == 1:
  35. submask = (submask - 1) & mask
  36. # if mask has final item, it must go on left
  37. continue
  38.  
  39. left = dp[mask^submask]
  40. right = dp[submask]
  41. for h1, v1 in enumerate(left):
  42. for h2, v2 in enumerate(right):
  43. nh = max(h1,h2)
  44. its += 1
  45. if nh+1 < MAXN:
  46. nv = v1 + v2 + (1<<h1) - 1 + (1<<h2) - 1 + submask_cost[submask]
  47. if nv < best[nh+1]:
  48. best[nh+1] = nv
  49. nprev[nh+1] = (submask, h1, h2)
  50. nroot[nh+1] = root[mask^submask][h1]
  51. submask = (submask - 1) & mask
  52.  
  53. dp.append(best)
  54. prev.append(nprev)
  55. root.append(nroot)
  56.  
  57. bestvalue = min(dp[-1])
  58.  
  59. besth = -1
  60. for i in range(MAXN):
  61. if dp[-1][i] == bestvalue:
  62. besth = i
  63. break
  64.  
  65. def format(mask, rt):
  66. other = []
  67. for i in range(len(objs)):
  68. if ((mask>>i)&1) == 1 and i != rt:
  69. other.append(objs[i][0])
  70. other_str = "" if not other else f" (w/ {' + '.join(other)})"
  71. return f"{objs[rt][0]}{other_str}"
  72.  
  73. instructions = []
  74. stack = [((1 << len(objs)) - 1, besth)]
  75. while len(stack) > 0:
  76. submask, curh = stack.pop()
  77. if bin(submask).count("1") == 1:
  78. continue
  79. rightmask, lefth, righth = prev[submask][curh]
  80. leftmask = submask^rightmask
  81. instructions.append(f"Combine {format(leftmask, root[leftmask][lefth])} with {format(rightmask, root[rightmask][righth])}")
  82. stack.append((leftmask, lefth))
  83. stack.append((rightmask, righth))
  84.  
  85. print(its)
  86. return instructions[::-1]
  87.  
  88. print("\n".join(compute_best("Boots", [
  89. ("Soul Speed III", 12),
  90. ("Thorns III", 12),
  91. ("Depth Strider III", 6),
  92. ("Feather Falling IV", 4),
  93. ("Protection IV", 4),
  94. ("Unbreaking III", 3),
  95. ("Mending", 2),
  96. ])))
  97.  
  98. """
  99. Output:
  100. Combine Boots with Soul Speed III
  101. Combine Thorns III with Feather Falling IV
  102. Combine Boots (w/ Soul Speed III) with Thorns III (w/ Feather Falling IV)
  103. Combine Depth Strider III with Unbreaking III
  104. Combine Boots (w/ Soul Speed III + Thorns III + Feather Falling IV) with Depth Strider III (w/ Unbreaking III)
  105. Combine Protection IV with Mending
  106. Combine Boots (w/ Soul Speed III + Thorns III + Depth Strider III + Feather Falling IV + Unbreaking III) with Protection IV (w/ Mending)
  107. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement