Computercraft Turtle Replication Challenge

I'm throwing my hat in the ring for the Computercraft Competition to make a self-replicating turtle. It's a bit of late entry -- the deadline was Nov 1, 2012, and the forum has long closed. But I love computercraft, so who cares!

Computercraft is a mod for minecraft. In it you program Lua code to control little turtles.

 turtles can only interact with these three blocks
turtles can only interact with these three blocks

They can:

  • Move up, down, and forward. This costs 1 fuel.
  • Look up, down, and forward (1 block) -- they can't see their environment
  • Mine blocks up, down, and forward.
  • Place blocks up, down, and forward.
  • Turn left or right.
  • Look in and manage an inventory of 16 slots
  • Craft items, assuming their inventory is completely empty other than the craft.
  • Refuel, using any item that can be used as fuel in a smelter.
  • Take the FIRST item out of a chest, or dump items in a chest

So... they can mine and turn for free, but moving costs fuel. And the biggest problem is the list of things they can't do:

  • They don't know their position or location
  • They have no idea what's in any block around them, other than directly in front of them
  • They can't interact with a chest other than the very first slot

They have a few capabilities added since the 2012 post, which I'm taking full advantage of

  • They can move an item from one slot in a chest to another slot, and generally look at the list of items in a chest
  • They can detect what item is in their inventory, or what's in front of them. So they learn it's "oak_planks". Previously, all they could do was check whether it was the same as another item in their inventory! Much harder.

This brings us to the challenge, which is to use a computercraft turtle... to build two computercraft turtles. Possible in theory, but in practice I've only seen maybe 1 completion of the challenge. You're guaranteed that the turtle starts at the bottom of an oak tree. There are various additional requirements for the challenge, which I've basically ignored, but I did display the status for the human watcher.

Here's a video of it happening. There's no sound or audio commentary. Sorry!

I proceed in hardcoded phases:

  • Chop a single log, craft it into planks, and consume it for fuel, so the turtle can move.
  • Chop down the first tree. Place a block at the top, so only small oak trees grow (not large oaks, which are more complicated to chop down). Also craft a chest to store materials we gather.
  • Note: At this point I speed up tick speed and place an automatic bonemeal machine to grow the tree, so it's more fun to watch.
  • Continue to chop down trees until we build up enough planks and fuel for later phases. We also add a sign to the left, to update the player on where we're at (phase, fuel, material-gathering progress).
  • Determine the turtle's height by going to some known height and counting back to where we were. We could either go down to bedrock, or up to world height. Since bedrock is bumpy, I picked world height.
  • Dig at ideal gold ore height, gather gold. Along the way, we've gotten some cobblestone.
  • Dig just above bedrock, gathering diamonds and redstone
  • Dig sideways at sea level in a straight line, looking for sand. Note that I temporarily slow down tick speed, because if the turtle moves itself out of loaded chunks, it shuts off and forgets everything.
  • Craft and place a furnace. Smelt the gold and sand.
  • Craft: a glass pane, a computer, a pickaxe, a crafting table, a turtle, and finally a crafting-mining turtle, same as we started it.

Along the way, the turtle refuels when it gets low on fuel, and deposits items in the chest or drops them to clear space for crafting and more gathering.

How long does it take to make two copies? Well, in a deep sense it doesn't matter, because you can keep doubling indefinitely. But just for amusement, let's find out. I added some logging profiling code to find out what the slow steps are, and they tell us the answers.

  • I sped up the tick rate, but luckily the internal clock also gets adjusted the same way, so we can measure what would have been the clock time no problem: main (1 times): 6959 seconds
  • We also bonemealed the trees! So we better take that into account too: awaitTree (22 times): 175 seconds. Let's change that to a more average value. A minecraft tree takes an average of 16 minutes to grow (provided there's space and light -- we actually set it to perpetual noon, but since it would be easy to place a torch, I'll ignore that)

So the real time is 5.8 hours waiting for trees to grow, plus 1.9 hours for everything else -- a total of 7.75 hours.

If you kept re-placing the turtles, that means you'd have over 1 million turtles in a week. (Well, you wouldn't, because chunkloading--but that's something you could do with turtles too, in theory.)

Tagged ,
leave comment

