Quarrying with ComputerCraft Turtles

I just recently began playing with ComputerCraft, which is a very cool Minecraft mod that adds programmable computers.   There are also turtles, which are basically computers that can carry out tasks like mining, digging, attacking, etc.  Turtles are fully programmable using Lua and various APIs.  You can do lots of amazing things with turtles.

This is not something amazing, but just a simple modification I made to the turtle's excavate program that allows you to dig stepped quarries giving easier retrieval of the ores around the edges:

9x9 excavate:

excavate14x14x3 quarry:quarry

 Source. You can copy this script directly to your turtle using the turtle's pastebin program.

To dig a quarry, you specify the diameter d and the number of blocks to dig down at each diameter, h_d.

It's a fun exercise to work out some of the properties of the resulting quarries.  Given a d and h_d, you can dig a quarry that is h_t blocks deep, where h_t=floor(\frac{d}{2}h_d) if d is even and h_t=ceil(\frac{d}{2}h_d) if d is odd.  The total height of the quarry is related to the number of constant-diameter levels, N_l by N_l=\frac{h_t}{h_d}.

Given a d and h_d, the total number of blocks you mine in a quarry, N_b, is found by simply adding up all the blocks at a given level and then adding all the levels.  Noting that d decreases by two blocks each time you make the quarry square smaller,

N_b = h_d[d^2 + (d-2)^2 + (d-4)^2 + ... + (d-2\eta)^2]

where \eta is related to N_l.   Writing this in summation form,

N_b = \sum_{m=0}^{\eta}h_d(d-2m)^2

where \eta=ceil(N_l)-1.  Now, the summation bit is straightforward to see, but the upper limit \eta took some playing around in Mathematica for me to work it out.  This seems to work pretty well, but if anyone can derive this limit (or a better one) more "rigorously", please let me know!

The real goofy vs. regular: A look at skateboarders in the SPoT database

In board sports, riders are "goofy" stance if they lead with their right foot and "regular" stance if they lead with their left. These terms seem to imply that one way is more "correct" or common than the other. I'm goofy, so I'd like to know just how goofy it is to be goofy. It turns out, a little bit goofy.

I checked out the Skatepark of Tampa (SPoT) skateboarder database, which has been maintained for 12 years and is probably the most extensive and detailed online.  The database consists of professional and amateur skateboarders who've been to SPoT for some reason or another.  Given that SPoT has been host to two of the largest annual contests in skateboarding since the mid 90's (the Tampa Pro and the Tampa Am), you can bet that the best have been through the doors. Note that in skateboarding, the term amateur (or am) doesn't necessarily imply that the person is less skilled than a professional - it only implies that the person doesn't have their name on a product yet.

SPoT Database Statistics

