{"id":203,"date":"2006-11-03T15:22:06","date_gmt":"2006-11-03T15:22:06","guid":{"rendered":"http:\/\/scientopia.org\/blogs\/goodmath\/2006\/11\/03\/programming-in-color-fixed\/"},"modified":"2006-11-03T15:22:06","modified_gmt":"2006-11-03T15:22:06","slug":"programming-in-color-fixed","status":"publish","type":"post","link":"http:\/\/www.goodmath.org\/blog\/2006\/11\/03\/programming-in-color-fixed\/","title":{"rendered":"Programming in Color (fixed)"},"content":{"rendered":"<p>Todays programming pathology is programs as art.<br \/>\nStart with a really simple stack based language, add in a crazy way of encoding instructions using color, and you end up with a masterpiece of beautiful insanity. It&#8217;s not too exciting from a purely computational point of view, but the programs are really great to look at. Yes, it&#8217;s a pathological language with truly beautiful source code!<br \/>\n*(The original version of this post had some trouble because I linked to the original images in-place,<br \/>\nwhich the owner of the Piet webpage had blocked. I didn&#8217;t realize he didn&#8217;t want links. I&#8217;ve since downloaded<br \/>\nthe images, coverted them to jpegs, and posted them here. I initially thought that the problem with the images was formats, which is what I originally said in this explanation. It&#8217;s not the image format, but the linking; but converting the files to jpeg and uploading them removed the links that caused the problem.)*<\/p>\n<p><!--more--><br \/>\nHere&#8217;s the complete list of<br \/>\ninstructions (without control flow):<br \/>\n1. **Push**: Push a value onto the stack.<br \/>\n2. **Pop**: discard the top value from the stack.<br \/>\n3. **Add**: Take the top two values off of the stack, add them, and push the result.<br \/>\n4. **Subtract**: like add, but subtract the values.<br \/>\n5. **Multiply**: like add, but multiply.<br \/>\n6. **Divide**: like add, but divide.<br \/>\n7. **Mod**: like add, by modulo.<br \/>\n8. **Not**: pop the top value off the stack; if it&#8217;s 0, push 1, otherwise push 0.<br \/>\n9. **Greater**: pop the top two values off the stack, push 1 if the second value on the<br \/>\nstack is greater than the top, otherwise, push 0.<br \/>\n10. **Roll**: pop the top two values of the stack. Call the top one &#8220;count&#8221;, and the second<br \/>\none &#8220;depth&#8221;. Now, take the top &#8220;depth&#8221; set of values on the stack, and rotate them by taking<br \/>\nthe value on top, and putting it on the bottom. Do the rotation count times.<br \/>\n11. **In**: read a value from input and push it on the stack.<br \/>\n12. **Out**: pop a value from the stack and print it.<br \/>\nSo far, seems pretty trivial, right? Standard stack based language, nothing unusual.<br \/>\nOk. So, now let&#8217;s start getting weird. We&#8217;re looking at a two dimensional language, so the control flow operators are all based on *direction*. There are two things that control the flow of the program: the direction of motion, and the direction of selection. The direction of motion describes where the pointer goes on a step; the direction of selection determines where it looks if it needs more information than it has under the instruction pointer. So the control flow operations are:<br \/>\n1. **Switch**: pop the top stack value *n*, and switch the selection direction *n* times.<br \/>\n2. **Rotate**: pop the top stack value *n*, and rotate the motion direction 90 degrees clockwise<br \/>\n*n* times.<br \/>\nOk, getting a bit weird now&#8230; But still, overall, pretty mundane. This isn&#8217;t a particularly<br \/>\ninteresting model of computation, nothing exciting about it. Certainly nothing do beat, say,<br \/>\nSnusp for the beauty of 2d programming.<br \/>\nNow&#8230; Here&#8217;s where it gets *really* interesting.  How are instructions encoded? By *color*. The program source is a PNG image!<br \/>\nYes, todays language is called &#8220;[Piet][piet]&#8221;, in honor of Piet Mondrian, a modern abstract artist, because programs are intended to look as much like a painting by Mondrian as possible. There are twenty colors in Piet that have semantic significance, given in the table below:<\/p>\n<table border=\"1\" cellpadding=\"3\" cellspacing=\"0\" align=\"right\">\n<tr>\n<td bgcolor=\"#FFC0C0\" align=\"center\">#FFC0C0<br \/>light red<\/td>\n<td bgcolor=\"#FFFFC0\" align=\"center\">#FFFFC0<br \/>light yellow<\/td>\n<td bgcolor=\"#C0FFC0\" align=\"center\">#C0FFC0<br \/>light green<\/td>\n<td bgcolor=\"#C0FFFF\" align=\"center\">#C0FFFF<br \/>light cyan<\/td>\n<td bgcolor=\"#C0C0FF\" align=\"center\">#C0C0FF<br \/>light blue<\/td>\n<td bgcolor=\"#FFC0FF\" align=\"center\">#FFC0FF<br \/>light magenta<\/td>\n<\/tr>\n<tr>\n<td bgcolor=\"#FF0000\" align=\"center\">#FF0000<br \/>red<\/td>\n<td bgcolor=\"#FFFF00\" align=\"center\">#FFFF00<br \/>yellow<\/td>\n<td bgcolor=\"#00FF00\" align=\"center\">#00FF00<br \/>green<\/td>\n<td bgcolor=\"#00FFFF\" align=\"center\">#00FFFF<br \/>cyan<\/td>\n<td bgcolor=\"#0000FF\" align=\"center\">#0000FF<br \/>blue<\/td>\n<td bgcolor=\"#FF00FF\" align=\"center\">#FF00FF<br \/>magenta<\/td>\n<\/tr>\n<tr>\n<td bgcolor=\"#C00000\" align=\"center\">#C00000<br \/>dark red<\/td>\n<td bgcolor=\"#C0C000\" align=\"center\">#C0C000<br \/>dark yellow<\/td>\n<td bgcolor=\"#00C000\" align=\"center\">#00C000<br \/>dark green<\/td>\n<td bgcolor=\"#00C0C0\" align=\"center\">#00C0C0<br \/>dark cyan<\/td>\n<td bgcolor=\"#0000C0\" align=\"center\">#0000C0<br \/>dark blue<\/td>\n<td bgcolor=\"#C000C0\" align=\"center\">#C000C0<br \/>dark magenta<\/td>\n<\/tr>\n<tr>\n<td bgcolor=\"#FFFFFF\" align=\"center\" colspan=\"3\">#FFFFFF&nbsp;white<\/td>\n<td bgcolor=\"#000000\" align=\"center\" colspan=\"3\"><font color=\"#FFFFFF\">#000000&nbsp;black<\/font><\/td>\n<\/tr>\n<\/table>\n<p>Most commands are given by the color transition between the pixel under the instruction pointer, and the pixel next to it in the selection direction. You&#8217;ll definitely need to keep that table handy to see how the instructions work.<br \/>\nSo, let&#8217;s look at how we encode instructions. We take the current pixel, and the pixel next to it in the selection direction, and consider their *difference* in the two dimensions given in the color table: hue, and darkness:<br \/>\n* **Add**: +1 hue; no change in lightness.<br \/>\n* **Divide**: +2 hue; no change in lightness.<br \/>\n* **Greater**: +3 hue; no change in lightness.<br \/>\n* **Dup**: +4 hue; no change in lightness.<br \/>\n* **In** (input character): +5 hue; no change in lightness.<br \/>\n* **Push**: No change in hue, +1 darkness.<br \/>\n* **Subtract**: +1 hue, +1 darkness.<br \/>\n* **Mod**: +2 hue, +1 darkness.<br \/>\n* **Rotate**: +3 hue, +1 darkness.<br \/>\n* **Roll**: +4 hue, +1 darkness.<br \/>\n* **Out** (output number): +5 hue, +1 darkness.<br \/>\n* **Pop**: No change in hue, +2 darkness.<br \/>\n* **Multiply**: +1 hue, +2 darkness.<br \/>\n* **Not**: +2 hue, +2 darkness.<br \/>\n* **Switch**: +3 hue, +2 darkness.<br \/>\n* **In** (input number): +4 hue, +2 darkness.<br \/>\n* **Out** (output character): +5 hue, +2 darkness.<br \/>\nNumbers in Piet come from the *size* of the color blocks. The number of pixels in the block correspond to a number. So, for example, a block of 6 light yellow pixels followed by a yellow pixel will push the number 6 onto a stack.<br \/>\nBlack and white are special colors &#8211; white is a separator; white pixels are just skipped over as if they weren&#8217;t there. Black pixels are blocks; if the program hits a black pixel, it will try switching the selection directions (ignoring white pixels); if that is also black, it will try rotating the motion direction +90 degrees. If it can&#8217;t find any non-black pixels using an sequence of alternating SWITCH\/ROTATE instructions, the program will halt.<br \/>\nSo now, finally, you can look at a few nifty examples of [Piet programs][programs]. To make them easier to read, the Piet programs are scaled up, so the pixels are actually represented by largish squares. As usual, we&#8217;ll start with &#8220;Hello World&#8221;:<br \/>\n<img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"Piet_hello_big.jpg\" src=\"https:\/\/i0.wp.com\/scientopia.org\/img-archive\/goodmath\/img_97.jpg?resize=150%2C145\" width=\"150\" height=\"145\" \/><br \/>\nThat *is* hello world. Each of the large color blocks is one of the characters; the narrow lines are stack manipulations and output statements; and the little black arrowhead toward the top left captures the pointer and forces the program to halt.<br \/>\nMoving on, here&#8217;s a much fancier &#8220;Hello world&#8221;:<br \/>\n<img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"hw3-5.jpg\" src=\"https:\/\/i0.wp.com\/scientopia.org\/img-archive\/goodmath\/img_98.jpg?resize=415%2C125\" width=\"415\" height=\"125\" \/><br \/>\nOne that really meets the goal of looking like a Mondrian painting, this program prints<br \/>\nout &#8220;Piet&#8221;:<br \/>\n<img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"Piet-4.jpg\" src=\"https:\/\/i0.wp.com\/scientopia.org\/img-archive\/goodmath\/img_99.jpg?resize=168%2C168\" width=\"168\" height=\"168\" \/><br \/>\nMy personal favorite Piet program; this one prints out the alphabet; and the author provided<br \/>\nan [annotated version][annote] of it to help you understand how it works! Here&#8217;s the program:<br \/>\n<img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"alpha_filled_big.jpg\" src=\"https:\/\/i0.wp.com\/scientopia.org\/img-archive\/goodmath\/img_100.jpg?resize=270%2C72\" width=\"270\" height=\"72\" \/><br \/>\nOne last brilliant Piet program, which even the guy who *designed* Piet can&#8217;t figure out&#8230; It&#8217;s the towers of Hanoi in Piet!<br \/>\n<img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"hanoibig.jpg\" src=\"https:\/\/i0.wp.com\/scientopia.org\/img-archive\/goodmath\/img_101.jpg?resize=216%2C126\" width=\"216\" height=\"126\" \/><br \/>\n[annote]: http:\/\/www.dangermouse.net\/esoteric\/piet\/alpha_filled_trace.png<br \/>\n[programs]: http:\/\/www.dangermouse.net\/esoteric\/piet\/samples.html<br \/>\n[piet]: http:\/\/www.dangermouse.net\/esoteric\/piet.html<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Todays programming pathology is programs as art. Start with a really simple stack based language, add in a crazy way of encoding instructions using color, and you end up with a masterpiece of beautiful insanity. It&#8217;s not too exciting from a purely computational point of view, but the programs are really great to look at. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[92],"tags":[],"class_list":["post-203","post","type-post","status-publish","format-standard","hentry","category-pathological-programming"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4lzZS-3h","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/203","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/comments?post=203"}],"version-history":[{"count":0,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/203\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/media?parent=203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/categories?post=203"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/tags?post=203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}