Minecraft Villager Trading Halls: Probability Math

Recently I was making a villager trading hall in minecraft.

One of the main goals of a trading hall is to collect all villager trades. One of the trickiest is books, provided by a librarian. I got to wondering -- how long is this going to take?

Well, we can do some math to find out.

There are currently (as of Minecraft Java Edition 1.21.11) 40 trade-able books. 36 of them are available from the enchanting table, treasure chests[1], or trading.

4 are available only from treasure chests and trading. These are called Treasure enchantments.

  • Curse of Binding
  • Curse of Vanishing
  • Frost Walker I and II
  • Mending

There's also three books, which can be found only from treasure chests. We don't care about them for trading halls:

  • Soul Speed
  • Swift Sneak
  • Wind Burst

There are no books available only from enchanting and not trading.


The core mechanic of searching for book trades is resetting. If we look at a librarian and find it has a trade we don't want, we reset it.

Villagers remember their profession and trades forever after trading. But if we haven't traded with a villager, we simply remove its profession, and then give it a profession again. Then we can see if we like the new starting trades better.

This is very useful for librarians, because they have every book available as a starting trade, so there's no need to investigate later trades for books.

These are the options to make the villager forget their profession I'm aware of:

  • Ignore the villager and get a new one (for example with a breeder), moving or killing the old one. This isn't a "reset" per se, but it acts similarly.
  • Break the profession block manually. In the case of a librarian, the lectern. When breaking the block, the villager loses their profession instantly.
  • Block the path between the villager and their profession block. I haven't seen this documented, but they reset at the same time as trades reset (twice per minecraft day). I did this by dropping the villager 1 block using a piston.
  • Move the villager at least 48 taxicab blocks from their profession block. (Not tested)
  • Move the profession block with a piston. This is an instant reset, but you can't do it for a lectern in Java edition. (Not tested)

Some of these are instant, some take longer. Once villagers are shown a profession block, it only takes them a couple seconds to get their new profession, so that part is easy.

I found breaking and re-placing blocks to be annoying, so I settled on moving librarians up and down with pistons. It takes about 5 real-time minutes for them to reset, so I used about 50 librarians to counteract that. By the time I finished checking all 50 librarians, they were ready to reset again because 5 minutes had passed.

Then the question is: How many librarians do we need to look at, to get every book?


Well, the first question is: what are we interested in? Let's say we're interested in getting each of the 40 enchantments.

Well, it turns out each enchantment is equally likely: there's a 1/40 chance of getting it. Well actually, 1/60 -- there's a chance that no book trade is offered at all.

Then this is the coupon collector's problem, a classic math problem.

The number of trades to look at turns out to be: 3/2 x n x H(n) where H(n) is the n-th harmonic number. For n=40, H(40) = 1/1 + 1/2 + 1/3 + ... + 1/39 + 1/40 = 4.2785. So we need to check 257 librarians on average to get every enchantment.


But, are we really okay with that result? Given that Efficiency V is available as a starting trade, I want a librarian with Efficiency V, not Efficiency I!

There are:

Enchantment Level
Aqua Affinity I
Channeling I
Curse of Binding I
Curse of Vanishing I
Flame I
Infinity I
Mending I
Multishot I
Silk Touch I
Fire Aspect II
Frost Walker II
Knockback II
Punch II
Depth Strider III
Fortune III
Looting III
Loyalty III
Luck of the Sea III
Lunge III
Lure III
Quick Charge III
Respiration III
Riptide III
Sweeping Edge III
Thorns III
Unbreaking III
Blast Protection IV
Breach IV
Feather Falling IV
Fire Protection IV
Piercing IV
Projectile Protection IV
Protection IV
Bane of Arthropods V
Density V
Efficiency V
Impaling V
Power V
Sharpness V
Smite V
  • 9 tradable enchantments with a max level of I
  • 4 with a max level of II
  • 13 with a max level of III
  • 7 with a max level of IV
  • 7 with a max level of V

What's the chance of getting each level of enchantment? It's equal. So for Mending, there's a 1/60 chance to get Mending I, because it's the only choice. For Efficiency, there's a 2/3 * 1/40 x 1/5 = 1/200 chance to get Efficiency I, Efficiency II, or Efficiency V.