Of the 6,047 profiles examined, 12.6% are pro.   4,030 of these profiles had stance data, of which only 44% are goofy.  I personally find this a bit surprising, as I assumed it would be closer to 50%.  To put it mathily, a chi-square test indicated that the difference from 50% is statistically significant, \chi^2(1, N=4030) = 57.65, p < .001 , V= .12  (where V is Cramer's V).

Average 22.79
Median 23

Many of the database entries also included age, which is summarized in the table.  Now, this doesn't necessarily mean that the average age of a competing skateboarder is 23 (I think that may be a tad high). To speculate a bit, I'd guess that the database, which has been maintained for 12 years, adds skaters regularly without deleting older ones.  Assuming that most of the ages are updated over time, the database will accumulate ageing skateboarders who aren't actually active anymore.  For more information about skateboarder age, see this Nerdsday Thursday post.

Also, word clouds are really pretty to look at so, why not, here are the most common first and last names in the database:

SPoT DB Common First Names

SPoT_last_namesNothing surprising here, but it does confirm my previous suspicion about the importance of people named "Mike" in the skate industry.  (Or maybe it's just a common name? Who knows, it's a mystery that will never be solved.)

So there we have it, the archetype skateboarder from the SPoT database is Chris Smith, regular, am, 23.  A very interesting follow up would be to see if there is any link between skateboarding stance, footedness in other sports, and handedness.

If statistics gets you pumped up to go skate, you can leave now.  For the other all of you, here's my favorite winning run from the 2011 Tampa Pro, featuring the very goofy Dennis Busenitz:

Minecraft Calculus: River Crossing Escape

This is a classic introductory calculus problem, with a Minecraft theme.  This particular variation is inspired by example 4 in section 4.5 of Stewart's calculus.

I'll set the scene:  You're weaponless.  You have half a heart and you're being chased by a skeleton archer.  There's a river between you and your cabin.  You need to get to your cabin as quickly as possible!

In more "mathy" terms, you want to get from A to B.  There are three main ways you can do this:

  1. A to B directly, diagonally across the river
  2. A to C and then C to B
  3. At slight angle from A to D and then D to B

We know that swimming is slower than running, so option 1 isn't the best.  Option 2 involves the smallest time in the water, but also the longest distance traveled.

Option 3 is somewhere in between - you spend a bit more time in the water, but the total distance is decreased.  The travel time in this case depends on where exactly point D is.  To find the optimal path, we must find the position of D which minimizes the travel time.  This sounds like calculus!

Let x be the distance between points C and D.  In math terms, |CD|=x.

We first need an expression for the total time traveled in terms of x.  The basic equation for time traveled at constant speed is


The first part of the trip is in the water where we travel from A to D.  We can use Pythagorean's theorem to get this distance as |AD|=\sqrt{x^2+w^2}.  Assuming that we can travel at a speed of v_w in the water, the time for this part is


The land part of the trip involves traveling what's left over of l after having already traveled x, so |DB|=l-x.  Traveling at v_l on land, the time for this part of the trip is


The total trip time, T(x), is just the sum of these two.

T(x)=\frac{\sqrt{x^2+w^2}}{v_w}+\frac{l-x}{v_l}     (1)

To optimize this function with respect to x, we need to find where it is stationary and then verify that this point is a minimum.  This means we want to find a place where the function isn't changing with small changes in x.  In other words, we want to find a spot where the derivative is zero.

Taking the derivative gives:


Setting this equal to zero, we can solve for x:

x=\frac{v_ww}{\sqrt{v_l^2-v_w^2}}     (2)

Let's plug in some numbers!  In Minecraft, you can swim at about 2.2 m/s and sprint at 5.6 m/s.  Let's take the river width w to be 7 blocks (1 block = 1 meter).  Plugging in, we find that x=2.9.  At this point, though, we can't tell if this is a maximum or a minimum.  One way to find out is to examine the curvature of the function at this point by using the 2nd derivative:


Plugging in x=2.9, we see that T''(2.9)=0.04 which, being positive, means that x=2.9 is a minimum point for T(x).  Visually:



So if you want to get to your cabin as quickly as possible, the fastest route is to swim across the river to a point D that is 2.9 meters from point C and then run the rest of the way.

This is actually a general problem.  Try replacing the speeds I used with speeds for soul sand, crouching and walking, a boat, etc. and see what happens!  Particularly, what happens to (2) if you can travel more quickly in the water than on land?


Scratch Work

Finding T'(x):  First, rewrite the square root as a power,  T(x)=\frac{(x^2+w^2)^{\frac{1}{2}}}{v_w}+\frac{l-x}{v_l}

Differentiating, using the chain rule on the first term,  T'(x)=\frac{\frac{1}{2}(x^2+w^2)^{-\frac{1}{2}}(2x)}{v_w}-\frac{1}{v_l}=\frac{x}{v_w\sqrt{x^2+w^2}}-\frac{1}{v_l}

Finding T''(x):  Starting with T'(x)=\frac{(x^2+w^2)^{-\frac{1}{2}}x}{v_w}+\frac{1}{v_l}, we differentiate with respect to x.  Note that the derivative of \frac{1}{v_l} with respect to x is 0, so we only have to deal with the first term.  Using the product rule and chain rule:


Cleaning up with some algebra:

T''(x)=\frac{-x^2}{v_w(x^2+w^2)^{\frac{3}{2}}}+\frac{1}{v_w\sqrt{x^2+w^2}} =\frac{1}{v_w\sqrt{x^2+w^2}}(1-\frac{x^2}{x^2+w^2})


From the notes: Two masses on a string going through a hole

Two masses on a string going through a hole.  Yeah, that's the stuff we learn about in physics class.  Anyway, this is the first in a (possibly) series of posts where I work out some problem from my lecture notes or homework.  The reason for doing this is because 1) there's a (small) chance it will be useful or interesting to someone else, and 2) the drawn out process of typing this up and thinking about how to explain it helps me study.

