[Question]
  [
Write a rectangular block of text that when arranged into a [Sierpinski carpet](http://en.wikipedia.org/wiki/Sierpinski_carpet), using same-sized blocks of spaces for the empty portions, creates a program that outputs the iteration number of the carpet.
For example, if your text block is
```
TXT
BLK
```
then running the program
```
TXTTXTTXT
BLKBLKBLK
TXT   TXT
BLK   BLK
TXTTXTTXT
BLKBLKBLK
```
should output `1` because the shape of the program represents the first iteration of the Sierpinski carpet.
Similarly, running
```
TXTTXTTXTTXTTXTTXTTXTTXTTXT
BLKBLKBLKBLKBLKBLKBLKBLKBLK
TXT   TXTTXT   TXTTXT   TXT
BLK   BLKBLK   BLKBLK   BLK
TXTTXTTXTTXTTXTTXTTXTTXTTXT
BLKBLKBLKBLKBLKBLKBLKBLKBLK
TXTTXTTXT         TXTTXTTXT
BLKBLKBLK         BLKBLKBLK
TXT   TXT         TXT   TXT
BLK   BLK         BLK   BLK
TXTTXTTXT         TXTTXTTXT
BLKBLKBLK         BLKBLKBLK
TXTTXTTXTTXTTXTTXTTXTTXTTXT
BLKBLKBLKBLKBLKBLKBLKBLKBLK
TXT   TXTTXT   TXTTXT   TXT
BLK   BLKBLK   BLKBLK   BLK
TXTTXTTXTTXTTXTTXTTXTTXTTXT
BLKBLKBLKBLKBLKBLKBLKBLKBLK
```
should output 2 because this is the shape of the second Sierpinski carpet iteration.
Running the text block as is
```
TXT
BLK
```
should output `0` because it can be considered the zeroth iteration.
This should work for all further iterations. (At least theoretically, assuming the computer has the memory and all.)
# Details
* Programs may not read or access information about their source code. Treat this like a strict quine challenge.
* Output goes to stdout or similar alternative. Only output the number and an optional trailing newline. There is no input.
* The text block may contain any characters that are not considered [line terminators](http://en.wikipedia.org/wiki/Newline#Unicode). The text block may contain spaces.
* The "empty space" in the carpet must consist entirely of [space characters](http://www.fileformat.info/info/unicode/char/0020/index.htm).
* You may optionally assume all the programs have a trailing newline.
You can use this stack snippet to generate a carpet for a given text block at any iteration:
```
<style>#o,#i{font-family:monospace;}</style><script>function c(e){e=e.split("\n");for(var n=new Array(3*e.length),t=0;t<n.length;t++){var l=t%e.length;n[t]=e[l]+(t>=e.length&&t<2*e.length?e[l].replace(/./g," "):e[l])+e[l]}return n.join("\n")}function f(){for(i=document.getElementById("i").value,n=parseInt(document.getElementById("n").value);n>0;)i=c(i),n--;document.getElementById("o").value=i}</script><textarea id='i'placeholder='code block...'rows='8'cols='32'></textarea><br>Iterations <input id='n'type='text' value='1'><br><br><button type='button'onclick='f()'>Generate</button><br><br><textarea id='o'placeholder='output...'rows='8'cols='32'style='background-color:#eee'readonly></textarea>
```
# Scoring
The submission whose initial text block is smallest by area (width times height) is the winner. The `TXT\nBLK` example is 3 by 2 for a score of 6. (Basically the shortest code wins, hence the code-golf tag.)
Tiebreaker goes to the submission that uses the fewest distinct characters in their text block. If still tied, answer posted first wins.
  
[Answer]
# piet - 32\*6 = 192
![enter image description here](https://i.stack.imgur.com/nnkcv.png)
I filled the empty space with the checker pattern. I think it makes the Sierpinski a bit trippier.
Here is the second iteration:
![enter image description here](https://i.stack.imgur.com/G89bd.png)
## original : 32\*7
![enter image description here](https://i.stack.imgur.com/uVxUK.png)
[Answer]
# CJam, 9 bytes
I think this can be improved, but for now, lets go with it...
```
];U):U8mL
```
**How it works**:
```
];             "Wrap everything on stack in an array and discard it";
               "Before this point, the only thing on array can be the log 8 result of";
               "last updated value of U, or nothing, if its the first code";
  U):U         "Increment by 1 and update the value of U (which is pre initialized to 0)";
      8mL      "Take log base 8 of U. This is the property of Sierpinski carpet that";
               "the occurrence of the code is 8 to the power iteration count, indexed 0";
```
[Try it online here](http://cjam.aditsu.net/#code=%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%0A%5D%3BU)%3AU8mL%20%20%20%20%20%20%20%20%20%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%20%20%20%20%20%20%20%20%20%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%20%20%20%20%20%20%20%20%20%5D%3BU)%3AU8mL%0A%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%0A%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%0A%5D%3BU)%3AU8mL%20%20%20%20%20%20%20%20%20%5D%3BU)%3AU8mL%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%3BU)%3AU8mL%20%20%20%20%20%20%20%20%20%5D%3BU)%3AU8mL%0A%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%0A%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%0A%5D%3BU)%3AU8mL%20%20%20%20%20%20%20%20%20%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%20%20%20%20%20%20%20%20%20%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%20%20%20%20%20%20%20%20%20%5D%3BU)%3AU8mL%0A%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL%5D%3BU)%3AU8mL)
[Answer]
# [><>](http://esolangs.org/wiki/Fish), 11 \* 2 = 22
```
";n"00pbi1v
+$3*:@3-0.>
```
Here we take a different approach by using ><>'s jump/teleport functionality.
The program only executes blocks in the top row, running the 1st/2nd block, then the 3rd/4th blocks, 9th/10th blocks, 27th/28th blocks, etc. (going up in powers of 3). As the top row has `3^n` blocks, only `n` blocks are executed before the program wraps back to the start, outputs the top of the stack and halts (due to the `n` instruction placed via `p`).
The program exploits the rule "There is no input.", as the `i` command pushes -1 onto the stack if EOF is met. So to test this you'll need to pipe in an empty file.
---
## Previous submission, 7 \* 4 = 28
```
l"v"10p
v>:1=?v
3  ;n{<
<^}+1{,
```
The first line continuously pushes the length of the stack for every block, and changes the first `"` quote to a down arrow `v` using the `p` put command. By the time the first line is over, the stack looks like
```
[0, 1, 2, .., 3^n]
```
(Note that the initial `l` is used twice.)
The last three lines then count how many times we need to divide by 3 before we hit 1 (since ><> doesn't have a log function). The bottom zero is used to keep track of the count.
[Answer]
# Perl, 26
```
$_+=.91/++$n;
die int."\n";
```
This uses the harmonic series to approximate the base 3 logarithm. I think it works, but I've only tried it for small numbers. Thanks to squeamish ossifrage for the idea of using `die`.
Old version (34):
```
$n--or$n=3**$s++;
print$s-1if!$o++;
```
[Answer]
# Perl, 30 (15×2)
First of all, I'm going to claim that 10 iterations is a reasonable limit, not 232. After 10 iterations, a program consisting of *N* bytes will have expanded to (*N* × 320) bytes (plus line breaks), which is over 3 gigabytes even for *N*=1. A 32-bit architecture would be completely unable to handle 11 iterations. (And obviously there aren't enough particles in the universe for 232 iterations).
So here's my solution:
```
$n++; $_=log$n;
print int;exit;
```
This works by incrementing the variable `$n` in the first line and calculating its logarithm at each step. The second line prints the integer part of this logarithm and quits.
A simple logarithm to base *e* (2.718..) is close enough to give correct results for the first 10 iterations.
[Answer]
# Golfscript, 9 \* 2 = 18
```
0+       
,3base,(}
```
*(Note that the first line has trailing spaces to make it rectangular)*
I couldn't find a log function for Golfscript, so `base` had to do.
Golfscript starts off with an empty string, so `0+` just increases the length of the string by 1 (by coersion). By the time the first line is over, the stack will have a string of length `3^n`, which we take the log base 3 of before we super comment. `n` is then automatically printed.
[Answer]
# **C, 12x8 = 96**
Inspired by @ciamej, I've reduced it. It uses that divide by 3 trick, plus the realization that the carpet effectively converts an if into a while loop.
The code was tested on gcc/Ubuntu for iterations up to 3.
```
#ifndef A //
#define A //
x;main(a){//
a++;/*    */
if(a/=3)x++;
printf(   //
"%d",x);} //
#endif    //
```
---
*Previous Solution: **C, 11x12***
Not a size winner, but hey, it's C.
It finds log2 of the blockcount by bitshifting, then uses some magic numbers and int truncation to estimate log3. The math should work up to 26 iterations (a 42 bit number).
```
#ifndef A//
#define A//
int n=0;//_
int main//_
(v,c){//___
n+=1;/*..*/
while(n//__
>>=1)v++;//
n=.3+.62*v;
printf(//__
"%d",n);}//
#endif//__
```
[Answer]
# CJam, 9 bytes
The idea of using `]` is from Optimizer, but it uses a very different method to count.
```
X~]:X,8mL
```
[Try it online](http://cjam.aditsu.net/#code=X~%5D%3AX%2C8mL)
How it works:
```
X~          "push X and dump its contents.  On the zeroth iteration, X is a single number, but later is it an array.";
  ]         "wrap everything into an array.  The stack would contain the contents of X plus the result of the previous instance of the code";
   :X       "store this array back into X.  X is now 1 element longer";
     ,      "take the length of X";
      8mL   "do a base-8 logarithm of it";
```
## Two other 9 byte solutions
```
]X+:X,8mL
],X+:X8mL
```
[Answer]
# Python 2, 15 \* 3 = 45
```
m=n=0;E=exit  ;
m+=1;n+=m>3**n;
print n;E()   ;
```
Another implementation of the count-first-row-then-log-three-and-exit idea. Can probably still be golfed a fair bit more.
[Answer]
# ><> (Fish), 12 \* 3 = 36
A more straightforward ><> solution:
```
'v'00p0l1+  
>  :2-?v$1+v
^$+1$,3< ;n<
```
We first run the top row of the top blocks. `'v'00p` puts `v` at the very first position of the whole program directing the program pointer downwards when it gets back to the start after reaching the end of the line. Before that every block pushes 0 and the length of the stack + 1 onto it. (stack will be `0 2 0 4 0 6 ...`)
On the first half of the second and third we count how many times we can divide the top stack element before we get 2 (we store this in the second to top element).
At the end we output the second to top element of the stack.
[Answer]
# bc, 2\*16+1 = 33
The extra +1 in the score is because the `-l` bc option is required:
```
a+=1;          
l(a)/l(3);halt;
```
[Answer]
# Golfscript, 7 \* 2 = 14
```
1+~abs(
3base,}
```
This is inspired by Sp3000's [answer](https://codegolf.stackexchange.com/a/47065/194), and in particular by the desire to optimise the long second line. `3base,` is as short as a base-3 logarithm will get in GS, and the super-comment `}` is clearly optimal.
What's required for the first line is to map the empty string `''` from the initial stdin to 0, and then to map each non-negative integer to its successor. In this way we end the first line with `3^n - 1` on the stack, and `3base,` doesn't require any decrement.
[Answer]
# C, 13x8
```
#ifndef A//__
#define A//__
x;a;main(){//
a++;;;;;;;;;;
while(a/=3)//
x++;printf(//
"%d",x);}//__
#endif//_____
```
[Answer]
# Perl, 76
I know there's probably not much point in posting this since it's already been thoroughly beaten, but here's my current solution anyways.
```
$_++;                                 
if(not$_&$_-1){print log()/log 8;$_--}
```
[Answer]
# Lua, 3 \* 17 = 51
Same strategy as most people:
```
x=(x or 0)+1;    
y=math.log(x,3)  
print(y)os.exit()
```
[Answer]
# PHP, 22×2 = 44 27×2 = 54
```
<?php $i++          ?>
<?php die(log($i,3))?>
```
Just another take on count-log3-out. Not very small, but my first golf ;)
]