DEV Community

Yusuke Endoh
Yusuke Endoh

Posted on

A Quine in Piet (and a plug for the esolang design contest)

I made a quine in Piet, an esoteric programming language.

The Piet quine (2026 edition)

Piet is an esolang that treats an image as a program: a cursor runs around the pixels of the image, and the changes in hue and lightness it crosses are the instructions it executes. See the official page for the details.

So this GIF file is a program, and running it prints this very GIF file.

The video

Here is a video that visualizes the run: which cell is being executed, and how far the output has been printed.

How to run it

Feed the image to npiet.

$ npiet quine.piet.gif > out.bin
$ diff -s quine.piet.gif out.bin
Files quine.piet.gif and out.bin are identical
Enter fullscreen mode Exit fullscreen mode

The image and the generator script live in this repository.

Structure

Roughly speaking, the image splits into a "data part", which takes up most of the top, and a "code part" at the bottom.

The data part and the code part

The data part

The "data part" is an instruction sequence that runs in a vertical zigzag from the top left to the bottom right, pushing data as it goes.

The data part: piling the data onto the stack

Once that run is over, all the data needed is on the stack. The data is the pixels of the "code part", compressed with an adaptive run-length encoding.

The code part

The run through the "code part" spirals clockwise.

First it runs from the bottom right to the bottom left, emitting the GIF file header. The loop along the way computes the palette table.

Code part (1): emitting the GIF header and the palette table

Then it goes around a big loop at the top left. This one looks at the data on the stack and emits the pixels of the "data part" area.

Code part (2): drawing the data part

Next it goes around a big loop at the top right. This one likewise looks at the data on the stack and emits the pixels of the "code part" area.

Code part (3): drawing the code part

Finally it emits the trailing data of the GIF file and stops. The dot of "Y. Endoh" is the terminator that halts the execution.

Code part (4): emitting the end of the GIF file and halting

Restoring the 2009 edition

I originally made a Piet quine back in 2009. Here is the article from back then (in Japanese):

Piet Quine - まめめも

npiet 1.0b には出力部分にバグがあったので動きませんでしたが、パッチ書いて報告したら直してもらえました。npiet 1.1 ならこのように。 $ ./npiet-1.1/npiet quine.gif > quine2.gif $ diff quine.gif quine2.gif

favicon mametter.hatenablog.com

But the image file itself had gone missing, for various reasons (and of course the generator script with it). So remaking the Piet quine stayed on my mind and I attempted it several times, but the motivation barrier to redoing a quine I had already made was pretty high, and Piet's sheer painfulness won every time.

Then it hit me: surely a modern AI coding agent can bring it back! The result was that I burned through my weekly limit on Claude Code's Fable and got nowhere. It even started lying to me: "Final conclusion: this is impossible in principle."

While telling it "that can't be right, I actually made one", I remembered that I had once given a talk about the Piet quine, and pointed it at that. Claude Code then restored the Piet quine of that era from the image in that PDF and got it running again. Ah, so that was the way in.

Here is what came back.

The Piet quine (2009 edition)

Very long.

Building the 2026 edition

Once something was running again, analyzing it and reconstructing the Ruby script that generates it was easy work for Claude Code.

By the way, I had always thought the 2009 Piet quine was too tall to look good, so I had Claude Code build a landscape one.

That turned out to be surprisingly hard.
The first step was refactoring: I had it design a DSL for laying out Piet and restructure the generator around it. The DSLs it came up with were genuinely bad, but after a lot of back and forth we got there.

Next came the folded data part. Claude Code could not build this on its own at all, and did not understand it even when I designed it and gave instructions; this took the longest to get working.

This is what we ended up with at that point.

The first working 2026 edition (400 x 295)

That is pointlessly huge and ugly, so the next job was shrinking it, which means golfing -- and Claude Code's golfing skills were just as bad. When I coached it with something like "you can cut instructions if you compute it in this order", it called me a genius, which did wonders for my self-esteem.

One thing led to another, and we got it down to my target of a 4:3 landscape image (252 x 189 pixels), so I am calling it done for now. 16:9 is still a way off.

Thoughts on Piet and AI coding agents

As of 2026, it seems fair to say that AI coding agents cannot draw Piet properly on their own.

That said, it is also a fact that this rewrite would not have happened without Claude Code. Anyone who has drawn Piet will know that it is not a maintainable language. Change one number -- the height of the image, say -- and it collides with the rest of the code and breaks, so you end up re-laying pixels by hand. That was exactly the wall that had kept me from redoing the Piet quine, and Claude Code does that tedious work without complaining, which was unambiguously great.

Anyway, it was fun to touch Piet again after all these years. It really is designed with an exquisite balance.

A plug for the esolang design contest

If this article made you curious about esolangs, I have something to tell you.

The "2026 Obfuscated Programming Language Design Contest" is running right now.

Design an esoteric programming language and submit it. I am one of the judges too. I am waiting for the most fun esolangs you can come up with.

The deadline is October 1, two months from now. Please do!

Top comments (0)