Figure 1, two masses on a string going through a hole.


Consider two masses connected by a string (figure 1).  The ideal string passes through a hole in a plate which is parallel to the x-y plane.  One mass, m is sitting on the plate and is free to rotate about the hole with no friction.  The second mass, M, is hanging below the plate and moves only in the vertical z direction under the influence of gravity.   The total length of the string is l and we define r ands to be the distances between the hole and m and M, respectively, so that l=r+s.

We can use Lagrangian mechanics to explore the system.  In Cartesian coordinates, the total kinetic energy is the sum of the kinetic energy of each mass:

T=\frac{1}{2}m(\dot{x_{m}}^2+\dot{y_{m}}^2+\dot{z_{m}}^2)+\frac{1}{2}M(\dot{x_{M}}^2+\dot{y_{M}}^2+\dot{z_{M}}^2)      (1)

where we've used the dot notation for time derivatives.

Considering that m is only rotating about the z axis and M is moving only vertically, it makes sense to consider switching to a more reasonable coordinate system.  We can transform to cylindrical coordinates with:






Taking derivatives of these with respect to time and inserting them into (1), we can rewrite the kinetic energy in cylindrical coordinates as


where r and \theta are now the generalized coordinates of the system.  The potential energy depends only on the height of M since m is confined to sit on the plate, so


With T and V we can write the Lagrangian L=T-V:


We see that \theta does not appear in the Lagrangian, which means that the generalized momentum corresponding to \theta is a conserved quantity:

p_{\theta}=\frac{\partial L}{\partial \dot{\theta}}=mr^2\dot{\theta}=rmv_m=a     (2)

a can be identified as the angular momentum of mass m.

We can also look at the total energy E=T+V:

E=\frac{1}{2}m(\dot{r}^2+(r\dot{\theta})^2)+\frac{1}{2}M\dot{r}^2-Mg(l-r)     (3)

From (2) we see that \dot{\theta}=\frac{a}{mr^2}.  Substituting this in (3) gives


We can separate the constants on the left hand side and use the remaining terms to define an effective potential V'(r):



Imagine m is rotating about the hole.  This means there will be some centripetal (or centrifugal, depending on your reference system) acceleration which will pull the hanging mass M up.  We want to find r such that the force is an extremum.  In other words:

