# Rectangular pseudo-fractal

### Goal

The program's goal is to draw an ASCII art rectangle repeatedly doubled in size, alternating horizontally and vertically. Each time the rectangle doubles in size, the extra area is represented by a different character and the previous areas remain unchanged. The two smallest sections contain one character each and may be in any corner.

The program accepts a single integer as input, defining the number of sections the full rectangle contains.

No other external resources or inputs are allowed.

### Sample input and output

10

ABDDFFFFHHHHHHHHJJJJJJJJJJJJJJJJ
CCDDFFFFHHHHHHHHJJJJJJJJJJJJJJJJ
EEEEFFFFHHHHHHHHJJJJJJJJJJJJJJJJ
EEEEFFFFHHHHHHHHJJJJJJJJJJJJJJJJ
GGGGGGGGHHHHHHHHJJJJJJJJJJJJJJJJ
GGGGGGGGHHHHHHHHJJJJJJJJJJJJJJJJ
GGGGGGGGHHHHHHHHJJJJJJJJJJJJJJJJ
GGGGGGGGHHHHHHHHJJJJJJJJJJJJJJJJ
IIIIIIIIIIIIIIIIJJJJJJJJJJJJJJJJ
IIIIIIIIIIIIIIIIJJJJJJJJJJJJJJJJ
IIIIIIIIIIIIIIIIJJJJJJJJJJJJJJJJ
IIIIIIIIIIIIIIIIJJJJJJJJJJJJJJJJ
IIIIIIIIIIIIIIIIJJJJJJJJJJJJJJJJ
IIIIIIIIIIIIIIIIJJJJJJJJJJJJJJJJ
IIIIIIIIIIIIIIIIJJJJJJJJJJJJJJJJ
IIIIIIIIIIIIIIIIJJJJJJJJJJJJJJJJ


### Selection criteria

The shortest code in a week wins the points.

• What should the output be for non-powers of 4? Jan 31 '14 at 1:27
• Is the output required to be of the sequence A, B, C, etc.? Jan 31 '14 at 1:28
• @marinus What is special about powers of 4? Maybe you misunderstood the example? Jan 31 '14 at 1:28
• @KendallFrey, no, there just needs to be n unique printable characters. Jan 31 '14 at 2:48
• Does it have to recur exactly in the top left corner Jan 31 '14 at 5:01

# APL, 25 chars/bytes*

{⍉⍣⍵⊃{a,⍺⍴⍨⍴a←⍉⍪⍵}/⌽⍵↑⎕A}


Exploded view

{                   ⍵↑⎕A}   ⍝ take the first ⍵ letters
⊃{           }/⌽        ⍝ fold over them, using the first one as initial accum. value
a←⍉⍪⍵           ⍝    ensure the accum. is a table, transpose it and call it 'a'
⍺⍴⍨⍴                ⍝    make a table as large as 'a' filled with the next letter
a,                    ⍝    append it to the right of 'a' and loop as new accumulator
⍉⍣⍵                        ⍝ transpose the result as many times as the original ⍵ number


Examples

      {⍉⍣⍵⊃{a,⍺⍴⍨⍴a←⍉⍪⍵}/⌽⍵↑⎕A}¨⍳8
A AB  AB  ABDD  ABDD  ABDDFFFF  ABDDFFFF  ABDDFFFFHHHHHHHH
CC  CCDD  CCDD  CCDDFFFF  CCDDFFFF  CCDDFFFFHHHHHHHH
EEEE  EEEEFFFF  EEEEFFFF  EEEEFFFFHHHHHHHH
EEEE  EEEEFFFF  EEEEFFFF  EEEEFFFFHHHHHHHH
GGGGGGGG  GGGGGGGGHHHHHHHH
GGGGGGGG  GGGGGGGGHHHHHHHH
GGGGGGGG  GGGGGGGGHHHHHHHH
GGGGGGGG  GGGGGGGGHHHHHHHH


⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
*: APL can be written in its own (legacy) single-byte charset that maps APL symbols to the upper 128 byte values. Therefore, for the purpose of scoring, a program of N chars that only uses ASCII characters and APL symbols can be considered to be N bytes long.

##$$&&&& %%%%&&&& %%%%&&&& '''''''' '''''''' '''''''' ''''''''  The highest number that doesn’t make the program longer is 믰 (= 255), which gives us (n = 8 this time): Āāăăąąąąćććććććć ĂĂăăąąąąćććććććć ĄĄĄĄąąąąćććććććć ĄĄĄĄąąąąćććććććć ĆĆĆĆĆĆĆĆćććććććć ĆĆĆĆĆĆĆĆćććććććć ĆĆĆĆĆĆĆĆćććććććć ĆĆĆĆĆĆĆĆćććććććć  If we make the program 1 character longer, e.g. use 냟및 (= \u4DFF) and n = 9, we get: 一丁七七丅丅丅丅万万万万万万万万 丂丂七七丅丅丅丅万万万万万万万万 丄丄丄丄丅丅丅丅万万万万万万万万 丄丄丄丄丅丅丅丅万万万万万万万万 丆丆丆丆丆丆丆丆万万万万万万万万 丆丆丆丆丆丆丆丆万万万万万万万万 丆丆丆丆丆丆丆丆万万万万万万万万 丆丆丆丆丆丆丆丆万万万万万万万万 丈丈丈丈丈丈丈丈丈丈丈丈丈丈丈丈 丈丈丈丈丈丈丈丈丈丈丈丈丈丈丈丈 丈丈丈丈丈丈丈丈丈丈丈丈丈丈丈丈 丈丈丈丈丈丈丈丈丈丈丈丈丈丈丈丈 丈丈丈丈丈丈丈丈丈丈丈丈丈丈丈丈 丈丈丈丈丈丈丈丈丈丈丈丈丈丈丈丈 丈丈丈丈丈丈丈丈丈丈丈丈丈丈丈丈 丈丈丈丈丈丈丈丈丈丈丈丈丈丈丈丈  ## C#, 239185182 180 bytes C# has nothing on the less verbose languages. using C=System.Console; class P{ static void Main(string[]a){ for(int x,i,n=int.Parse(a[0]);n-->0;C.CursorTop=0) for(i=1<<n,x=1<<n/2+n%2;i-->0;) C.Write((char)(n+33)+(i%x<1?"\n":"")); } }  Output, characters chosen for prettiness: !"$$&&&&((((((((****************
##&&&&((((((((****************
%%%%&&&&((((((((****************
%%%%&&&&((((((((****************
''''''''((((((((****************
''''''''((((((((****************
''''''''((((((((****************
''''''''((((((((****************
))))))))))))))))****************
))))))))))))))))****************
))))))))))))))))****************
))))))))))))))))****************
))))))))))))))))****************
))))))))))))))))****************
))))))))))))))))****************
))))))))))))))))****************

• Not sure how you counted, but I counted 184. You can save two characters by ditching the braces from the outer for loop, making 182.
– Bob
Jan 31 '14 at 6:33
• Thanks @Bob! I must have miscounted while micro-optimizing. Feb 1 '14 at 11:44

# PERL, 122 chars

$N=<>;$x=$r=1;do{$_=chr$a+++65;$s=$x;$o=$_ x$s;$o.=$_++x$s,$s*=2while$N+65>ord++$_;print"$o\n"x$r;$r=$x;$x*=2}while++$a<$N  with added whitespace: $N=<>;
$x=$r=1;
do{
$_=chr$a+++65;
$s=$x;
$o=$_ x$s;$o.=$_++x$s,$s*=2 while$N+65>ord++$_; print "$o\n"x$r;$r=$x;$x*=2
} while++$a<$N


Output:

