qr-backup C restore program

In 2025, I posted two bounties for qr-backup. One I did myself, and the other has now been completed by Claude. The task was as follows:

qr-backup is designed to save to paper, and restore from the command-line.

[...]

this feature request is to add a printable, 1-page short C program which you can type in by hand, compile, and use to restore backups from an image.

This is a very difficult technical challenge in minimization. You should provide a 1-page (2KB) short version of qr-backup's restore process, written in C. Library use is not allowed.

[...]

The reward is 5 hours of my time and everlasting fame.

Claude has succeeded! Although, it took seven hours of my time to get it to succeed so... I'm deducting that. It gets nothing. Sorry, Claude.

First, I asked it to just write any C program that can do a restore. That's... already extremely hard, especially the QR decoding part. I really had to hand-hold it through how to set up testing. Advice: suggest red/green testing. I also had to tell it to test incrementally. For example, when decoding a QR, check where the finders are. Then compare that to where you know the finders are -- don't try testing end-to-end. Also -- a reminder to freely look at other open-source QR decoders, and add print debugging to that, to see intermediate output! For example, it's really useful to see the parsed grid of pixels from a QR, to make sure you're doing that step right.

Sampled QR (21x21):
#######....#..#######
#.....#..##.#.#.....#
#.###.#.##..#.#.###.#
#.###.#.#.#...#.###.#
#.###.#.##..#.#.###.#
#.....#.#.#.#.#.....#
#######.#.#.#.#######
........#............
#.#####..##.#.#####..
#..###.#######..#.###
#.#...##...###.##.#.#
#..#.#.##.#.#..#.....
.#.##.###.#..#.#.#...
........##.....#..#..
#######....#...#..##.
#.....#.#.###..##.#..
#.###.#.##.#.#.##.##.
#.###.#.#.#.#.#...#..
#.###.#.#.##.#..#.#..
#.....#..#..##.####.#
#######.##..#.#.##...

At this point, most of my contributions to LLM work are contributing my own heuristics, or noticing when they're stuck. In a couple months they won't need me past goal-setting. Also, I seem to be a bit better at logical thinking? LLMs are not great at debugging in the "human" way.

Anyway, eventually we got a working version! It's very complicated. First, we look at the image of a QR, and locate the "finders" -- the three squares on the corners.

 finders in three corners of a QR code
finders in three corners of a QR code

How do we locate them? They have black-white-black-white-black runs in the ratio of: 1:1:3:1:1, passing through the center, along horizontal and vertical lines.

 false positives are no big deal
false positives are no big deal

If it's rotated, it's still the same ratio through the center -- but every length is up to 41% longer.

This gets us several "candidate" centers. There might be some false positives (shown above), but the three real finders should be included. Then we basically guess the right finders, and run some checks. In the worst case, a check could be "try to decode the QR, assuming this is right" -- it's brute-force but it works.

 explaining the concept of flattening to a technical audience
explaining the concept of flattening to a technical audience

We apply homography to "flatten" and de-rotate the image, using the finder locations. Then we detect the QR version, size, encoding, and apply error correction for any damaged or smudged pixels.

Now we have a QR decoder! But the qr-backup process is a bit more complicated. We also:

  • Combine the QRs in order (they have numbers at the beginning of each)
  • Base-64 decode the contents of each
  • Apply erasure coding which restores missing or damaged QRs.
  • Decompress the zipped contents
  • Decrypt using standard gpg, using a user-supplied password
  • Do a checksum of the result to make sure the data is undamaged.

We do all of this with no libraries (other than libc).

The result is the qr_restore.c. It clocks in at 5300 lines (208K).

What's next? Why, we want to start trimming the fat of course. Let's remove any GPG features we don't need, redundant code, etc. We'll generate tables instead of hard-coding them. We'll remove intermediate debug outputs, like the .png s above showing where finders are.

This process takes multiple days and very hard work.

Now we have qr_strip.c, sitting at only 2000 lines of code. It's still human-readable, but much simpler.

