[Piet](http://www.dangermouse.net/esoteric/piet.html) = **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** ![1 Codel][1] 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** ![2 Codels][2] 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 codels, 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** ![3 Codels][3] 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** ![4 Codels][4] 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, `push`ing 1 onto the stack twice, `add`ing the ones together, then `output`ing the result. **5 Codels** ![5 Codels][5] 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** ![6 Codels][6] 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** ![7 Codels][7] 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 the 3-codel version, except with different colours. **8 Codels** ![8 Codels][8] 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 `push`es the value 1 onto the stack, slides across the white so no command is executed, and then `pop`s 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** ![9 Codels][9] 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 `push`es 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** ![12 Codels][10] 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 `inputs` 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** ![15 Codels][11] 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 `input`, `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** ![20 Codels][12] 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** ![25 Codels][13] 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. **30 Codels** ![30 Codels][14] Fibonacci generator. Prints out terms of the Fibonacci sequence to stdout and doesn't stop. This is the first introduction of the `roll` operator, as well as the first time that a region size bigger than 1 is used with the `push` operator to get a specific value onto the stack. As always starts in the top-left moving right. The first 2 operations `push` a 1 onto the stack and then `output` it since the Fibonacci sequence starts with two 1s, but the main program loop will only print 1 once. Then it `push`es 2 more 1s onto the stack to end up in the dark magenta in the top-right to start the main program loop. Moving down the right side we `duplicate` and `output` to print off the next term of the sequence, then `duplicate` again to get a copy of the current sequence value. Moving left across the bottom executes 2 `push` operations. Since the light red region in the bottom-right is 3 codels in size, the first `push` will push a 3 onto the stack instead of a 1. Moving up into the light blue is a `roll` operation. This pops the top 2 values off of the stack and performs a number of rolls equal to the first value popped, to a depth equal to the second value popped. In this case, it will perform 1 roll to a depth of 3. A roll to depth `n` takes the top value of the stack (our duplicated current value) and buries it `n` places deep. Our stack is only 3 deep right now so it will bury the top value at the bottom. Moving up once more performs an `add` operation adding together the current sequence value with the previous sequence value. Our stack now has the next (new current) sequence value on top, and the last value below it. The program now moves right across the white into the dark magenta to start the loop again. The yellow pattern in the middle is never used. [1]: https://i.sstatic.net/eB4XY.png [2]: https://i.sstatic.net/lRG1h.png [3]: https://i.sstatic.net/rFVzV.png [4]: https://i.sstatic.net/m3XR2.png [5]: https://i.sstatic.net/nYWRF.png [6]: https://i.sstatic.net/e8y5T.png [7]: https://i.sstatic.net/EnEqR.png [8]: https://i.sstatic.net/V9FYs.png [9]: https://i.sstatic.net/yKnUm.png [10]: https://i.sstatic.net/uGZR7.png [11]: https://i.sstatic.net/OjV6N.png [12]: https://i.sstatic.net/IiXez.png [13]: https://i.sstatic.net/BQKEu.png [14]: https://i.sstatic.net/gVE3q.png