\frac{\partial V'}{\partial r}=0

Setting the derivative equal to zero gives


And solving for r:


which gives the radius of "orbit" of mass m about the hole for a given angular momentum a such that M remains suspended in the air.


Anti Rage-Induced-Accidental-Disconnect Minecraft Client Mod

We've all been there: You're playing on your favorite Minecraft PvP server, ready to get revenge on the player who just killed you, when suddenly, you're staring blankly at Minecraft's title screen.  How could such a thing happen?  Well, maybe Mr. McMillan has something to say about the matter:


Yes, Mr. McMillan, precisely.  The death screen GUI has "Respawn" and "Title screen" buttons that are way too close to each other, resulting in players sometimes unintentionally leaving the game when they actually want to respawn.  Now, this isn't something that happens all the time, but it happens occasionally and it's annoying.

So, in the first and only installment of the new (and old, by the time you read this) series "Weekly 1 Minute Minecraft Mods", we can fix the problem.

We will change this:



into this:



(Texture pack is the beautiful Plast Pack.)

The new layout isn't pretty, but in the case of PvP I'll definitely take function over fashion (with the exception of gold pants).

Making the Mod

You need to download and install Minecraft Coder Pack (MCP).  There is a lot of information about how to do this online.

Once MCP is ready to go, find the file "GuiGameOver.java" in the minecraft/src subdirectory.  In this file, there are only two lines we need to modify:

this.buttonList.add(new GuiButton(1, this.width / 2 - 100, this.height / 4 + 72, I18n.getString("deathScreen.respawn")));
this.buttonList.add(new GuiButton(2, this.width / 2 - 100, this.height / 4 + 96, I18n.getString("deathScreen.titleScreen")));

These files add the "Respawn" and "Title screen" buttons.  The only thing we need to change is the third argument to the GuiButton constructor.  We can shift the buttons up and down by changing the values of 72 and 96, with larger values corresponding to a lower position on the screen.  The "after" photo shown above uses values of 70 and 120:

this.buttonList.add(new GuiButton(1, this.width / 2 - 100, this.height / 4 + 70, I18n.getString("deathScreen.respawn")));
this.buttonList.add(new GuiButton(2, this.width / 2 - 100, this.height / 4 + 120, I18n.getString("deathScreen.titleScreen")));

Once this is changed, save it and then proceed as you would with any other mod (recompile, reobfuscate, and drop the class in the appropriate Minecraft .jar file after backing everything up, etc.). Now you can crank up your mouse's sensitivity and fail to unintentionally disconnect from your favorite server with ease!

Note that this does involve changing core Minecraft files and so may result in issues if there is an overlap with another mod.  Also, it seems this won't be an issue when 1.7 comes out.

Simple LaTeX Table Generator

Anyone who's ever had to type up a large table in LaTeX knows that it can be a bit of work. When faced with a particulalry large table myself, I of course thought "why not python?".

It turns out there are already a few ways to generate latex tables, but here's my take:

""" This short script converts a CSV table into latex table.
Command Line Arguments:
required positional arguments:
infile input file name
optional arguments:
-h, --help show this help message and exit
-ncols N, --numbercolumns N
number of columns in file
-vd, --verticaldivider
adds vertical dividers to table
-hd, --horizontaldivider
adds horizontal dividers to table
import csv
import sys
import argparse
# define and parse input arguments
parser = argparse.ArgumentParser()
parser.add_argument("infile", help="input file name")
parser.add_argument("-ncols", "--numbercolumns", type=int, help="number of columns in file", default=2)
parser.add_argument("-vd", "--verticaldivider", action="store_true", help="adds vertical dividers to table")
parser.add_argument("-hd", "--horizontaldivider", action="store_true", help="adds horizontal dividers to table")
args = parser.parse_args()
# csv input and latex table output files
infile = args.infile
outfile = infile +".table"
with open(infile, 'r') as inf:
    with open(outfile, 'w') as out:
        reader = csv.reader(inf)
        # build the table beginning code based on number of columns and args
        # columns all left justified
        code_header = "\\begin{tabular}{"
        for i in range(args.numbercolumns):
            code_header += " l "
            if i < args.numbercolumns - 1 and args.verticaldivider:
                code_header += "|"
        code_header += "}\n\\hline\n"
        # begin writing data
        for row in reader:
            # replace "," with "&"
            if args.horizontaldivider:
                out.write(" & ".join(row) + " \\\\ \\hline\n")
                out.write(" & ".join(row) + " \\\\ \n")
        if not args.horizontaldivider:

Example input file:


Running with the -vd and -hd flags to specify vertical and horizontal dividers produces:

\begin{tabular}{ l | l | l }
1 & 2 & 3 \\ \hline
4 & 5 & 6 \\ \hline

It's very minimal, and the main idea is that it does 95% of the work for you, leaving only very minor cosmetic tweaks.