Files
farmer-was-replaced/sunflowers.py

56 lines
1.8 KiB
Python

from __builtins__ import *
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
# to a list of sunflower coordinates containing that petal count.
#
# Parameters:
# width (int): The number of columns in the grid.
# height (int): The number of rows in the grid.
#
# Returns:
# dictionary: A dictionary mapping petal count to a list of sunflower coordinates containing that petal count.
#
# 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):
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 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:
# flowers (dictionary): A dictionary mapping petal count to a list of sunflower coordinates containing that petal count.
#
# 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