$echo 8 | perl pseudo-fractal.pl ABDDFFFFHHHHHHHH CCDDFFFFHHHHHHHH EEEEFFFFHHHHHHHH EEEEFFFFHHHHHHHH GGGGGGGGHHHHHHHH GGGGGGGGHHHHHHHH GGGGGGGGHHHHHHHH GGGGGGGGHHHHHHHH  # PERL, 94 81 chars $N=$_;$_=$:=A;$h=1;++$i%2?s/$/$:x$h/gem:($_.=($/.$:x2x$h)x$h,$h*=2)while++$:,--$N


It constructs the fractal iteratively letter by letter, adding new rows and columns and rows and columns... Uses simple string operations to do that. Note that I am abusing standard variable instead of letter one to allow for syntax sugar (like omitting spaces - $:x2 etc.) With added whitespace and comments: $N=$_;$_=$:=A; #$: is current letter
$h=1; ++$i%2?
s/$/$:x$h/gem: # every odd run - add "columns" ($_.=($/.$:x2x$h)x$h,$h*=2) # every even run - add "rows" while++$:,--$N # iterate over letters  Some output: $ echo 8 | perl -p pseudo-fractal.fill.pl.5a5
ABDDFFFFHHHHHHHH
CCDDFFFFHHHHHHHH
EEEEFFFFHHHHHHHH
EEEEFFFFHHHHHHHH
GGGGGGGGHHHHHHHH
GGGGGGGGHHHHHHHH
GGGGGGGGHHHHHHHH
GGGGGGGGHHHHHHHH


# Sclipting, 45 characters

가⓶貶上倘감雙⓶壹長⓸講增字⓶復⓷是標⓷各①合終并不⓶梴❸⓶疊合終不뀐標뀐并終終⓶丟各겠終


This solution works completely differently from the other Sclipting solution. It’s much more boring, but it’s shorter...

## Annotated

for i in [0..n-1]
가⓶貶上
if (i != 0)
倘
i &= 1
감雙
e = list[0].Length
⓶壹長
c = ((char) (c[0] + 1)).Repeat(e)
⓸講增字⓶復
if (i)
⓷是
concatenate c onto every element of list
標⓷各①合終并
else
不
concatenate c.Repeat(list.Length) onto list
⓶梴❸⓶疊合
終
else (i.e., i == 0)
不
c = "A"
뀐
list = ["A"]
標뀐并
終

concatenate "\n" to every element in list
⓶丟各겠終


### Without indent

var inp,j,i,x: integer;s:string;L:TStringlist;begin L:=TStringList.Create;readln(s);inp:=StrToIntDef(s,4);if inp<4then inp:=4;s:='';l.Add('AB');for I:=2to inp-1do begin j:=Length(L[0]);if i mod 2=0then for x:=0to L.Count-1do L.Add(s.PadLeft(j,Chr(65+i)))else for x:=0to L.Count-1do L[x]:=L[x]+s.PadLeft(j,Chr(65+i));end;Write(L.GetText);readln;end.


### With indent

var
inp,j,i,x: integer;
s:string;
L:TStringlist;
begin
L:=TStringList.Create;
inp:=StrToIntDef(s,4);
if inp<4then inp:=4;
s:='';

for I:=2to inp-1do
begin
j:=Length(L[0]);
if i mod 2=0then
else
for x:=0to L.Count-1do
end;
Write(L.GetText);
end.


# CJam, 30 (23) bytes

CJam is a few months younger than this challenge so it's not eligible for the green checkmark.

l~(Sa1\${{_,I'!+*+}%z}fI\{z}*N*


Test it here.

The OP clarified in a comment that any set of unique printable characters is allowed, so I'm just taking the printable ASCII characters from the start (with a space in the corner, ! next and so on).

If the orientation may change between even and odd inputs (which I don't think, but that's what the GolfScript submission does), I can do it in 25 bytes:

S]l~({{_,I'!+*+}%z}fIN*


The idea is really simple: start with a grid containing a space, and then N-1 times transpose it and double all lines with the next character.

For the long version, at the end I also transpose again N-1 times in order to guarantee a consistent orientation.

# Canvas, 22 bytes

A；╷｛┐⤢ｌ；Ｌ┘¹Ｚｊ＠┘┘＊＋｝╶［⤢


Try it here!