Piet


Piet Program Gallery

These are sample programs for the Piet programming language I invented.

Hello World

This program prints "Hello world!" and then exits. It is shown in two sizes: 1 pixel per codel, and 25 pixels per codel. Program flow proceeds clockwise from the upper left red block along the edge of the program until the dark blue block at lower left is reached. It then proceeds up to the single light blue codel, right to the dark cyan codel, and right into the large green block at centre. From there it flows left through the dark green codel to the yellow block, then up through the dark yellow codel to the red block inside the black codels, where execution halts. This produces the required ASCII codes in a brute force manner, with each of the large blocks coding one of the required characters, but there are a few stack tricks in there to prevent having to redefine the same character more than once. (Thanks to Matt Rudary for debugging!)

Fibonacci Numbers

This program prints the first 100 Fibonacci numbers. It is shown at 1 pixel per codel, and twice at 121 pixels per codel. The second copy of the large version is overlaid with a black line to show program flow, beginning at the upper left. The flow sets up initial stack entries and prints the first two numbers before entering a loop at the dark blue codel in the centre of the left edge. The loop executes clockwise, rejoining itself at the dark blue codel. The branch in the long green block at the bottom is the test to see whether the 100th number has been printed. If it has, the code branches up to the red block and terminates there. Note that the yellow->dark blue transition on both entry points to the loop are "pointer" commands defining rotations of the DP, necessary to get the DP pointing in the right direction to trace the loop. Non-significant codels are left white to emphasise readability of the code, but in practice these could be filled with other colours for greater artistic effect. The only significant white codel is the slide from dark blue to yellow just before rejoining the loop, required to get the yellow to dark blue transition for the "pointer" command.

Towers of Hanoi

This program solves the Towers of Hanoi problem. It was written by Sylvain Tintillier. Don't ask me how it works, I have no idea!

Fancy Hello World

To show you how different two programs that do the same thing can look, this is another version of "Hello World!" written by Matt Rudary. I'm not sure how it works, but it looks a lot more elegant than my version.

Prime Number Test

Alexander Rodgers writes:
There are two versions, but the first is merely the "better-looking" one, with unimportant squares filled in which colours instead of black. The effect of the program is to take in a number, and output either "PRIME" or "NOT PRIME" depending on which it is. The first part finds the whole number lower than the input's square root (a surprising amount of work in Piet) to save on runtime. Then, the program goes input-modulus-(2, 3, etc) to see if it has any factors. If it does, it goes through that big light magenta square to write out NOT, then into the dark cyan square. If none of the numbers below the square root are factors, it enters the dark cyan square. From there the program writes PRIME. It's strange that the text section takes up a lot more room than the calculation section.


Artistic Hello World

Thomas Schoch demonstrates that programs written in Piet really can be artistic. He calls this version of "Hello, world!" his masterpiece, and provides a detailed explanation of its principles, both artistic and programmatic.

Artistic Hello World 2

Thomas Schoch provides another implementation of "Hello, world!" He writes:
This is similar code, but without the DP manipulation, since the black color block enforces program flow. Also included is an animated GIF consisting of 3 runnable variations of the static version. Note that the animated GIF can be run by the Piet interpreter! (Of course, I prefer the animated version.)


Artistic Hello World 3 and 4

Thomas Schoch writes again:
Program flow of these is obviously simple. They were made just to demonstrate how decorative Piet programs can be.



Composition with Red, Yellow and Blue by Piet Mondrian
Composition with Red,
Yellow and Blue
.
1921, Piet Mondrian.

"Piet"

Thomas Schoch just can't stop coding in Piet!
This was made with the goal of having a Piet program that really looks like a painting of Piet Mondrian. It prints "Piet".


Alpha

Chad Etzel was so inspired by learning Piet to attack Perplex City's puzzle card #234 that he's started coding in it. Here's a program that prints out the letters of the alphabet. He's also supplied a full trace of the code.
I basically had it push 1, multiply a bunch of times (so as not to affect the stack) around the big blue block to get back to the beginning of the loop. That's where the majoriy of the steps come from. I could have had way fewer instructions by having only a few pushes/pops, but I figured I would make it pretty :-). It is a graphic language after all... I encourage everyone to try to write a Piet program, it's wonderfully mindbending.


Prime Number Generator

Sylvain Tintillier provides a method of generating prime numbers using Piet. Figuring out how it works is easy, he says, "Just look at the bitmap!"

Adder

Mike Tolly supplies a simple adding program, which adds together two input numbers.

Pi

Richard Mitton supplies this amazing program which calculates an approximation of pi... literally by dividing a circular area by the radius twice. Richard says:
The output is printed without the decimal point after the 3.

Naturally, a more accurate value can be obtained by using a bigger program.




Home | Esoteric Programming Languages | Piet
Last updated: Friday, 25 January, 2008; 03:26:58 PST.
Copyright © 1990-2007, David Morgan-Mar. dmm@dangermouse.net
Hosted by: DreamHost