How do we calculate the coupon collector's problem for un-equal probabilities? Well... it's really complicated[2].

But the answer is that we will have to talk to an average of 933 librarians to get all enchants at max level.


But hey. We can buy Efficiency V for 17 emeralds, if we get the right trade. Are we really okay getting a 64 emerald trade? What if we want only the best trades?

Enchantment Level Cost
Aqua Affinity I 5-19
Bane of Arthropods V 17-71
Blast Protection IV 14-58
Breach IV 14-58
Channeling I 5-19
Curse of Binding I 10-38
Curse of Vanishing I 10-38
Depth Strider III 11-45
Density V 17-71
Efficiency V 17-71
Feather Falling IV 14-58
Fire Aspect II 8-32
Fire Protection IV 14-58
Flame I 5-19
Fortune III 11-45
Frost Walker II 16-64
Impaling V 17-71
Infinity I 5-19
Knockback II 8-32
Looting III 11-45
Loyalty III 11-45
Luck of the Sea III 11-45
Lunge III 11-45
Lure III 11-45
Mending I 10-38
Multishot I 5-19
Piercing IV 14-58
Power V 17-71
Projectile Protection IV 14-58
Protection IV 14-58
Punch II 8-32
Quick Charge III 11-45
Respiration III 11-45
Riptide III 11-45
Sharpness V 17-71
Silk Touch I 5-19
Smite V 17-71
Sweeping Edge III 11-45
Thorns III 11-45
Unbreaking III 11-45

Mostly, the price range is based only on the level, but there are a few minor complications:

  • Some price ranges go above 64! In the game, these get capped. For this reason, you're 8 times more likely to get Efficiency V for 64 emeralds than any other number.
  • Treasure enchantments (in bold above) are double the price of any other enchantment. This is actually a double -- they're never offered for odd numbers of emeralds. Interesting!

The chance of getting an Efficiency V book at the best possible price is: 1/16,500 = 2/3 x 1/40 x 1/5 x 1/55 (because there are 55 possible different prices -- counting ones above 64).

To get every book at the best price, we'd need to talk to 45,594 librarians[2] to get every max-level enchant at the best price.

[1]: I think [2]: Source code here. This uses the inclusion-exclusion principle to estimate set sizes, together with optimizations to take care of repeat probabilities.

Tagged ,
leave comment

Shaders Gone Mad

Recently I've been trying radically changing my work. Today's challenge: Every hour, on the hour, make my screen worse. Why? Because no one can stop me. And to make things harder, I have to stay on the computer -- I can't just give up and read a book.

I decided to do everything using shaders, a technology that runs directly on a graphics card, so it's very fast.

I applied various shaders to my working Linux environment using 'picom'. As you'll read below, there are a couple limitations to this approach, but overall it was pretty easy to get started. For details on my setup, see the end of the article.

 no shader
no shader

12 noon -- Switch to monochrome, with a blue tint. This is something I've actually done before, to try to ease the pain of a bright monitor on my eyes.

 added blue tint
added blue tint

1pm -- Add 20 degree rotation ; wavy

I rotated the screen 20 degrees. Actually, it applies to each window on its own, so the result was... funky. Each individual window looked like the screenshot below.

 20 degree rotation
20 degree rotation

I gave up on this (too annoying for this early in the day) and tried slow horizontal waves, like you're under the ocean.

 animated waves
animated waves

The first issue was that typing didn't update the whole screen -- I fixed that by adding the following to picom.conf:

unredir-if-possible = false;
vsync = true;
use-damage = false;

But I hit two problems. Both come from the fact that shaders are applied per-window, and the mouse is not part of a window.

  • The mouse is NOT composited. It's not blue, and it stays in one place. If pixels move in the window, the same pixels don't move on the mouse, and you end up not clicking the right place because the mouse doesn't visually get shifted with the window.