Now the final pass. C code doesn't need whitespace. And why use all those long, descriptive variable names?

 why are all my code reviews just AAAAAAAAH
why are all my code reviews just AAAAAAAAH

Running it through a custom minifier, we reduce the size even smaller.

 from 208K to 35K
from 208K to 35K

If we print in really tiny font, we can fit that on a single page.


Limitations. At the time of writing, decoded some rotated/skewed QRs is a bit broken. I'm not sure why, they used to be working. I'll fix it soon, and post updated code/PDFs.

Tagged ,
leave comment

"Steganographic" Rickroll

Claude wrote me a tool to encode data in videos.

Here is a copy of Rick Astley's "Never Gonna Give You Up".

Encoded in the (fairly obvious) big on-screen pixels is a copy of the complete works of William Shakespeare.

They can be decoded with this tool.

curl https://gist.githubusercontent.com/za3k/d2d993685a23be04bc3ee5b28281e6d4/raw/0a27700266ff726604d802a9263bda552c57b67a/gistfile1.txt -o decode.py
yt-dlp "https://www.youtube.com/watch?v=EAhEepVNYj8" -o rickroll.mkv
python decode.py rickroll.mkv

Thanks Claude!

Tagged ,
leave comment

DDRPad.com dance pad under Linux

I had quite a struggle getting this pad working under linux. Here's how I did it on USB.

First, get rid of the built-in xpad module, which doesn't work.

sudo modprobe -r xpad
echo "blacklist xpad" | sudo tee /etc/modprobe.d/xpad.conf

Then install and test xboxdrv

sudo pacman -S xboxdrv
sudo xboxdrv --detach-kernel-driver --dpad-as-button

You can test with evtest. Pick the XBox controller. If it shows up and shows events when you press buttons, that's good. Especially test holding left and right at the same time -- you should see two "1" events. There's a bug in most USB adapters, because most controllers don't let you hold dpad left and dpad right at the same time. You want to make sure both work at the same time for DDR.

Assuming that works fine, you can play now! Let's add it to systemd. Make these two files as root in any text editor.

# /etc/udev/rules.d/99-dancepad.rules
ACTION=="add", ATTRS{idVendor}=="054c", ATTRS{idProduct}=="0268", ATTRS{product}=="PLAYSTATION(R)3 Controller", TAG+="systemd", ENV{SYSTEMD_WANTS}="xboxdrv-dancepad.service"

 

# /etc/systemd/system/xboxdrv-dancepad.service
[Unit]
Description=Xbox controller driver for dance pad

[Service]
Type=simple
ExecStart=/usr/bin/xboxdrv --detach-kernel-driver --dpad-as-button
Restart=on-failure

[Install]
WantedBy=multi-user.target

And then you need to restart or run:

sudo systemctl daemon-reload
sudo udevadm control --reload-rules

It should work now. If you have the EXACT same pad and it doesn't, drop me a comment/email and I'll try to help.

