refactor: unify planting logic with on_plant callback and improve variable naming

This commit is contained in:
2026-01-10 16:39:00 -05:00
parent 5a16e7a8a3
commit 70caea1243
2 changed files with 51 additions and 102 deletions

View File

@@ -6,7 +6,7 @@ import utils
# Places sunflowers at random locations in the specified area.
#
# We cannot simply just use utils.plant_grid here, because we need to also keep track
# of the number of petals on each sunflower, and return a dictionary mapping petal count
# of the number of petals on each sunflower and return a dictionary mapping petal count
# to a list of sunflower coordinates containing that petal count.
#
# Parameters:
@@ -19,23 +19,38 @@ import utils
# Important:
# utils.plant_grid uses very similar logic with this function, so any changes to this function should be reflected in utils.plant_grid as well.
def place(width, height):
return utils.plant_grid_return_measurements(width, height, [Entities.Sunflower], 0.75, True)
results = {}
def on_plant(x, y):
m = measure()
if m not in results:
results[m] = []
results[m].append((x, y))
utils.plant_grid(width, height, [Entities.Sunflower], 0.75, True, on_plant)
return results
# Harvests all sunflowers in the specified area.
#
# The rules for sunflower harvesting is that we must harvest the flowers with the most petals first
# as doing so gives us an 8x bonus.
# The rules for sunflower harvesting are that we must harvest the flowers with the most petals first
# as doing so harvests eight times more power. Keep a tally of the total power harvested.
#
# Parameters:
# width (int): The number of columns in the grid.
# height (int): The number of rows in the grid.
# flowers (dictionary): A dictionary mapping petal count to a list of sunflower coordinates containing that petal count.
def harvest_grid(width, height, flowers):
result = {}
#
# Returns:
# int: an estimate of the total power harvested. If the game is ever out of sync somehow (e.g.: there's an erroneous
# sunflower on your map outside the provided flowers), this could be inaccurate.
def harvest_grid(flowers):
result = 0
# in reverse order of petals
petals = [15, 14, 13, 12, 11, 10, 9, 8, 7]
for count in petals:
for coord in flowers[count]:
utils.move_to(coord[0], coord[1])
harvest()
result += count * 8
return result