I thought about a few workarounds, but they were complicated:

  • Drawing a fake mouse and hiding the real one (doesn't work if the fake mouse is its own window, in terms of position)
  • Somehow moving the entire screen into a window, with something like virtual desktop or nested X servers.

The second problem: it's really hard to hide the uncomposited mouse.

I decided this was really a bit of a distraction, and I'd just work within the contraint that pixels never moved, so I could live with the default mouse.

2pm - Added an animated "bar" of missing pixels. It slowly scrolls top to bottom. You can also see the vaguely "CRT" effect I added as my final 1pm version.

 CRT effect and animated bar
CRT effect and animated bar

3pm - Added a "halo"/ghosting using gaussian blur. This one is a bit hard to see in the screenshot.

 halo effect
halo effect

4pm - Added animated noise effect

 first noise pass
first noise pass
 me watching minecraft on youtube
me watching minecraft on youtube

5pm - Added grain effect

 second noise pass
second noise pass

5pm - Added washed out effect

 washed out
washed out

6pm - add wave, even though the mouse won't line up with what I'm clicking any more.

 added wave animation
added wave animation

8pm - add vertical scroll. Sorry, it's really hard to see in this screenshot.

 vertical scroll -- not really visible in this shot
vertical scroll -- not really visible in this shot

9pm - invert colors, add 20 degree tilt

 tilted 20 degrees with colors inverted
tilted 20 degrees with colors inverted

9:30pm - At this point, I was getting pretty sick to my stomach, so I decided to speed things up... by adding lots of filters really fast, until I couldn't take it. The next one was a 2x2 grid.

 2x2 grid of each window
2x2 grid of each window

And at this point it was unusable, so I called it quits.

A video of what they all look like combined:

 turning shaders off and on
turning shaders off and on

Below are my final shaders and config.

# ~/.config/picom/picom.conf
backend = "glx";
glx-use-copysubbuffer-mesa = true;
glx-no-stencil = true;
glx-no-rebind-pixmap = true;

unredir-if-possible = false;
vsync = true;
use-damage = false;

# Default shader for most windows
window-shader-fg = "/home/zachary/.config/picom/horrible.glsl";
// ~/.config/picom/horrible.glsl
#version 330
uniform sampler2D tex;
in vec2 texcoord;
uniform float time;

vec4 default_post_processing(vec4 c);

vec4 window_shader() {
    vec2 texsize = textureSize(tex, 0);
    vec2 coord = texcoord / texsize;

    if (true) {
        // ========================================
        // 2x2 grid - repeat window 4 times
        // ========================================
        coord = fract(coord * 2.0);
    }

    if (true) {
        // ========================================
        // PASS 5: Rotate entire screen 20 degrees
        // ========================================
        float angle = radians(20.0);
        mat2 rotation = mat2(cos(angle), -sin(angle),
                            sin(angle), cos(angle));
        coord = coord - 0.5;  // Center
        coord = rotation * coord;
        coord = coord + 0.5;  // Un-center
    }

    if (true) {
        // ========================================
        // PASS 4: Bad reception scroll with jank
        // ========================================
        float scrollSpeed = 0.00005;
        float scroll = mod(time * scrollSpeed, 1.0);

        // Add jittery jumps
        float jank = step(0.98, fract(time * 2.3)) * 0.1;
        jank += step(0.95, fract(time * 1.7)) * -0.05;

        coord.y = mod(coord.y + scroll + jank, 1.0);
    }

    if (true) {
        // ========================================
        // PASS 2: Underwater wave distortion
        // ========================================
        float waveAmplitude = 0.1;
        float waveFrequency = 10.0;
        float waveSpeed = 0.00015;

        //coord.y += sin(coord.x * waveFrequency + time * waveSpeed) * waveAmplitude;
        coord.x += cos(coord.y * waveFrequency * 0.7 + time * waveSpeed * 0.8) * waveAmplitude * 0.8;
    }

    // Sample the texture with all distortions applied
    vec4 color = texture2D(tex, coord, 0);

    if (true) {
        // ========================================
        // Simple blur effect
        // ========================================
        vec4 blurred = vec4(0.0);
        float blurAmount = 0.01;  // Blur radius in normalized coords

        // Sample surrounding pixels
        for (float x = -2.0; x <= 2.0; x += 1.0) {
            for (float y = -2.0; y <= 2.0; y += 1.0) {
                vec2 offset = vec2(x, y) * blurAmount;
                blurred += texture2D(tex, coord + offset, 0);
            }
        }
        blurred /= 25.0;  // Average of 5x5 samples
        color = ((color * 1) + blurred)/2;
    }

    if (true) {
        // ========================================
        // TV static noise
        // ========================================
        float noise = fract(sin(dot(coord + time * 0.001, vec2(12.9898, 78.233))) * 437589.5453);
        color.rgb = mix(color.rgb, vec3(noise), 0.2);  // 20% noise, adjust to taste
    }

    if (true) {
        // =======================================
        // Pixel-y static noise
        // ========================================
        float noise = fract(sin(dot(coord + fract(time/800) * 100, vec2(12.9898, 78.233))) * 437589.5453);
        color.rgb = mix(color.rgb, vec3(noise), 0.4);  // 20% noise, adjust to taste
    }

    if (true) {
        // ========================================
        // PASS 3: CRT scanlines
        // ========================================
        float scanlineIntensity = 0.15;
        float scanlineCount = 1080.0;  // Adjust for your screen height

        float scanline = sin(coord.y * texsize.y * 3.14159 * 2.0 / (texsize.y / scanlineCount));
        scanline = scanline * 0.5 + 0.5;  // Remap to 0-1
        color.rgb -= scanlineIntensity * (1.0 - scanline);
    }
    if (true) {
        // ========================================
        // Washed out effect
        // ========================================
        color.rgb = mix(color.rgb, vec3(1.0), 0.4);  // Mix 40% white, adjust 0.4 to taste
    }

    if (true) {
        // ========================================
        // PASS 1: Monochrome + blue underwater tint
        // ========================================
        float gray = dot(color.rgb, vec3(0.299, 0.587, 0.114));
        //color.rgb = vec3(gray);

        // Apply blue underwater tint
        color.rgb = vec3(0.4, 0.6, 1.0) * gray;
    }


    if (true) {
        // ========================================
        // Horizontal dead band (scrolling)
        // ========================================
        float bandHeight = 0.05;
        float bandY = mod(time * 0.0001, 1.0);  // Scrolls from top to bottom, loops

        if (abs(coord.y - bandY) < bandHeight / 2.0) {
            color.rgb = vec3(0.0);
        }
    }

    if (true) {
        // Invert colors
        color.rgb = vec3(1., 1., 1.) - color.rgb;
    }

    return default_post_processing(color);
}
Tagged
leave comment

Hack-a-Day, Day 25: Command line book publishing

Hack-a-Day is my self-imposed challenge to do one project a day, for all of November.

This is my first exception this year - a project that took TWO days (despite best efforts). About 15 hours.

I wrote a program which can take a PDF, and then get it self-published (through lulu.com), and sent to my house.

Source code is on github. This project was co-written with AI, with Claude doing the heavy lifting.

I learned some Playwright along the way.

Expect to see me posting about a bunch of wacky books in the future. Today's is reasonable -- just my recent cookbook update

 This book was ordered by a computer with no human interaction.
This book was ordered by a computer with no human interaction.
Tagged , , ,
leave comment

Hack-a-Day, Day 22: Hack-a-Golf

Hack-a-Day is my self-imposed challenge to do one project a day, for all of November.

 computer mini-golf)
computer mini-golf)

Today's project was mini-golf. I've seen these online, and I thought it was an easy problem (I was mostly right).

It turns out finding the intersection of two lines is really hard, though! It kind of seems easy mathematically, but in practice it's really fiddly with a lot of edge cases. Reflecting is also harder to figure out on a computer than by hand.

My little demo only has one level, but the hard part was the engine -- adding 8 more holes would be pretty easy, I think. There's no hilly slopes or other special features in this verion.

I stayed up too late finishing this one, heh. You can play online here or view the source code on github.

 the path of a ball without friction)
the path of a ball without friction)
Tagged , ,
leave comment

Hack-a-Day, Day 20: 1-D Platformer

Hack-a-Day is my self-imposed challenge to do one project a day, for all of November.

 1-D platfomer (the levels scroll left/right)
1-D platfomer (the levels scroll left/right)

Today's project was a simple platformer. I got something playable, but I wouldn't say it's to the point of actually being a game. Hopefully I'll have time to go back and finish it before the month is out.

I had a lot of fun making this one. I love visual stuff. You can play online here or view the source code on github.

Tagged , ,
leave comment