Some failed attempts:

  • The raphnet controller sold on DDRPad.com doesn't add anything -- it shows up as XBox. I didn't actually re-check after the above, but you shouldn't need to order it.
  • The Wingman FGC retro (ZPP006M) didn't work. Nothing showed up.
  • The very cheap Amazon controllers (mine was sold as "Xahpower" but the hardware presents as SHANWAN). They work, but they can't do left+right together.
  • I even tried a kernel patch, which does seem like it works, but not on this pad (not that it's needed).

Debugged with help from Claude.

Tagged ,
leave comment

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/300 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.


Addendum: As I later noticed, as well as at least one commenter, there's no reason to get Efficient V at 17 emeralds. You can get it at 21 emeralds if you plan to cure the villagers -- because the price will still drop to 1 emerald after the cure.

The current cure mechanic is: book prices drop 20 emeralds, and you can't permanently stack more than 1 cure. So we're happy with any price 21 emeralds or lower, for any book, as the "best possible" price.

The chance of getting an Efficiency V book at the best is now: 1/3300 = 2/3 x 1/40 x 1/5 x 5/55. The chance of getting of getting an Aqua Affinity book at the best price is 1/60 = 2/3 x 1/40, because the whole price range of 5-19 is under 21!

Under these assumptions, we'd need to talk to 2,741 librarians[2] to get every max-level enchant at the best discounted 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

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 15: Vibe Chat

Today's project was a vibe-coded chat program. For those unfamiliar, "vibe coding" is programming where an AI does the majority of the coding, and in fact is often undertaken by non-programmers. In my case I took an approach a bit closer to "architect" than entirely hands-off, but an LLM did all the heavy lifting.

The code is here -- roughly one commit per interaction, with a few combined. The prompts are not included.

I've mostly been using AI very little during hack-a-day... sometimes to help debug, and in one case to write another "boring bit" (convert Minecraft world to JSON, for the voxel engine). It might get stuff done, but it's not going to improve the same set of skills to do stuff with an AI. And I'm generally a bit wary of using AI, because it can really just spew some absolute bullshit, which is in my head afterwards.

I've had a relatively better experience using Anthropic's Claude than most other products (for which I have a paid plan). Unfortunately they have very opaque usage caps, and I'd hit limits repeatedly during this project. Then it would say "please try again at 4pm" (in 3 hours). So I pretty much ran out of LLM usage on this one.

Overall I'd say I got to do some coding I usually wouldn't. The project was a curses frontend for a chat (and backend, but that didn't really get done yet). Something like making a curses interface would usually be a bit too boring for me--being able to collaborate with an LLM, who doesn't find such things boring, is great. Other than tooling issues, the main problem is that Claude doesn't write the best code. It generally has a very "junior programmer" vibe, with no use of abstraction, and tends toward the verbose.

My general take on AI though is that someone showed me a horse than can write an essay, and I'm complaining its penmanship is atrocious. It's pretty amazing stuff, and we're probably all going to be dead soon.

In the meantime it's pretty fun to mess about with.

PS: I do plan to update this one further, it just will require a bit of work each day given the rate limits. I had really grand plans, but we only got the bare minimum done.

Peace out!

Tagged , , , ,
leave comment

Hack-a-Day, Day 07: Pokédex

Today's hack-a-day project was the Pokédex -- the fictional companion that tells you about pokemon in the game. My main goal was just to get the info into a reasonable database format, but along with way I built a little viewer too.

The plan is to make some kind of art game where you do pokemon fanart, a coloring book, or even a tracing game in the coming days. And now I'm ready, with art of each pokemon on hand.

Tagged , , ,
leave comment

Hack-a-Day, Day 04: Reverse Vibe Coding

Yikes, been having some back pain, and the past few days it's been tougher to work. I've started four projects in four days, without too much to show for it.

  • Day 01 project is waiting on computation to run; overall I'm happy with it but will post when I get the results.
  • Day 02 project I barely started and won't finish, most likely. It takes a photo of a Go board and tries to output the game. I'd learn some image processing doing it, but I'm sure there's plenty of existing and better tools to do the same thing.
  • Day 03 project was a bit ambitious. Will post it if I finish (and hopefully I will, it's cool!)

Today's hack-a-day project was Reverse Vibe Coding. I sometimes use LLMs such as Claude for "vibe coding", mostly on throwaway type projects. It didn't seem fair for that to go only one way, so today I offered to vibe code for Claude -- it picks what I should make, and I code it up for Claude.

The result is the Conversation Flow Visualizer. This graphs when new topics come up in conversation, and what they are.

Frankly I think it's dumb and useless, but Claude is the boss, so there ya go! Can't pick who you work fo... okay, I guess I could this time.

In any case, it was pretty relaxing to be a junior dev and just do as I'm told for a bit, honestly. Easy win.

I honestly think this would be a good way to learn a new programming language or a new library.

Peace out!

Tagged , , ,
leave comment