PATH: Count to Ten

This PATH code is a script that counts to 10. Actually it counts from 0 to 9, but it’s the same thing. A program that counts to 10 would be a lot bigger, because this one implements a loop that increments a memory cell (or “variable”) from 48 (ASCII for 0) to 57 (ASCII for 9).

Count to Ten:

/+++\\
\\++\\} /\\/\\
$+++\\+++++\\
/  \\/+++++/
v -{\\+++++\\
\\}+./+++++/
   +\\+++++\\
   \\++/\\/\\/

Looks like a hippo, doesn’t it? The code is quite simple, if you split it into three parts:

Part 1: Initiate countdown memory cell

/+++\\
\\++\\}
$+++\\
   \\/

This is where the code flow starts (look at the $). The only thing the first part of code does is incrementing the default memory cell to 9. It’s written like this to save space by using other parts of the code — it would work as well if it was written $+++++++++ and then directed to the right place. This memory cell will be used as a countdown in the third part, the loop.

Part 2: Count up to the ASCII numbers


    } /\\/\\
    \\+++++\\
    /+++++/
    \\+++++\\
    /+++++/
   +\\+++++\\
   \\++/\\/\\/

In the ASCII table, 0 is number 48. Therefore, this part of the code first changes to the next memory cell (}) and then increments it to 48. Now, both the memory cells are set and ready.

Part 3: Do the loop


/  \\
v -{\\
\\}+./
   +

The code starts on the plus in the bottom, and moves upwards. The first part is there only to get into the loop: First zero is printed with . (the current memory cell is at 48, which corresponds to the position of 0 in the ASCII table). Then, { changes memory cell to the one with the countdown value, and the loop starts.

In the loop, the following commands are given:

  • v — If the current memory cell (the countdown cell) is greater than zero, go down. If it isn’t, which means that the countdown is complete, the code flow will continue to the left and terminate.
  • } — Change to the ASCII number memory cell.
  • + — Increment the current memory cell (the ASCII number). This increases the number to be printed with one.
  • . — Print the ASCII value of the current memory cell. This is the number.
  • { — Change back to the countdown memory cell.
  • - — Decrement the current memory cell, i.e. the countdown cell. Note that if it reaches zero, the code flow will exit when it reaches the v.

The code is very simple as long as you understand it. If I looked back at this tomorrow, I’d probably mistake it for modern art. On a second thought, I already consider PATH art.

Maybe Related?

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

FireStats iconAnvänder FireStats