the_farmer_was_replaced/Cactus.py
2024-09-13 14:16:12 +02:00

77 lines
1.3 KiB
Python

def Cactus():
size = get_world_size()
init_plant(size, Entities.Cactus)
goto((0,0))
field = []
for y in range(size):
row = []
for x in range(size):
goto((x,y))
better_plant(Entities.Cactus)
row.append(measure())
field.append(row)
for y in range(size):
curr = field[y]
while not is_sorted(curr):
for x in range(size):
goto((x,y))
if measure(East) < measure():
swap(East)
if measure(North) < measure():
swap(North)
for x in range(size):
goto((x,y))
curr[x] = measure()
for i in range(len(field)-1, 0, -1):
quick_print(field[i], is_sorted(field[i]))
while num_items(Items.Cactus) < 3000:
harvest()
Cactus()
def diag_sort(mat):
lst = []
n, m = len(mat), len(mat[0])
# leftmost column
for i in range(n):
lst.append([i, 0])
# rightmost row
for i in range(m):
lst.append([0, i])
lst.pop(0)
for el in lst:
i, j = el[0], el[1]
arr = []
# getting the diagonal elements
while i < n and j < m:
arr.append(mat[i][j])
i, j = i+1, j+1
arr = insertion_sort(arr)
i, j = el[0], el[1]
# setting the element in sorted order
while i < n and j < m:
mat[i][j] = arr.pop(0)
i, j = i+1, j+1
return mat
m1 = [
[4,5,6],
[9,8,7],
[2,3,1]
]
for el in m1:
quick_print(el)
m1 = diag_sort(m1)
quick_print("")
for el in m1:
quick_print(el)