Compare commits
5 Commits
4e774109ef
...
3eadbfed90
| Author | SHA1 | Date | |
|---|---|---|---|
| 3eadbfed90 | |||
| 6e6d7f43c0 | |||
| 677f84b856 | |||
| 5b18a3224b | |||
| 83b0032c23 |
208
cactus.py
208
cactus.py
@@ -6,17 +6,17 @@ import utils
|
|||||||
# Returns:
|
# Returns:
|
||||||
# Tuple
|
# Tuple
|
||||||
def compute_cost(width, height):
|
def compute_cost(width, height):
|
||||||
cost = get_cost(Unlocks.Cactus)
|
cost = get_cost(Unlocks.Cactus)
|
||||||
seeds = width * height
|
seeds = width * height
|
||||||
|
|
||||||
for item in cost:
|
for item in cost:
|
||||||
if item == Items.Cactus:
|
if item == Items.Cactus:
|
||||||
cost[item] = seeds * cost[item]
|
cost[item] = seeds * cost[item]
|
||||||
|
|
||||||
return cost
|
return cost
|
||||||
|
|
||||||
def place(width, height):
|
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.
|
# Harvests all cacti in the specified area.
|
||||||
#
|
#
|
||||||
@@ -31,116 +31,116 @@ def place(width, height):
|
|||||||
# width (int): The number of columns in the grid.
|
# width (int): The number of columns in the grid.
|
||||||
# height (int): The number of rows in the grid.
|
# height (int): The number of rows in the grid.
|
||||||
def sort_and_harvest(width, height):
|
def sort_and_harvest(width, height):
|
||||||
x = get_pos_x()
|
x = get_pos_x()
|
||||||
y = get_pos_y()
|
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
|
# 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):
|
for row in range(height):
|
||||||
_sort_row_bubble(width)
|
_sort_row_bubble(width)
|
||||||
if row < height - 1:
|
if row < height - 1:
|
||||||
move(North)
|
move(North)
|
||||||
y += 1
|
y += 1
|
||||||
utils.move_to(x, y)
|
utils.move_to(x, y)
|
||||||
|
|
||||||
y -= (height - 1)
|
y -= (height - 1)
|
||||||
utils.move_to(x, y)
|
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
|
# 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):
|
for col in range(width):
|
||||||
_sort_column_bubble(height)
|
_sort_column_bubble(height)
|
||||||
if col < width - 1:
|
if col < width - 1:
|
||||||
move(East)
|
move(East)
|
||||||
x += 1
|
x += 1
|
||||||
utils.move_to(x, y)
|
utils.move_to(x, y)
|
||||||
|
|
||||||
move(West)
|
move(West)
|
||||||
|
|
||||||
|
|
||||||
# we should be fully sorted! so just harvest...
|
# we should be fully sorted! so just harvest...
|
||||||
harvest()
|
harvest()
|
||||||
|
|
||||||
|
|
||||||
# Sort a row of cacti based on size. Implement bubble sort.
|
# Sort a row of cacti based on size. Implement bubble sort.
|
||||||
#
|
#
|
||||||
# Parameters:
|
# Parameters:
|
||||||
# width (int): The number of columns in the grid.
|
# width (int): The number of columns in the grid.
|
||||||
def _sort_row_bubble(width):
|
def _sort_row_bubble(width):
|
||||||
start_index = 0
|
start_index = 0
|
||||||
end_index = width - 1
|
end_index = width - 1
|
||||||
swapped = True
|
swapped = True
|
||||||
|
|
||||||
while start_index < end_index and swapped:
|
while start_index < end_index and swapped:
|
||||||
swapped = False
|
swapped = False
|
||||||
|
|
||||||
# Forward pass (East): Bubble the largest item to the right (end_index)
|
# Forward pass (East): Bubble the largest item to the right (end_index)
|
||||||
# We travel from start_index to end_index
|
# We travel from start_index to end_index
|
||||||
for _ in range(end_index - start_index):
|
for _ in range(end_index - start_index):
|
||||||
if measure(East) < measure():
|
if measure(East) < measure():
|
||||||
swap(East)
|
swap(East)
|
||||||
swapped = True
|
swapped = True
|
||||||
move(East)
|
move(East)
|
||||||
|
|
||||||
# The rightmost element is now sorted
|
# The rightmost element is now sorted
|
||||||
end_index -= 1
|
end_index -= 1
|
||||||
|
|
||||||
if not swapped:
|
if not swapped:
|
||||||
break
|
break
|
||||||
|
|
||||||
swapped = False
|
swapped = False
|
||||||
|
|
||||||
# Backward pass (West): Bubble the smallest item to the left (start_index)
|
# Backward pass (West): Bubble the smallest item to the left (start_index)
|
||||||
# We travel from end_index back to start_index
|
# We travel from end_index back to start_index
|
||||||
# FIXED: Added +1 to range to ensure we reach start_index
|
# FIXED: Added +1 to range to ensure we reach start_index
|
||||||
for _ in range(end_index - start_index + 1):
|
for _ in range(end_index - start_index + 1):
|
||||||
if measure(West) > measure():
|
if measure(West) > measure():
|
||||||
swap(West)
|
swap(West)
|
||||||
swapped = True
|
swapped = True
|
||||||
move(West)
|
move(West)
|
||||||
|
|
||||||
# The leftmost element is now sorted
|
# The leftmost element is now sorted
|
||||||
start_index += 1
|
start_index += 1
|
||||||
|
|
||||||
# Move to the start of the new window
|
# Move to the start of the new window
|
||||||
# We are currently at (start_index - 1), so move East once to get to start_index
|
# We are currently at (start_index - 1), so move East once to get to start_index
|
||||||
if start_index < end_index:
|
if start_index < end_index:
|
||||||
move(East)
|
move(East)
|
||||||
|
|
||||||
|
|
||||||
# Sort a column of cacti based on size. Implement bubble sort.
|
# Sort a column of cacti based on size. Implement bubble sort.
|
||||||
#
|
#
|
||||||
# Parameters:
|
# Parameters:
|
||||||
# height (int): The number of rows in the grid.
|
# height (int): The number of rows in the grid.
|
||||||
def _sort_column_bubble(height):
|
def _sort_column_bubble(height):
|
||||||
start_index = 0
|
start_index = 0
|
||||||
end_index = height - 1
|
end_index = height - 1
|
||||||
swapped = True
|
swapped = True
|
||||||
|
|
||||||
while start_index < end_index and swapped:
|
while start_index < end_index and swapped:
|
||||||
swapped = False
|
swapped = False
|
||||||
|
|
||||||
# Forward pass (North): Bubble the largest item to the top (end_index)
|
# Forward pass (North): Bubble the largest item to the top (end_index)
|
||||||
for _ in range(end_index - start_index):
|
for _ in range(end_index - start_index):
|
||||||
if measure(North) < measure():
|
if measure(North) < measure():
|
||||||
swap(North)
|
swap(North)
|
||||||
swapped = True
|
swapped = True
|
||||||
move(North)
|
move(North)
|
||||||
|
|
||||||
end_index -= 1
|
end_index -= 1
|
||||||
|
|
||||||
if not swapped:
|
if not swapped:
|
||||||
break
|
break
|
||||||
|
|
||||||
swapped = False
|
swapped = False
|
||||||
|
|
||||||
# Backward pass (South): Bubble the smallest item to the bottom (start_index)
|
# Backward pass (South): Bubble the smallest item to the bottom (start_index)
|
||||||
# FIXED: Added +1 to range to ensure we reach start_index
|
# FIXED: Added +1 to range to ensure we reach start_index
|
||||||
for _ in range(end_index - start_index + 1):
|
for _ in range(end_index - start_index + 1):
|
||||||
if measure(South) > measure():
|
if measure(South) > measure():
|
||||||
swap(South)
|
swap(South)
|
||||||
swapped = True
|
swapped = True
|
||||||
move(South)
|
move(South)
|
||||||
|
|
||||||
start_index += 1
|
start_index += 1
|
||||||
|
|
||||||
if start_index < end_index:
|
if start_index < end_index:
|
||||||
move(North)
|
move(North)
|
||||||
|
|||||||
40
main.py
40
main.py
@@ -1,5 +1,6 @@
|
|||||||
import cactus
|
import cactus
|
||||||
import utils
|
import utils
|
||||||
|
import maze
|
||||||
import pumpkins
|
import pumpkins
|
||||||
import sunflowers
|
import sunflowers
|
||||||
from __builtins__ import *
|
from __builtins__ import *
|
||||||
@@ -12,8 +13,8 @@ def plant_carrots(width, height):
|
|||||||
def plant_grass(width, height):
|
def plant_grass(width, height):
|
||||||
utils.plant_grid(width, height, [Entities.Grass], 0.75, False)
|
utils.plant_grid(width, height, [Entities.Grass], 0.75, False)
|
||||||
|
|
||||||
def plant_alternating(width, height, plantWith):
|
def plant_alternating(width, height, plantWith, fn = None):
|
||||||
utils.plant_grid(width, height, plantWith, 0.75, False)
|
utils.plant_grid(width, height, plantWith, 0.75, False, fn)
|
||||||
|
|
||||||
# Entire grid utils:
|
# Entire grid utils:
|
||||||
# These are utilities to plant & harvest the entire grid with specific plants.
|
# These are utilities to plant & harvest the entire grid with specific plants.
|
||||||
@@ -38,6 +39,16 @@ def entire_grid_trees_and_grass():
|
|||||||
utils.move_to(0, 0)
|
utils.move_to(0, 0)
|
||||||
plant_alternating(world_size, world_size, [ Entities.Tree, Entities.Grass ])
|
plant_alternating(world_size, world_size, [ Entities.Tree, Entities.Grass ])
|
||||||
|
|
||||||
|
def entire_grid_trees_and_grass_fertilizer():
|
||||||
|
world_size = get_world_size()
|
||||||
|
utils.move_to(0, 0)
|
||||||
|
|
||||||
|
def on_plant(x, y):
|
||||||
|
use_item(Items.Fertilizer)
|
||||||
|
|
||||||
|
plant_alternating(world_size, world_size, [ Entities.Tree, Entities.Grass ], on_plant)
|
||||||
|
|
||||||
|
|
||||||
def entire_grid_cactus():
|
def entire_grid_cactus():
|
||||||
world_size = get_world_size()
|
world_size = get_world_size()
|
||||||
utils.move_to(0, 0)
|
utils.move_to(0, 0)
|
||||||
@@ -76,12 +87,21 @@ if __name__ == "__main__":
|
|||||||
while num_items(Items.Pumpkin) < 500000:
|
while num_items(Items.Pumpkin) < 500000:
|
||||||
entire_grid_pumpkins()
|
entire_grid_pumpkins()
|
||||||
|
|
||||||
for i in range(10):
|
utils.move_to(0, 0)
|
||||||
entire_grid_trees_and_grass()
|
|
||||||
|
|
||||||
entire_grid_carrots()
|
maze.place(utils.world_size)
|
||||||
entire_grid_carrots()
|
maze.solve(utils.world_size)
|
||||||
entire_grid_pumpkins()
|
|
||||||
|
|
||||||
|
# entire_grid_trees_and_grass_fertilizer()
|
||||||
|
# utils.harvest_grid(get_world_size(), get_world_size())
|
||||||
|
|
||||||
|
# for i in range(10):
|
||||||
|
# entire_grid_trees_and_grass()
|
||||||
|
|
||||||
|
# entire_grid_carrots()
|
||||||
|
# entire_grid_carrots()
|
||||||
|
# entire_grid_pumpkins()
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
worldSize = get_world_size()
|
worldSize = get_world_size()
|
||||||
@@ -91,11 +111,11 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
# only run this if our total power is below 5k.
|
# only run this if our total power is below 5k.
|
||||||
# why? because the rest doesn't use over 5k power.
|
# why? because the rest doesn't use over 5k power.
|
||||||
while num_items(Items.Power) < 5000:
|
while num_items(Items.Power) < 50000:
|
||||||
utils.move_to(0, 0)
|
utils.move_to(0, 0)
|
||||||
grid = sunflowers.place(sectionSize, sectionSize)
|
grid = sunflowers.place(worldSize, worldSize)
|
||||||
utils.move_to(0, 0)
|
utils.move_to(0, 0)
|
||||||
sunflowers.harvest_grid(sectionSize, sectionSize, grid)
|
sunflowers.harvest_grid(grid)
|
||||||
|
|
||||||
utils.move_to(0, 0)
|
utils.move_to(0, 0)
|
||||||
cactus.place(sectionSize, sectionSize)
|
cactus.place(sectionSize, sectionSize)
|
||||||
|
|||||||
95
maze.py
Normal file
95
maze.py
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
import utils
|
||||||
|
from __builtins__ import *
|
||||||
|
|
||||||
|
# Plant a full maze.
|
||||||
|
#
|
||||||
|
# Parameters:
|
||||||
|
# dimensions (tuple): The dimensions of the maze. If not set, default to the maximum size.
|
||||||
|
def place(dimensions=None):
|
||||||
|
if dimensions == None:
|
||||||
|
dimensions = get_world_size()
|
||||||
|
|
||||||
|
plant(Entities.Bush)
|
||||||
|
substance = dimensions * 2**(num_unlocked(Unlocks.Mazes) - 1)
|
||||||
|
use_item(Items.Weird_Substance, substance)
|
||||||
|
|
||||||
|
def solve(dimensions=None):
|
||||||
|
if dimensions == None:
|
||||||
|
dimensions = get_world_size()
|
||||||
|
|
||||||
|
grid_size = get_world_size()
|
||||||
|
|
||||||
|
directions = [North, East, South, West]
|
||||||
|
left_of = {North: West, West: South, South: East, East: North}
|
||||||
|
right_of = {North: East, East: South, South: West, West: North}
|
||||||
|
back_of = {North: South, South: North, East: West, West: East}
|
||||||
|
|
||||||
|
def next_pos(x, y, direction):
|
||||||
|
if direction == North:
|
||||||
|
return x, (y + 1) % grid_size
|
||||||
|
if direction == South:
|
||||||
|
return x, (y - 1) % grid_size
|
||||||
|
if direction == East:
|
||||||
|
return (x + 1) % grid_size, y
|
||||||
|
return (x - 1) % grid_size, y
|
||||||
|
|
||||||
|
def direction_between(x, y, nx, ny):
|
||||||
|
if (x + 1) % grid_size == nx and y == ny:
|
||||||
|
return East
|
||||||
|
if (x - 1) % grid_size == nx and y == ny:
|
||||||
|
return West
|
||||||
|
if x == nx and (y + 1) % grid_size == ny:
|
||||||
|
return North
|
||||||
|
return South
|
||||||
|
|
||||||
|
visited = []
|
||||||
|
x = get_pos_x()
|
||||||
|
y = get_pos_y()
|
||||||
|
visited.append([x, y])
|
||||||
|
|
||||||
|
def is_visited(vx, vy):
|
||||||
|
for i in range(len(visited)):
|
||||||
|
if visited[i][0] == vx and visited[i][1] == vy:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
stack = [[x, y, 0]]
|
||||||
|
|
||||||
|
while len(stack) > 0:
|
||||||
|
current = stack[-1]
|
||||||
|
x = current[0]
|
||||||
|
y = current[1]
|
||||||
|
next_dir_index = current[2]
|
||||||
|
|
||||||
|
if get_entity_type() == Entities.Treasure and can_harvest():
|
||||||
|
harvest()
|
||||||
|
return
|
||||||
|
|
||||||
|
moved = False
|
||||||
|
while next_dir_index < len(directions):
|
||||||
|
direction = directions[next_dir_index]
|
||||||
|
stack[-1][2] = next_dir_index + 1
|
||||||
|
if can_move(direction):
|
||||||
|
next_xy = next_pos(x, y, direction)
|
||||||
|
nx = next_xy[0]
|
||||||
|
ny = next_xy[1]
|
||||||
|
if not is_visited(nx, ny):
|
||||||
|
move(direction)
|
||||||
|
visited.append([nx, ny])
|
||||||
|
stack.append([nx, ny, 0])
|
||||||
|
moved = True
|
||||||
|
break
|
||||||
|
next_dir_index = stack[-1][2]
|
||||||
|
|
||||||
|
if moved:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if len(stack) == 1:
|
||||||
|
return
|
||||||
|
|
||||||
|
prev = stack[-2]
|
||||||
|
prev_x = prev[0]
|
||||||
|
prev_y = prev[1]
|
||||||
|
back_dir = direction_between(x, y, prev_x, prev_y)
|
||||||
|
move(back_dir)
|
||||||
|
stack.pop()
|
||||||
27
utils.py
27
utils.py
@@ -3,15 +3,26 @@ from __builtins__ import *
|
|||||||
world_size = get_world_size()
|
world_size = get_world_size()
|
||||||
|
|
||||||
def move_to(x, y):
|
def move_to(x, y):
|
||||||
while x > get_pos_x():
|
cx = get_pos_x()
|
||||||
move(East)
|
cy = get_pos_y()
|
||||||
while x < get_pos_x():
|
|
||||||
move(West)
|
|
||||||
|
|
||||||
while y > get_pos_y():
|
east_steps = (x - cx) % world_size
|
||||||
move(North)
|
west_steps = (cx - x) % world_size
|
||||||
while y < get_pos_y():
|
if east_steps <= west_steps:
|
||||||
move(South)
|
for _ in range(east_steps):
|
||||||
|
move(East)
|
||||||
|
else:
|
||||||
|
for _ in range(west_steps):
|
||||||
|
move(West)
|
||||||
|
|
||||||
|
north_steps = (y - cy) % world_size
|
||||||
|
south_steps = (cy - y) % world_size
|
||||||
|
if north_steps <= south_steps:
|
||||||
|
for _ in range(north_steps):
|
||||||
|
move(North)
|
||||||
|
else:
|
||||||
|
for _ in range(south_steps):
|
||||||
|
move(South)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user