The Farmer Was Replaced Cactus Code — Sort, Then n² Harvest

Rewritten for 1.0+. Cacti have sizes 0–9. When every neighbor is grown and in sorted order, harvesting one cactus harvests the whole connected sorted square for n² cactus.

Sorted order (wiki definition)

  • Plant on soil. Planting costs pumpkins (`get_cost(Entities.Cactus)`).
  • `measure()` → size in 0…9. `measure(East)` reads the neighbor without moving.
  • Sorted iff every grown N/E neighbor is ≥ this cactus, and every grown S/W neighbor is ≤ this cactus.
  • Unsorted grown cacti look brown; sorted turn green. Harvest spreads only through fully grown sorted neighbors → payout `n**2`.
  • `swap(direction)` swaps this tile with the neighbor.

Teaching script: plant → wait → bubble toward NE → harvest

Bubble sort is easy to read, not the fastest. Wait until every cactus can harvest before swapping — sorting unfinished tiles wastes swaps.

python
def go_to(x, y):
	while get_pos_x() != x:
		if get_pos_x() < x:
			move(East)
		else:
			move(West)
	while get_pos_y() != y:
		if get_pos_y() < y:
			move(North)
		else:
			move(South)

def plant_all():
	size = get_world_size()
	for y in range(size):
		for x in range(size):
			go_to(x, y)
			if get_ground_type() != Grounds.Soil:
				till()
			plant(Entities.Cactus)

def wait_until_grown():
	size = get_world_size()
	ready = False
	while not ready:
		ready = True
		for y in range(size):
			for x in range(size):
				go_to(x, y)
				if get_entity_type() != Entities.Cactus or not can_harvest():
					ready = False

def bubble_sort_once():
	# Push larger sizes toward North/East
	size = get_world_size()
	swapped = False
	for y in range(size):
		for x in range(size):
			go_to(x, y)
			here = measure()
			if x < size - 1:
				east = measure(East)
				if east != None and here > east:
					swap(East)
					swapped = True
					here = measure()
			if y < size - 1:
				north = measure(North)
				if north != None and here > north:
					swap(North)
					swapped = True
	return swapped

clear()
while True:
	plant_all()
	wait_until_grown()
	while bubble_sort_once():
		pass
	go_to(0, 0)
	harvest()

Multi-drone / speed notes

  • Bubble sort on 32×32 is slow (many passes). Faster sorts live in community GitHub collections — this page stays readable.
  • Drones do not share memory. Parallel sorts need careful tile ownership or you double-swap the same edge.
  • Leaderboard cactus target factors as 32 perfect full-farm harvests of n² with n = 1024.