Skip to content

Commit 9a12bc9

Browse files
committed
Refactor unique check to occur on both increment and decrement and fix missing floating point handling on greedy search solutions
1 parent 24c20f5 commit 9a12bc9

1 file changed

Lines changed: 19 additions & 14 deletions

File tree

src/Classes/Item.lua

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -829,17 +829,6 @@ function ItemClass:ParseRaw(raw, rarity, highQuality)
829829
local function adjustCombination(values, target, result, best, visited, sum, count)
830830
-- This is used to avoid unnecessary checks on decrement.
831831
local function checkAndAdjustCombination(values, target, result, best, visited, sum, count)
832-
-- Generate a unique key from the result table this prevents duplicates combinations being searched
833-
local key = ""
834-
for _, v in ipairs(values) do
835-
if result[v] and result[v] > 0 then
836-
key = key .. v .. "x" .. result[v]
837-
end
838-
end
839-
840-
if visited[key] then return end
841-
visited[key] = true
842-
843832
-- If it's a valid solution, update best
844833
if math.abs(sum-target) < 1e-9 then
845834
if not best.count or count < best.count then
@@ -861,18 +850,34 @@ function ItemClass:ParseRaw(raw, rarity, highQuality)
861850
end
862851

863852
for _, v in ipairs(values) do
853+
local function checkUnique(result)
854+
-- Generate a unique key from the result table this prevents duplicates combinations being searched
855+
local key = ""
856+
for value, count in ipairs(result) do
857+
if count > 0 then
858+
key = key .. value .. "x" .. count
859+
end
860+
end
861+
if visited[key] then return end
862+
visited[key] = true
863+
end
864+
864865
-- Incrementing is done first as to reach the target you will need to add a count as such it should be more efficient.
865866
-- Try increasing (if it doesn't overshoot or exceed maximum number of remaining runes)
866867
if sum + tonumber(v) <= target + 1e-9 and count + 1 < remainingRunes then
867868
result[v] = (result[v] or 0) + 1
868-
checkAndAdjustCombination(values, target, result, best, visited, sum + v, count + 1)
869+
if checkUnique(result) then
870+
checkAndAdjustCombination(values, target, result, best, visited, sum + v, count + 1)
871+
end
869872
result[v] = result[v] - 1
870873
end
871874

872875
-- Try decreasing (if possible and only if target is still reachable).
873876
if (result[v] or 0) > -1e-9 and (not best.count or target - 1e-9 < sum - tonumber(v) + values[#values] * (best.count - count - 1)) then
874877
result[v] = result[v] - 1
875-
adjustCombination(values, target, result, best, visited, sum - v, count - 1)
878+
if checkUnique(result) then
879+
adjustCombination(values, target, result, best, visited, sum - v, count - 1)
880+
end
876881
result[v] = result[v] + 1
877882
end
878883
end
@@ -889,7 +894,7 @@ function ItemClass:ParseRaw(raw, rarity, highQuality)
889894
end
890895

891896
local greedyCount = 0
892-
if leftover == 0 then -- Greedy search found a solution
897+
if math.abs(leftover) <= 1e-9 then -- Greedy search found a solution
893898
for v, c in pairs(greedySolution) do
894899
greedyCount = greedyCount + c
895900
end

0 commit comments

Comments
 (0)