Skip to content

Commit 34f2c1a

Browse files
committed
Hash categories (which makes it slower :( )
Maybe I messed up or misunderstood... I'll revert this right away since it is 2x slower, probably because of sorted instead of sort?
1 parent 42e9af2 commit 34f2c1a

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

worlds/yachtdice/Rules.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ class Category:
5858
def __init__(self, name, quantity=1):
5959
self.name = name
6060
self.quantity = quantity # how many times you have the category
61+
62+
def __hash__(self):
63+
return hash((self.name, self.quantity))
64+
6165

6266
# return mean score of a category
6367
def mean_score(self, num_dice, num_rolls):
@@ -100,17 +104,17 @@ def extract_progression(state, player, options):
100104
number_of_fixed_mults = state.count("Fixed Score Multiplier", player)
101105
number_of_step_mults = state.count("Step Score Multiplier", player)
102106

103-
categories = [
107+
categories = tuple(
104108
Category(category_value, state.count(category_name, player))
105109
for category_name, category_value in category_mappings.items()
106110
if state.count(category_name, player) # want all categories that have count >= 1
107-
]
111+
)
108112

109113
extra_points_in_logic = state.count("1 Point", player)
110114
extra_points_in_logic += state.count("10 Points", player) * 10
111115
extra_points_in_logic += state.count("100 Points", player) * 100
112116

113-
return categories, number_of_dice, number_of_rerolls, number_of_fixed_mults * 0.1, number_of_step_mults * 0.01, extra_points_in_logic,
117+
return (categories, number_of_dice, number_of_rerolls, number_of_fixed_mults * 0.1, number_of_step_mults * 0.01, extra_points_in_logic)
114118

115119

116120

@@ -125,7 +129,7 @@ def dice_simulation_strings(categories, num_dice, num_rolls, fixed_mult, step_mu
125129
Function that returns the feasible score in logic based on items obtained.
126130
"""
127131
tup = (
128-
tuple([c.name + str(c.quantity) for c in categories]),
132+
categories,
129133
num_dice,
130134
num_rolls,
131135
fixed_mult,
@@ -138,7 +142,7 @@ def dice_simulation_strings(categories, num_dice, num_rolls, fixed_mult, step_mu
138142
return yachtdice_cache[tup]
139143

140144
# sort categories because for the step multiplier, you will want low-scoring categories first
141-
categories.sort(key=lambda category: category.mean_score(num_dice, num_rolls))
145+
categories = sorted(categories, key=lambda category: category.mean_score(num_dice, num_rolls))
142146

143147
# function to add two discrete distribution.
144148
# defaultdict is a dict where you don't need to check if an id is present, you can just use += (lot faster)

0 commit comments

Comments
 (0)