# Launched
> Visualizing Python:
On occasion, I find myself teaching Python, usually while in the process of teaching differential equations, statistics, discrete mathematics, or business. This project uses a nice game that animates Python commands as it highlights the individual lines of code. You can find the game here:

Credit given to Timon Herzog for creating it and to Metaroot for helping to publish it.
They have recently released DLC that extends the coding challenges. I highly recommend it. My hope is both to create a resource that others can use to learn Python and also to demonstrate a little bit of my coding style with some visually pleasing examples.
In order to avoid spoiling the whole game, I won’t be presenting solutions to every challenge, and many other solutions (perhaps better ones) are possible. The main coding ideas illustrated are proper commenting, the development of functions as tools, the development of processes that use those tools, and the development of systems that use those processes.
Also included will be examples of tuning parameters by minimizing the mean plus the standard deviation of runtimes.
Statistics and Tests
This code creates the functions for the mean and standard deviation. You can use this to test whether one solution is better in a statistically significant way or compare solutions by looking at their mean + deviation.
Also created is test code to help maintain the original module if improvements or alterations are needed later.
Statistics Module

Statistics Test Module

Logistics
The most important and fundamental action in the game is to move the drone from one location to the next in a time efficient way. Some challenge allow you to wrap over edges of the map. Others do not.
Therefore, we need a different logistics function for each of those two cases. This code writes those functions.
Logistics Module

Logistics Test


Priority Retrieval
Every planted sunflower has a random number of petals. The resources gathered increases exponentially if the sunflowers with the most petals are picked first. So, we need to write code that scans through the field and picks the flowers in the proper order.
Sunflower Module

Sunflower Test

Planting Carrots
This requires a standardized planting function that takes a watering level as a parameter. Because growth rates are affected by several things in this game, calibrating this parameter with the Statistics module is sometimes worthwhile.
Carrot Module

Carrot Test

Cactii Planting
This requires a standardized planting function that takes a watering level as a parameter. Notice that some cactii are brown and some are green. These plants grow to certain heights. They turn green if they are shorter than all other cactii planted above and/or to the right of them. Since green cactii result in much higher yeild, it is necessary to sort before harvesting. See the longer cactii program below.
Cactii Module

Cactii Testing

Leaderboard and Challenges
The game has a nice leaderboard for people to compete in writing efficient code to accomplish certain tasks, usually gathering a large amount of a particular resource. This code is a naive attempt at the Hay challenge that uses only the most basic features of the game.
As you can see, it performs quite badly.
Leaderboard Code

Naive Hay Code

Improved Challenge Result
As an illustration of one way to improve this time, we have used companion planting. Every grass planted creates a random location (within 3 moves) and a random companion type that it wants at the location. This code manages two harvest locations, measures the companions needed, plants those companions, and then harvests.
While this code is more complicated, it is much, much faster.
Hay Companion Code


Extended Companion
This programm bulk produces resources to help progress through milestones quickly. It uses a large number of simultaneous drones and creates a diamond pattern. In the center of each diamond is a plant whose companion planting request is satisfied. Between diamonds, a filler resource is planted to maximize output.
Extended Companion Code




Error Correcting and Verification
Here, pumpkin plants will increase yeild exponentially if they can form a larger square pumpkin. However, sometimes planting a pumpkin will result in a failure. This requires the drones to make many passes to replant pumkins and verify their proper growth.
It is necessary to create a small time buffer before measurement or the drone will measure before a result (positive or negative) is obtained. We do this with a do_a_flip() command.
Since we are using a large number of drones that cannot share information, we need to use the drone count command to tell the main drone when the subordinate drones have finished checking the field.
Pumpkin Planting Module

Pumpkin Field Code

2D Sorting
As mentioned earlier the Cactii need to be sorted to push the shorter plants down and to the left. Since we don’t want drones to get trapped in loops, they focus on sorting in one direction at a time. Making muliple passes allows them to do the bulk of the sorting. They then need to alternate directions. A verification process could be created. However, testing suggests that it is faster to tweak the parameters for the number of passes until we have a high probability of successful sorting.
2D Sorting Code











Solving a Maze
Here we spawn a large number of drone to find a treasure chest in a maze. The drone moves randomly until it finds a deadend. It then back out of that deadend and prunes the dead path from its set of options.
There is a nice challenge here to remove walls after the treasure is found. This creates loops for the drones to get stuck in. This challenge is also much harder with one drone searching rather than 32. The reader is invited to consider how they could solve those two challenges.
I’ve called this program AI as a bit of a joke. It’s not AI, but neither are LLM’s technically. These two programs will also use the following two data files. One is a dictionary with keys corresponding to every square. This will be used to store the number of times the drone has visted a square and help limit looping behaviour. The other is a set of all permutations of ways a drone can consider its first couple of choices.
A natural question is why not just fully randomize each choice at every crossing the first time a drone encounters that crossing. Unfortunately, the random choice function within the game is slow enough that the perfomance penalty was too high. I might incorporate a linear congruential generator to create a psuedo-random sequence and the reduce it to the cardinal directions by looking at its digits mod 4, but that will have to wait.
Biased AI






Biased AI Search Party


Collision Avoidance
Here we are playing an old game called ‘Snake’ or ‘Worm’ that has been around since the early days of the internet. Since it needs to be automated and the speed increases with every success, this code attacks the probem in stages. First, it searchs the whole field for the first apple. Measuring each apple gives us the randomized location of the next, so we begin using efficient paths to gather them.
As long as the snake is less than twice the length of the field, we can bounce off the top and bottom to avoid collisions. Then, we can start wrapping around the whole field. Lastly, it is efficient to start following a space filling predetermined path after a certain length is reached.
Snake Code










