diff --git a/cactus.py b/cactus.py index 86be5a5..cb93e22 100644 --- a/cactus.py +++ b/cactus.py @@ -6,17 +6,17 @@ import utils # Returns: # Tuple def compute_cost(width, height): - cost = get_cost(Unlocks.Cactus) - seeds = width * height + cost = get_cost(Unlocks.Cactus) + seeds = width * height - for item in cost: - if item == Items.Cactus: - cost[item] = seeds * cost[item] + for item in cost: + if item == Items.Cactus: + cost[item] = seeds * cost[item] - return cost + return cost def place(width, height): - utils.plant_grid(width, height, [Entities.Cactus], 0.0, True) + utils.plant_grid(width, height, [Entities.Cactus], 0.0, True) # Harvests all cacti in the specified area. # @@ -31,116 +31,116 @@ def place(width, height): # width (int): The number of columns in the grid. # height (int): The number of rows in the grid. def sort_and_harvest(width, height): - x = get_pos_x() - y = get_pos_y() - # first sort all by row, and in this loop we keep tally of the sum of all cacti in each row - for row in range(height): - _sort_row_bubble(width) - if row < height - 1: - move(North) - y += 1 - utils.move_to(x, y) + x = get_pos_x() + y = get_pos_y() + # first sort all by row, and in this loop we keep tally of the sum of all cacti in each row + for row in range(height): + _sort_row_bubble(width) + if row < height - 1: + move(North) + y += 1 + utils.move_to(x, y) - y -= (height - 1) - utils.move_to(x, y) + y -= (height - 1) + utils.move_to(x, y) - # now sort each row by column to account for north/south, but we don't need to keep track of the sum of each column - for col in range(width): - _sort_column_bubble(height) - if col < width - 1: - move(East) - x += 1 - utils.move_to(x, y) + # now sort each row by column to account for north/south, but we don't need to keep track of the sum of each column + for col in range(width): + _sort_column_bubble(height) + if col < width - 1: + move(East) + x += 1 + utils.move_to(x, y) - move(West) + move(West) - # we should be fully sorted! so just harvest... - harvest() + # we should be fully sorted! so just harvest... + harvest() - # Sort a row of cacti based on size. Implement bubble sort. - # - # Parameters: - # width (int): The number of columns in the grid. + # Sort a row of cacti based on size. Implement bubble sort. + # + # Parameters: + # width (int): The number of columns in the grid. def _sort_row_bubble(width): - start_index = 0 - end_index = width - 1 - swapped = True + start_index = 0 + end_index = width - 1 + swapped = True - while start_index < end_index and swapped: - swapped = False - - # Forward pass (East): Bubble the largest item to the right (end_index) - # We travel from start_index to end_index - for _ in range(end_index - start_index): - if measure(East) < measure(): - swap(East) - swapped = True - move(East) - - # The rightmost element is now sorted - end_index -= 1 + while start_index < end_index and swapped: + swapped = False + + # Forward pass (East): Bubble the largest item to the right (end_index) + # We travel from start_index to end_index + for _ in range(end_index - start_index): + if measure(East) < measure(): + swap(East) + swapped = True + move(East) + + # The rightmost element is now sorted + end_index -= 1 - if not swapped: - break - - swapped = False - - # Backward pass (West): Bubble the smallest item to the left (start_index) - # We travel from end_index back to start_index - # FIXED: Added +1 to range to ensure we reach start_index - for _ in range(end_index - start_index + 1): - if measure(West) > measure(): - swap(West) - swapped = True - move(West) - - # The leftmost element is now sorted - start_index += 1 - - # Move to the start of the new window - # We are currently at (start_index - 1), so move East once to get to start_index - if start_index < end_index: - move(East) + if not swapped: + break + + swapped = False + + # Backward pass (West): Bubble the smallest item to the left (start_index) + # We travel from end_index back to start_index + # FIXED: Added +1 to range to ensure we reach start_index + for _ in range(end_index - start_index + 1): + if measure(West) > measure(): + swap(West) + swapped = True + move(West) + + # The leftmost element is now sorted + start_index += 1 + + # Move to the start of the new window + # We are currently at (start_index - 1), so move East once to get to start_index + if start_index < end_index: + move(East) - # Sort a column of cacti based on size. Implement bubble sort. - # - # Parameters: - # height (int): The number of rows in the grid. + # Sort a column of cacti based on size. Implement bubble sort. + # + # Parameters: + # height (int): The number of rows in the grid. def _sort_column_bubble(height): - start_index = 0 - end_index = height - 1 - swapped = True + start_index = 0 + end_index = height - 1 + swapped = True - while start_index < end_index and swapped: - swapped = False - - # Forward pass (North): Bubble the largest item to the top (end_index) - for _ in range(end_index - start_index): - if measure(North) < measure(): - swap(North) - swapped = True - move(North) - - end_index -= 1 + while start_index < end_index and swapped: + swapped = False + + # Forward pass (North): Bubble the largest item to the top (end_index) + for _ in range(end_index - start_index): + if measure(North) < measure(): + swap(North) + swapped = True + move(North) + + end_index -= 1 - if not swapped: - break - - swapped = False - - # Backward pass (South): Bubble the smallest item to the bottom (start_index) - # FIXED: Added +1 to range to ensure we reach start_index - for _ in range(end_index - start_index + 1): - if measure(South) > measure(): - swap(South) - swapped = True - move(South) - - start_index += 1 - - if start_index < end_index: - move(North) + if not swapped: + break + + swapped = False + + # Backward pass (South): Bubble the smallest item to the bottom (start_index) + # FIXED: Added +1 to range to ensure we reach start_index + for _ in range(end_index - start_index + 1): + if measure(South) > measure(): + swap(South) + swapped = True + move(South) + + start_index += 1 + + if start_index < end_index: + move(North)