Piet
Factoid
Piet is a programming language where the source code consists of images. Program flow starts with the top-left pixel and moves around the image between pixels and pixel groups until it terminates.
For legibility, Piet programs are commonly displayed in an enlarged version. In such a case the term codel
is used to describe a group of same-coloured pixels that correspond to an individual pixel in the source image.
For this challenge, since Piet does not use characters, one codel per vote will be used for sample programs.
1 Codel
This is a valid program, it does nothing and terminates. The control flow starts in the top-left (only) pixel and has no way out, which ends the program.
The pixel can in this case be any colour for the exact same effect.
2 Codels
This will continually read characters from stdin and keep a running total of their unicode values (though nothing is done with this total and it is not displayed).
Progam flow moves back and forth between the 2 pixels, since the only way out of each one is into the other. Commands in piet are executed by movement from one codel or region into another, depending on the difference in hue and lightness of the 2 regions. The input is the command moving left-to-right and then the add is right-to-left. On the first "add" command, nothing will happen since there is only one value on the stack, and the specification says that commands without enough values available are ignored.
This program is a loop that will never end, as most piet programs will be at extremely small sizes, since it takes at least a few codels to properly "trap" the program flow and end it.
3 Codels
This is a basic echo-type program, it will read a character at a time from stdin and print it to stdout.
Again this is an infinite loop. The program starts by travelling left-to right, which does the input then output. The program will continue to flow in the same direction whenever it can. At the light green codel the only exit is to start moving back the other way. When travelling back left-to-right it attempts to perform subtract and add commands, but the stack is empty so these become no-ops.
4 Codels
Prints out 2 to stdout indefinitely.
Not a particularly interesting program functionally, but now that we finally have a composite number of codels we can show off slightly more advanced flow than left-to-right. When program flow attempts to exit a codel it first tries the current direction. If it's unable (in this case due to the edge of the image) it rotates 90 degrees clockwise and attempts to exit again. In this case, the program goes around clockwise 1 codel at a time, pushing 1 onto the stack twice, adding the ones together, then outputting the result.
5 Codels
Repeatedly reads a character at a time from stdin and tracks the sum of their unicode values.
This is essentially the same functionality as the 2-codel version, but this challenge is about showcasing the language, and one of the cool things about piet is how you can have different-looking pictures that do the same thing.
Here we see the white codel for the first time, which allows program flow to slide across it without executing instructions. The magenta and blue codels do all the work here. Travelling from blue to red does nothing because it crosses the white codel in the middle. The 2 red ones just push the number 1 onto the stack and pop it back off as it travels left-to-right then right-to-left across them, and then across the white codel so no instruction is executed.
6 Codels
Again, repeating earlier functionality with a different look. This is another echo program that reads a character at a time from stdin to stdout.
Here we see our first black codel. Program flow cannot enter a black codel, so from the light magenta codel in the top-right the program will fail to exit right due to the image edge, fail to exit down due to the black codel, and bounce back left into the red codel. The blue and green codels are purely decorative, the program will never enter them.
7 Codels
Yet another echo program with a different look.
Here we see our first codel blocks larger than size 1. In piet, any contiguous block of codels of the same colour is treated as a single block. The size of the block does not matter except when executing the "push" instruction, so this program is treated exactly like a 3-codel version with these same colours.
8 Codels
Reads a number from stdin and outputs the square to stdout, repeatedly.
Control flow is a basic clockwise pattern just as in the 4-codel program. Starting from the top-left, the operations in order are input, duplicate (pushes an extra copy of the top value of the stack onto the stack), multiply, output. Then it pushes the value 1 onto the stack, slides across the white so no command is executed, and then pops that 1 off of the stack when moving from the lower-left to upper-left codel. This returns it to the beginning of the program with an empty stack, and it repeats.
9 Codels
Adds 1 + 2 = 3, and then terminates.
Now that we have a program with greater than 2 codels in both dimensions, we can finally set up a region that will trap the program and end it instead of looping forever. The first operation moving from the red codel to the dark red region is a push of 1, then the program rotates and flows down into the light red codel in the middle and pushes a value of 2. Flowing from the light red to the light yellow executes an add operation. The bottom light yellow bar causes the program to end since there is no way for it to flow out as all the corners are blocked off.
The 1- and 2-high programs are quickly becoming ugly and uninteresting so from this point on I'm going to focus on numbers that allow at least a few codels in each direction.
12 Codels
Finally a program that does something that could be argued as useful (though it's still a bit of a stretch). Reads 2 numbers from stdin sequentially and then outputs their sum, and does this repeatedly.
Program flows left-to-right across the 4 coloured bars perfoming 2 reads followed by an add command. It then moves into the lower-right codel and performs an output, and then goes left across the white region back to the start.
This could have been done in 8 codels, but since we have the extra space we can make something that's a little bit inspired by an old no-signal TV display.
15 Codels
Reads a number from stdin and outputs its' square.
This uses a couple of tricks to get a bit of a symmetrical look to a program that actually does something. The leftmost red bar is a different colour on the bottom codel than the rest, taking advantage of the fact that (for me at least) these 2 shades of red look very similar. the program will move from the lighter red region right into the light blue codel, and then straight across the middle of the program to the light green on the right side where it is trapped. It performs read, duplicate, multiply, and output operations.
The darker red codel, along with the medium green codels on the top and bottom of the middle column, are decorative and the program will never reach them.
20 Codels
Reads numbers from stdin until a 0 is read, at which point it outputs the sum of all entered numbers and exits.
We finally have enough room to do control flow in the form of the pointer operation. The 4 codels along the top perform input
, duplicate
, and not
operations, and then another not
operation moving from the magenta in the top-right to the 2-codel yellow below it. The not
operation pops the top value off of the stack and pushes a 1 if the top value was a 0, and a 1 otherwise. Therefore a double-not
replaces any nonzero value with a 1. Moving from the yellow bar down to the dark blue performs a pointer
operation, which pops the top value off of the stack and moves the direction pointer clockwise that many times.
If the top value is a 1 (i.e. we didn't enter a zero) the direction pointer will point left, moving to the magenta codels for an add operation (which will be ignored the first time due to only one value on the stack) and then through the white back to the start of the program.
If the top value of the stack is a zero at the pointer operation, the direction pointer will not change and the program will continue downwards. Moving into the lighter blue band will pop the 0 that was entered off of the stack, leaving only the sum of the accumulated numbers. Moving into the cyan bar on the bottom will output that sum, and then end since the program flow is trapped.
25 Codels
Countdown! Reads a number from stdin, and then prints a countdown to 1 to stdout one number at a time. For example, if 5 is read, will print 54321.
The first operation from cyan to yellow is the input. Then the yellow is where the program "loop" starts. Yellow > Magenta > Blue is a duplicate then an output, so it prints the top value on the stack but keeps a copy. Moving down the right side, we push the value 1 onto the stack then perform a subtraction, decreasing our entered value by 1. Next is duplicate, not, and another not moving from the light magenta in the bottom-right to the dark yellow beside it. This is the same zero/nonzero check as the previous program. Moving left into the light blue codel performs a pointer operation, that will either move left into the dark cyan to end the program if we're done, or up to the yellow to re-start our loop without the initial input but the original value decreased by 1.
All 3 of the red codels are decorative and could be any colour.