def get_raw_score(p: planet_wars.Planet): """ Returns the basic score of the planet. :param p: `Planet` object :return: `float` score of the planet """ return p.growth_rate()
def defensible(pw: planet_wars.PlanetWars, planet: planet_wars.Planet): quickest_planet = min(pw.my_planets(), key=lambda p: turn_to_take(pw, p, planet)) quickest_turns = turn_to_take(pw, quickest_planet, planet) for t in range(quickest_turns + 1, furthest_meaningful_planet(pw, planet, 2) + 2): my_maximum_ships = max( 0, sum(planet.my_maximum_ships[:t - 1]) - planet.num_ships() + planet.growth_rate() * (t - quickest_turns)) enemy_maximum_ships = sum(planet.enemy_maximum_ships[:t]) if my_maximum_ships < enemy_maximum_ships: return False return True
def turn_to_take(pw: planet_wars.PlanetWars, my_planet: planet_wars.Planet, neutral_planet: planet_wars.Planet): """ Finds the minimum turns to take `neutral_planet` with `my_planet`. :param pw: `PlanetWars` object :param my_planet: `Planet` object :param neutral_planet: `Planet` object :return: `int` turns to take the planet """ distance = pw.distance(my_planet.planet_id(), neutral_planet.planet_id()) if my_planet.num_ships() > neutral_planet.num_ships(): return distance else: lacking_ships = neutral_planet.num_ships() - my_planet.num_ships() + 1 for t in range(pw.map_size): lacking_ships -= my_planet.my_arriving_ships[ t] + my_planet.growth_rate() if lacking_ships <= 0: break else: return 999999 return distance + t
def return_ships(pw: planet_wars.PlanetWars, planet: planet_wars.Planet): quickest_planet = min(pw.my_planets(), key=lambda p: turn_to_take(pw, p, planet)) quickest_turns = turn_to_take(pw, quickest_planet, planet) return planet.growth_rate() * (pw.map_size / 2 - quickest_turns)