title: Output Graham's Number

text:
# Question
Title: Output Graham's Number
Write a program that outputs all 64 layers of this representation of Graham's number from Wikipedia:

Your output **must exactly match** this ASCII version (in which `^` represents Knuth's up-arrows and combinations of slashes and underscores represent the large horizontal curly brackets):

```
3^^...............................................................................................................................^3
 \_______________________________________________________________  _______________________________________________________________/
                                                                 \/
 3^^.............................................................................................................................^3
  \______________________________________________________________  ______________________________________________________________/
                                                                 \/
  3^^...........................................................................................................................^3
   \_____________________________________________________________  _____________________________________________________________/
                                                                 \/
   3^^.........................................................................................................................^3
    \____________________________________________________________  ____________________________________________________________/
                                                                 \/
    3^^.......................................................................................................................^3
     \___________________________________________________________  ___________________________________________________________/
                                                                 \/
     3^^.....................................................................................................................^3
      \__________________________________________________________  __________________________________________________________/
                                                                 \/
      3^^...................................................................................................................^3
       \_________________________________________________________  _________________________________________________________/
                                                                 \/
       3^^.................................................................................................................^3
        \________________________________________________________  ________________________________________________________/
                                                                 \/
        3^^...............................................................................................................^3
         \_______________________________________________________  _______________________________________________________/
                                                                 \/
         3^^.............................................................................................................^3
          \______________________________________________________  ______________________________________________________/
                                                                 \/
          3^^...........................................................................................................^3
           \_____________________________________________________  _____________________________________________________/
                                                                 \/
           3^^.........................................................................................................^3
            \____________________________________________________  ____________________________________________________/
                                                                 \/
            3^^.......................................................................................................^3
             \___________________________________________________  ___________________________________________________/
                                                                 \/
             3^^.....................................................................................................^3
              \__________________________________________________  __________________________________________________/
                                                                 \/
              3^^...................................................................................................^3
               \_________________________________________________  _________________________________________________/
                                                                 \/
               3^^.................................................................................................^3
                \________________________________________________  ________________________________________________/
                                                                 \/
                3^^...............................................................................................^3
                 \_______________________________________________  _______________________________________________/
                                                                 \/
                 3^^.............................................................................................^3
                  \______________________________________________  ______________________________________________/
                                                                 \/
                  3^^...........................................................................................^3
                   \_____________________________________________  _____________________________________________/
                                                                 \/
                   3^^.........................................................................................^3
                    \____________________________________________  ____________________________________________/
                                                                 \/
                    3^^.......................................................................................^3
                     \___________________________________________  ___________________________________________/
                                                                 \/
                     3^^.....................................................................................^3
                      \__________________________________________  __________________________________________/
                                                                 \/
                      3^^...................................................................................^3
                       \_________________________________________  _________________________________________/
                                                                 \/
                       3^^.................................................................................^3
                        \________________________________________  ________________________________________/
                                                                 \/
                        3^^...............................................................................^3
                         \_______________________________________  _______________________________________/
                                                                 \/
                         3^^.............................................................................^3
                          \______________________________________  ______________________________________/
                                                                 \/
                          3^^...........................................................................^3
                           \_____________________________________  _____________________________________/
                                                                 \/
                           3^^.........................................................................^3
                            \____________________________________  ____________________________________/
                                                                 \/
                            3^^.......................................................................^3
                             \___________________________________  ___________________________________/
                                                                 \/
                             3^^.....................................................................^3
                              \__________________________________  __________________________________/
                                                                 \/
                              3^^...................................................................^3
                               \_________________________________  _________________________________/
                                                                 \/
                               3^^.................................................................^3
                                \________________________________  ________________________________/
                                                                 \/
                                3^^...............................................................^3
                                 \_______________________________  _______________________________/
                                                                 \/
                                 3^^.............................................................^3
                                  \______________________________  ______________________________/
                                                                 \/
                                  3^^...........................................................^3
                                   \_____________________________  _____________________________/
                                                                 \/
                                   3^^.........................................................^3
                                    \____________________________  ____________________________/
                                                                 \/
                                    3^^.......................................................^3
                                     \___________________________  ___________________________/
                                                                 \/
                                     3^^.....................................................^3
                                      \__________________________  __________________________/
                                                                 \/
                                      3^^...................................................^3
                                       \_________________________  _________________________/
                                                                 \/
                                       3^^.................................................^3
                                        \________________________  ________________________/
                                                                 \/
                                        3^^...............................................^3
                                         \_______________________  _______________________/
                                                                 \/
                                         3^^.............................................^3
                                          \______________________  ______________________/
                                                                 \/
                                          3^^...........................................^3
                                           \_____________________  _____________________/
                                                                 \/
                                           3^^.........................................^3
                                            \____________________  ____________________/
                                                                 \/
                                            3^^.......................................^3
                                             \___________________  ___________________/
                                                                 \/
                                             3^^.....................................^3
                                              \__________________  __________________/
                                                                 \/
                                              3^^...................................^3
                                               \_________________  _________________/
                                                                 \/
                                               3^^.................................^3
                                                \________________  ________________/
                                                                 \/
                                                3^^...............................^3
                                                 \_______________  _______________/
                                                                 \/
                                                 3^^.............................^3
                                                  \______________  ______________/
                                                                 \/
                                                  3^^...........................^3
                                                   \_____________  _____________/
                                                                 \/
                                                   3^^.........................^3
                                                    \____________  ____________/
                                                                 \/
                                                    3^^.......................^3
                                                     \___________  ___________/
                                                                 \/
                                                     3^^.....................^3
                                                      \__________  __________/
                                                                 \/
                                                      3^^...................^3
                                                       \_________  _________/
                                                                 \/
                                                       3^^.................^3
                                                        \________  ________/
                                                                 \/
                                                        3^^...............^3
                                                         \_______  _______/
                                                                 \/
                                                         3^^.............^3
                                                          \______  ______/
                                                                 \/
                                                          3^^...........^3
                                                           \_____  _____/
                                                                 \/
                                                           3^^.........^3
                                                            \____  ____/
                                                                 \/
                                                            3^^.......^3
                                                             \___  ___/
                                                                 \/
                                                             3^^.....^3
                                                              \__  __/
                                                                 \/
                                                              3^^...^3
                                                               \_  _/
                                                                 \/
                                                               3^^^^3

```

Notice that there are 64 layers, that there are no trailing spaces, and that two `^` occur before the dots and only one occurs after.

The MD5 hash of your output should precisely be `c84d0e20eac2a7f891ab42a8eb77759f`, or `1ad3b10d06e38693436c14958b2c47a8` if there is a trailing newline (which is ok).

The shortest code in bytes wins. Output to stdout. There is no input.

# Answer
> 4 votes
# CJam - 64

```
63{SI*3'^_'._63I-:J*_'^3NSI)*'\'_J*_S_@'/NS65*"\/
"}fIS63*3'^4*3

```

Try it at http://cjam.aditsu.net/

# Answer
> 10 votes
## Python, 122

```
i=63
while i:
 for s in'3^^'+'..'*i+'.^3','\%s  %s/'%(('_'*i,)*2),'\/':print' '*(66-len(s)/2)+s
 i-=1
print' '*63+'3^^^^3'

```

Example:

```
$ python grahams.py | md5
1ad3b10d06e38693436c14958b2c47a8

```

# Answer
> 8 votes
# Perl -- 113 (112 + 1 for -E)

---

Golfed:

```
$.=$"x63,$y="_"x($!=63-$_),$x.=($z=$"x$_)."3^^".".."x$!.".^3$/ $z\\$y  $y/$/ $. \\/$/"for 0..62;say"$x$.3^^^^3"

```

Ungolfed:

```
for(0..62) {
    $.=$"x63;
    $!=63-$_;
    $y="_"x$_;
    $z=$"x$!;
    $x.=$z."3^^".".."x$!.".^3$/ $z\\$y  $y/$/ $. \\/$/";
}
say"$x$.3^^^^3"

```

~~EDIT: There was an error in string interpolation where perl thought $m3 was a variable, so I had to add 2 characters to fix that.~~ Solved by changing $m to $.

NINJA EDIT: Replaced instances of " " with $". Saves 2 characters

NINJA EDIT 2: Thanks @chilemagic for pointing out that I can use say instead of print with -E to save a character.

NOT-SO-NINJA EDIT 3: @Nax's solution beat my old solution, so I buffed it up a little to hold its ground.

# Answer
> 7 votes
# Haskell, 152

```
(?)=replicate
g 0=["3^^^^3"]
g i=("3^^"++(i*2)?'.'++".^3"):map(' ':)(("\\"++i?'_'++"  "++i?'_'++"/"):((i+1)?' '++"\\/"):g(i-1))
main=putStr.unlines.g$63

```

# Answer
> 4 votes
# C 161 182

(i think i maxed out this method, maybe i should find a new one that can be golfed down more)
compile as C99!

```
main(i){for(char d[]={[0 ...126]=46,i=0},l[]={[0 ...62]=95,0};i<63;)printf("3^^%s^3\n%*s\\%s  %s/\n%*s\n%*s",d+2*i++,i,"",l+i,l+i,67,"\\/",i,"");puts("3^^^^3");}

```

# JS 158

```
r="repeat";o="";u="_";s=" ";(f=i=>i?"3^^"+"."[r](i*2+1)+"^3\n"+s[r](64-i)+"\\"+u[r](i)+s[r](2)+u[r](i)+"/\n"+s[r](65)+"\\/\n"+s[r](64-i)+f(i-1):"3^^^^3")(63);

```

xem.github.io/obfuscatweet/ **124:**

```
eval(unescape(escape('').replace(/uD./g,'')))

```

# Answer
> 3 votes
# Bash+coreutils, 150

I thought I might be able to do better than this. Oh well.

```
r()(yes "${2:- }"|head -$[$1]|tr -d '
')
for i in {63..1};{
r 63-$i
echo "3^^`r 2*$i .`.^3
`r 64-$i`\\`r $i _`  `r $i _`/
`r 65`\/"
}
r 63
echo 3^^^^3

```

### Output:

```
$ ./graham.sh | md5sum
1ad3b10d06e38693436c14958b2c47a8  -
$

```

# Answer
> 1 votes
## STATA 135

```
forv x=63(-1)1{
di _d(63-`x')" ""3^^"_d(2*`x'+1)".""^3"_n_d(64-`x')" ""\"_d(`x')"_""  "_d(`x')"_"_n-d(65)" ""\/"
}
di _d(63)"3^^^^3"

```

# Answer
> 1 votes
# JavaScript 179

```
n='';a=Array;for(b=64;b>1;)n+=(c=a(65-b).join(' '))+'3^^'+a(2*b).join('.')+'^3\n'+c+' \\'+(u=a(b--).join('_'))+'  '+u+'/\n'+(d=a(64).join(' '))+'  \\/\n'
console.log(n+d+'3^^^^3')

```

Test it in your browser console or at http://jsfiddle.net/2qwvrvcw/.

# Answer
> 1 votes
# Javascript (203)

```
u="_";s=" ";for(i=64;i>1;i--)console.log(s.repeat(64-i)+"3^^"+".".repeat(i*2-1)+"^3\n"+s.repeat(64-i)+"\\"+u.repeat(i)+s.repeat(2)+u.repeat(i)+"/\n"+s.repeat(65)+"\\/");console.log(s.repeat(63)+"3^^^^3")

```

Ungolfed:

```
underscore = "_";
space = " ";
for (i = 64; i > 1; i--) console.log(space.repeat(64 - i) + "3^^" + ".".repeat(i * 2 - 1) + "^3\n" +
                                     space.repeat(64-i) + "\\" + underscore.repeat(i) + space.repeat(2) + underscore.repeat(i) + "/\n" +
                                     space.repeat(65) + "\\/");
console.log(space.repeat(63) + "3^^^^3");

```

# Answer
> 1 votes
## C# (~~212~~211)

```
using C=System.Console;class P{static void Main(){for(int i=64;i>1;)C.Write(@"{0}3^^{2}.^3
{0} \{1}  {1}/
{3,65}\/
",new string(' ',64-i),new string('_',--i),new string('.',i*2),"");C.Write("{0,69}","3^^^^3");}}

```

.Net Fiddle (it seems to require that the class and Main method are public)

Slightly ungolfed:

```
using C = System.Console;
class Program {
  static void Main() {
    for (int i = 64; i > 1; ) {
      C.Write("{0}3^^{2}.^3\n{0} \\{1}  {1}/\n{3,65}\\/\n",
 new string(' ', 64 - i),
 new string('_', --i),
 new string('.', i * 2), "");
    }
    C.Write("{0,69}", "3^^^^3");
  }
}

```

# Answer
> 1 votes
# Perl, 116 112 109 108 (including -E)

My first attempt at golfing. Please take it as such:

Golfed:

```
perl -E'map{$_=" $_!^".(($b=63-$_)?"2.$b.$b.^!
 $_ \\_$b 2_$b/
 65\\/":"4!");s#(.)(\d+)#"$1"x$2#eg;y/!/3/;say}0..63'

```

Very slightly ungolfed (not maintained ... ):

```
map
{
    $b = 63 - $_;
    $_ = " $_!^".($b
        ? "2.$b.$b.^!N $_ \\_$b 2_$b/N 65\\/"
        : "4!");
    s#(.)(\d+)#"$1"x$2#eg;    # behaves exactly as $_ =~ s/(.)(\d+)/"$1"x$2/eg
    y/!N/3\n/;                  # y == tr, so this does $_ =~ tr/!N/3\n/
    say;                        # once more $_ completion, this does say $_ == print "$_\n"
} (0..63);

```

Essentially it works like this: for each line of 3s and a brace (these are handled together), it writes their sequence *RLE-encoded*, then decodes it. The rule is simple: every char followed by a number is expanded to that char that number of times. Characters without a number are left alone. Finally, the character `!` denotes a 3 and a `N` means a newline (3 had to be encoded because it is number and would eagerly participate as the number of repetitions).

Each line of 3s and its brace get an element in the 64-element array the map is generating. Say, like print, can output arrays -- if given an array, it prints all the elements (without any separators).

I used chilemagic's trick with `-E`. Thanks for it.

**Reinforcements:** Two 113 byte solutions started to lurk here. RLE shall not succumb without fight!

**Cleansing of Feeds**: I have now realized that I have no need for a special transliteration of \n, because all strings in Perl are automagically multiple-lined! Thus RLE clings nearer to the Ruby's back! Now at 109 bytes.

**Agonizing Enlightment**: To count characters properly is harder than I thought. After some struggling, I settled on

```
echo $((wc -c <<<'golf'-1))
```
(-1, because it counts a LF at the very end of the script). Moreover I have rearranged my assignments and saved 1 character, hereby settling on 108 bytes.

# Answer
> 0 votes
# PHP 214

Thought I was doing alright until I counted the characters, anyway here it is -

```
<?function a($n,$v=" "){for($j=0;$j<$n;$j++){$r.=$v;}return $r;}for($i=64;$i>1;$i--){$b=a($i-1,"_");$v.=a(64-$i)."3^^".a($i*2-1,".")."^3\n".a(64-$i+1)."\\".$b.a(2).$b."/\n".a(65)."\/\n";}$v.=a(63)."3^^^^3";echo $v;

```

# Answer
> 0 votes
**MATLAB (325)**

```
m=127:-2:0;n=0:63;p='UniformOutput';q=false;f=@ones;g=@arrayfun;a=g(@(x,y)[f(1,y)*' '  '3^^' f(1,x)*'.'  '^3'],m,n,p,q);b=g(@(x,y)[f(1,y+1)*' '  '\' f(1,(x-1)/2)*'_' '  ' f(1,(x-1)/2)*'_' '/'],m,n,p,q);a{64}(a{64}=='.')='^';g(@(x,y)fprintf('%s\n%s\n%s\n',x{:},y{:},[f(1,65)*' ' '\/']),a,b),fprintf(repmat('\b',1,137))

```

ungolfed:

```
m=127:-2:0;
n=0:63;
p='UniformOutput';
q=false;
f=@ones;
g=@arrayfun;
a=g(@(x,y)[f(1,y)*' '  '3^^' f(1,x)*'.'  '^3'],m,n,p,q);
b=g(@(x,y)[f(1,y+1)*' '  '\' f(1,(x-1)/2)*'_' '  ' f(1,(x-1)/2)*'_' '/'],m,n,p,q);
a{64}(a{64}=='.')='^';
g(@(x,y)fprintf('%s\n%s\n%s\n',x{:},y{:},[f(1,65)*' ' '\/']),a,b),fprintf(repmat('\b',1,137))

```

**MATLAB (252)**

```
n=0;f=@ones;g=@arrayfun;for i=127:-2:0 if n~=63 y=f(1,i)*'.';else y='^';z='';end;fprintf('%s\n%s\n%s\n',[f(1,n)*' '  '3^^' y  '^3'],[f(1,n+1)*' '  '\' f(1,(i-1)/2)*'_' '  ' f(1,(i-1)/2)*'_' '/'],[f(1,65)*' ' '\/']);n=n+1;end;fprintf(repmat('\b',1,137))

```

ungolfed:

```
n=0;
f=@ones;
g=@arrayfun;
for i=127:-2:0
    if n~=63
        y=f(1,i)*'.';
    else y='^';
        z='';
    end
    fprintf('%s\n%s\n%s\n',[f(1,n)*' '  '3^^' y  '^3'],[f(1,n+1)*' '  '\' f(1,(i-1)/2)*'_' '  ' f(1,(i-1)/2)*'_' '/'],[f(1,65)*' ' '\/']);
    n=n+1;
end
fprintf(repmat('\b',1,137))

```

# Answer
> 0 votes
# Lua - 174

```
p=print a=" "b="_"for i=0,62 do j=63-i p(a:rep(i).."3^^."..(".."):rep(j).."^3")p(a:rep(i+1).."\\"..b:rep(j).."  "..b:rep(j).."/")p(a:rep(65).."\\/")end p(a:rep(63).."3^^^^3")

```

---

Before that, I got two algorithmically different solutions (both 182 !) :

```
a=" "b="_"for i=0,62 do j=63-i print(a:rep(i).."3^^."..(".."):rep(j).."^3")print(a:rep(i+1).."\\"..b:rep(j).."  "..b:rep(j).."/")print(a:rep(65).."\\/")end print(a:rep(63).."3^^^^3")

```

#

```
a=" "b="_"i=63 while i>0 do c=b:rep(i/2)for _,s in pairs{"3^^."..(".."):rep(i/2).."^3","\\"..c.."  "..c.."/","\\/"}do print(a:rep(66-#s/2)..s)i=i-1 end end print(a:rep(63).."3^^^^3")

```

---
Tags: code-golf, kolmogorov-complexity
---


title: How many trees in the Alphabet Forest?

text:
# Question
Title: How many trees in the Alphabet Forest?
Inspired by the connection rules from Calvin's Hobbies' question Make me an alphabet tree take a number of lines of letters as input and output the number of distinct connected trees it represents.

Letters connect to diagonally adjacent letters (never horizontally or vertically) and only where such a connection is available. Available connections correspond to the end of a line in the shape of the letter. For example, X can potentially connect at all four corners as they are all the ends of lines. W can only connect at the top left and top right corners, as the bottom left and bottom right corners are not ends of lines.

## Example 1

Input:

```
XXX
XXX
XXX

```

Output:

```
2

```

The two trees (which do not form part of the output):

```
X   X             X
 \ /             / \
  X             X   X
 / \             \ /
X   X             X

```

## Example 2

Input:

```
AMF
QWK

```

Output:

```
3

```

The three trees (which do not form part of the output):

```
A   F                M
 \ /                  \
  W         Q          K

```

## Rules

1. The only valid characters are spaces, newlines, and the 23 upper case letters listed in Calvin's Hobbies' question: ACEFGHIJKLMNPQRSTUVWXYZ.
2. The letters B, D and O are not valid.
3. The output is undefined if the input contains invalid characters.
4. A tree can be of any size, including just 1 letter and including over 23 letters.
5. The same letter can appear an arbitrary number of times in a single tree.
6. The input will not be more than 79 lines long.
7. Each line (excluding newlines) will not be more than 79 characters long.
8. The lines will not necessarily be the same length as each other.
9. A tree is defined to be a set of connected letters. Since the connections are all diagonal this excludes letters which are horizontally or vertically adjacent from being part of the same tree.
10. Input containing zero trees (for example, all spaces and newlines) gives output 0.
11. To clarify, input and output are as defined on meta (so there is not already a variable containing the input).

This is code golf. The shortest correct code wins. Code length is measured in bytes.

If you have python 3.4, the alphabet tree counter outputs the number of connected trees in its input, and can be used to test your golfed code gives the same results. The alphabet tree checker confirms whether its input is a valid output for Calvin's Hobbies' original question, and outputs a list of the distinct trees in the input. This may be useful for visualising the trees if there is any dispute over the number of trees in a given input (although using it on large forests will give a lot of output - a visualisation of every single tree separately).

If the either of these is incorrect please let me know so I can fix it.

## Table of connections.

1 is for upper left, 2 is for upper right, 3 lower left, 4 lower right.

```
A: 3 4
C: 2 4
E: 2 4
F: 2 3
G: 2
H: 1 2 3 4
I: 1 2 3 4
J: 1 3
K: 1 2 3 4
L: 1 4
M: 3 4
N: 2 3
P: 3
Q: 4
R: 3 4
S: 2 3
T: 1 2
U: 1 2
V: 1 2
W: 1 2
X: 1 2 3 4
Y: 1 2
Z: 1 4

```

## Sample forest

Here is an example of a forest to test your code with. It has the maximum size of 79 x 79 and no spaces. The alphabet tree counter gives an output of 714. I don't recommend running the alphabet tree ***checker*** on it as it will try to output a separate visualisation of every single one of the 714 trees.

```
CKZNXXJEHCNHICKFHNAFZTMNXZKXSXAFHEKKFJYUZPCHRSNAUPEZKIHZJSKKPHQJYHKMXSHCXKEHCXQ
YSRCHHQLXXKKXITAAHSKMXISHIGFXMVXTZZKLXIXVHJYKTAHSHYPCFLHSWIHHCXXRXXJNKXXVVRNSKM
CZWJKKNIXXUHKXCKRTXXSVHQZERHXIXNHXPKELNKHXRHFKHIEPKUWKXKUPHEKKIKLLSFWUSLYEEXLSI
KHGURVKSXXKAGXIWKCSHFNVWWHNHNIVYKKAKZKFSKHCVRHUENHFIIKNUNICH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ZKRLALIEVHXYKHXIFYKXMAINSNIASMGLLYHXHRIXVWUKZHFREJCAXKXWYHXNUKTQCUKIHLWKTEKMC
MHLXXHHYQWHTXVHCMKKHGIUFSEHNEVYSXQHHXXKHMXUXQKHZHHHSHRKWHMMEHZMIKKYYWIUNEIJUIKT
NCHKRNSJYLLIXKXAIERXKKXIQZVHXFVIIAXHIYUGZURXFHSXKLIWESKHEXQSCUHWKALZIVEZHAFKKWA
IKCJWNVRKMITKNYXJYKYXVXXUKWHXXJAKLXXXVKXGXKKZXMHCKKCGRLZQINVHRLNVWXTKVLTEPTXUXH
XVCICKXXXEVTIKIHHSHCKYIJXNCJKJSLSHVXJHTHSIKUXPHWRIKMFXXMMHKHCAQWITIEWEXPFHYXXKU
XXSHHIXHWWRKITSHSXHJKEWHXIZHSHZCHFARIXNPHYSNIKJXLQENKFCXYRWMXAPIYTZKNMXFHNIMCCS
PAACXJMNVZZHMXXSNVSXEIINNCHLSTIXHHKYXLEHQHKHPRWIXXHRKVCXFKLHSHKGXJGKLLLWSHIISKT
WUYXTJHXXMKJCYIXQJRNFKYHHUUKTIAYXVUUXFCYLHXWWZAUHWZIUAIGKHKIMKXXXKJZXCKKRCJAKQJ
EVAGXIHTINSXENKWVXWCLFUVHXFPFCIFVHWXRIFZRYZKUUEMXNGAJLIFHJJIGUXFKKRIMKGFKXJYXKV
KKRHHWKIYXWXWKLWXWKFHXLXRXRKKENMHHIQRHTTMFYTGRLSHRXIXKKUEHXXJKRIKSYLTKYJMNIUWVE

```

# Answer
> 4 votes
### GolfScript, 150 characters

I thought a GolfScript implementation is missing in this competition. While it is not a particularly fitting challenge for GolfScript it can be done quite nicely.

```
n%{.,' '80*>+}%n*:^,,{.80+'AFJMNPRS'\'CEFGNSTUVWY'3$'ACELMQRZ'3$2+'JLTUVWYZ'}%4/{2/.{~'HIKX'+\^[=]&},=},{~@;;]}%.,{:A{A{1$&},{|}/$}%.&}*^'
 '-,\{,-)}/

```

Note that edc65\`s testcase takes only a few seconds but the huge forests may run for several minutes, depending on your computer. Although the algorithm itself is not extremely slow it is only slightly optimised for speed instead of size. It copies huge lists of trees several times which is a costly operation in GolfScript.

I am still trying to find a shorter representation of the encodings for the letters' connections. Also you can save 16 characters if you can guarantee that all lines have the same length.

# Answer
> 8 votes
## Ruby - 228

My very first Ruby golf. Still new to Ruby. Ruby is awesome language! Love learning it! =)
But I'm afraid will probably lose. =(

```
g=$<.each.map{|k|k.ljust 99}*''<<?@*1e5
p (0..1e4).map(&f=->k,a=!0{l=(0..3).map{|i|k+~0**i+99*~0**(i/2)if"xhv8gstx930ognx8puyr".to_i(36)>>(i+4*g[k].ord-260)&1>0};(g[k]=?@;l.map{|v|v&&f[v,k]})if(l&[a]!=[])|!a&l.any?}).count{|x|x}

```

To run, create a file with the forest as `forest.txt` and run using `ruby test.rb forest.txt`

**GoRuby Edition - 188**

```
g=A.e.m{|k|k.lj 99}.j.lj 1e5
p 9999.mp(&f=->k,a=N{l=4.mp{|i|k+~0**i+99*~0**(i/2)if"xhv8gstx930ognx8puyr".toi(36)>>(i+4*g.g(k)-260)&1>0}.cp;(g[k]=?@;l.m{|v|f[v,k]})if(l.i a)|!a&l.an}).cp.sz

```

Ok... So my fear came true. @Howard (I'd actually expected him) actually made a GS solution.

# Answer
> 6 votes
# JavaScript (E6) 235 ~~238 240 276 284 291 294 319~~

A function with a string parameter (the forest), returning the tree count. This seems ok with the rules in meta about input/ouput (point 2).

This scans the forest numbering the branches (var u) and keeping a running count of joinined branches (var j now). Branches that have been joined are remembered in a set, so that two joined branches are recognizable even if they have different numbers.

**Edit** Slightly changed the algorithm, now it manages the forest string as a char array avoiding the split into lines. Simplified the branch joining - slower but shorter. Bonus: at the end the r array can be used as a map to extract any of the n trees from the forest. I'll elaborate.

~~Probably a little more golfable, but not so much as the answer in Ruby. I'll try.~~

**Edit** Ruby *and* goRuby passed. In the meantime @bitpwner has probably become a Ruby guru and he'll cut away half of his ruby code with his next edit ...

Here is a jsfiddle with less golfed code and the graphical output of tree connections.

```
C=t=>(r=['\n',...t]).map((e,i)=>
  e<' '?(a=b,p=i-h,b=[]):
   (k=(2&a[h=b.push(n='0x'+'30909a8ff6f53a0213accccfc5'[e.charCodeAt()-65]|0)]|1&a[h-2])&n/4,
   x=r[p-1],y=r[++p],k>2&&x-y?r=r.map(v=>v-x?v:y,++j):0,
   r[i]=k>1?y:k?x:n&&++u),j=u=h=b=0)|u-j

```

**Test**

```
C("AMF\nQWK")

```

Result: 3

*A new testcase*

```
CKZNXXJEHCNHICKFHNAFZTMNXZKXSXAFHEKKFJYU
YSRCHHQLXXKKXITAAHSKMXISHIGFXMVXTZZKLXIX
QZWJKKNIXXUHKXCKRTXXSVHQZERHXIXNHXPKELNW
 HGUR                              KZKF
 GAFR                              RZHX
 EXXXC                            THXRR
  LXHI                            UAJH
  KNFX                            NJAK
  TWWHXUTXGKXFKZUTHHZPJGMAEHZVGHLEXHKY
   XHRHKKXHHVXYZMNHIYHKKXXKAVWKUKEXYG
   AXXKHHNKKMKPRFKKURCWLFXWIXUGTKKXHK
   USHXK                        VNHZI
    LMKY                        MUHQ
    JTKL                        KNXX
    PVCKP                      XEPJA
     QIQX                      KYKJ
     IJIIHEXKIYGKHKIALIXKRHRXYXRAVH
     SXSNXRHIUXHTXWYRHXHSXZKKKXIKHF
      KIVHCKYXKXMTXELJXNCKWYHKXVHF
      HXNM                    WWHM
      GKFKE                  KSVJH
       KXXP                  WHXW
       UKHX                  RKUN
       JLFKX                EFKFK
        XFAAMXHXUFITLAWNMJYHTCWQ
        XPJRXIVVFTJKQFNSKMKFKIHM
        KYELCKHLXXHXHTVMJHNFQUFJ

C("CKZNXXJEHCNHICKFHNAFZTMNXZKXSXAFHEKKFJYU\nYSRCHHQLXXKKXITAAHSKMXISHIGFXMVXTZZKLXIX\nQZWJKKNIXXUHKXCKRTXXSVHQZERHXIXNHXPKELNW\n"
+" HGUR                              KZKF\n GAFR                              RZHX\n EXXXC                            THXRR\n"
+"  LXHI                            UAJH\n  KNFX                            NJAK\n  TWWHXUTXGKXFKZUTHHZPJGMAEHZVGHLEXHKY\n"
+"   XHRHKKXHHVXYZMNHIYHKKXXKAVWKUKEXYG\n   AXXKHHNKKMKPRFKKURCWLFXWIXUGTKKXHK\n   USHXK                        VNHZI\n"
+"    LMKY                        MUHQ\n    JTKL                        KNXX\n    PVCKP                      XEPJA\n"
+"     QIQX                      KYKJ\n     IJIIHEXKIYGKHKIALIXKRHRXYXRAVH\n     SXSNXRHIUXHTXWYRHXHSXZKKKXIKHF\n"
+"      KIVHCKYXKXMTXELJXNCKWYHKXVHF\n      HXNM                    WWHM\n      GKFKE                  KSVJH\n"
+"       KXXP                  WHXW\n       UKHX                  RKUN\n       JLFKX                EFKFK\n"
+"        XFAAMXHXUFITLAWNMJYHTCWQ\n        XPJRXIVVFTJKQFNSKMKFKIHM\n        KYELCKHLXXHXHTVMJHNFQUFJ")

```

Result: 149

*The huge forest*

```
C("CKZNXXJEHCNHICKFHNAFZTMNXZKXSXAFHEKKFJYUZPCHRSNAUPEZKIHZJSKKPHQJYHKMXSHCXKEHCXQ\nYSRCHHQLXXKKXITAAHSKMXISHIGFXMVXTZZKLXIXVHJYKTAHSHYPCFLHSWIHHCXXRXXJNKXXVVRNSKM\n"+
"CZWJKKNIXXUHKXCKRTXXSVHQZERHXIXNHXPKELNKHXRHFKHIEPKUWKXKUPHEKKIKLLSFWUSLYEEXLSI\nKHGURVKSXXKAGXIWKCSHFNVWWHNHNIVYKKAKZKFSKHCVRHUENHFIIKNUNICHYRKYXXHTLKYHPQKWHKI\n"+
"KGAFRXJMKMJJSXCUTFAKLHHFXTIXKNHKKRHIZHXWRRXJNHXQEMKKXCNVHWFMPERJKKZLHIKIUZRKWLM\nYRXXXCXXHVISVIYTHNHKSXKRXYHXUFYYKXTHXRYIIKHEWHXVZAZHTQKJAKKHXKKVCKJHWHIIZXXZZYK\n"+
"FQLXHIHJFKSKWMTHXISAXTVIKKHTIXUAGUAJHHHHRVHXHRNWKXKUKWH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CAXKXWYHXNUKTQCUKIHLWKTEKMC\n"+
"MHLXXHHYQWHTXVHCMKKHGIUFSEHNEVYSXQHHXXKHMXUXQKHZHHHSHRKWHMMEHZMIKKYYWIUNEIJUIKT\nNCHKRNSJYLLIXKXAIERXKKXIQZVHXFVIIAXHIYUGZURXFHSXKLIWESKHEXQSCUHWKALZIVEZHAFKKWA\n"+
"IKCJWNVRKMITKNYXJYKYXVXXUKWHXXJAKLXXXVKXGXKKZXMHCKKCGRLZQINVHRLNVWXTKVLTEPTXUXH\nXVCICKXXXEVTIKIHHSHCKYIJXNCJKJSLSHVXJHTHSIKUXPHWRIKMFXXMMHKHCAQWITIEWEXPFHYXXKU\n"+
"XXSHHIXHWWRKITSHSXHJKEWHXIZHSHZCHFARIXNPHYSNIKJXLQENKFCXYRWMXAPIYTZKNMXFHNIMCCS\nPAACXJMNVZZHMXXSNVSXEIINNCHLSTIXHHKYXLEHQHKHPRWIXXHRKVCXFKLHSHKGXJGKLLLWSHIISKT\n"+
"WUYXTJHXXMKJCYIXQJRNFKYHHUUKTIAYXVUUXFCYLHXWWZAUHWZIUAIGKHKIMKXXXKJZXCKKRCJAKQJ\nEVAGXIHTINSXENKWVXWCLFUVHXFPFCIFVHWXRIFZRYZKUUEMXNGAJLIFHJJIGUXFKKRIMKGFKXJYXKV\n"+
"KKRHHWKIYXWXWKLWXWKFHXLXRXRKKENMHHIQRHTTMFYTGRLSHRXIXKKUEHXXJKRIKSYLTKYJMNIUWVE")

```

Result: 714

# Answer
> 3 votes
## Python - ~~437~~ 429

Since forests contain newlines, I've set the input to come from a text file instead.
To run, specify the forest in a file called `forest.txt`,
save the code as `forest_of_alphabets.py`
and run from terminal/command line with `python forest_of_alphabets.py forest.txt`

**Code:**

```
import sys
N="@"
g=open(sys.argv[1]).read().replace(" ",N).split()
u=max(map(len,g))+1
g=[list(k)+[N]*(u-len(k))for k in g]+[[N]*u]
def f(y,x,a=0):
 c=g[y][x]
 if c>N:
    l=[zip([-1,-1,1,1],[1,-1]*2)[i]for i in 0,1,2,3if c in"CHAAEICFFJEHGKHIHLIJITKKKULMNVMNSWQPTXRRUYXSVZZXW___X___Y"[i::4]]
    if a<1 or a in l:g[y][x]=N;return[f(y+j,x+i,(-j,-i))for j,i in l]
 return 0
s=j=0
exec"i=0;exec's+=f(j,i)>0;i+=1;'*u;j+=1;"*len(g)
print s

```

Any suggestions are welcomed. Did this in a rush during my office break.
Tested with both test cases in this page and those in the alphabet tree question.

**Update:** Tested with the sample, which yields 714 too. Changed the while loops to the exec which is all the rage these days.

# Answer
> 3 votes
## C - ~~691~~ 571 Bytes

Any suggestions for further golfing are welcome.

```
#include <cstdio>
#include <cstring>
#define L(i) strlen(t[i])
#define R(a,b,c) if(z&a&&T(b,c)&8/a){f(b,c);}
#define S !!strstr(
#define U return
#define V if(i<0||j<0||j>=L(i)
char t[80][80];int n=0,I=0,J,C=0;int T(int i,int j){V)U 0;char c[]="i";c[0]=t[i][j];U 1*S"HIJKLTUVWXYZ",c)+2*S"CEFGHIKNSTUVWXY",c)+4*S"AFHIJKMNPRSX",c)+8*S"ACEHIKLMQRXZ",c);}int f(int i,int j){V||t[i][j]<0)U 0;int z=T(i,j);t[i][j]=-1;R(1,i-1,j-1)R(2,i-1,j+1)R(4,i+1,j-1)R(8,i+1,j+1)U 1;}main(){while(scanf("%s\n",t[n++])>0){};n--;for(;I<n;I++){for(J=0;J<L(I);J++){C+=f(I,J);}}printf("%d\n",C);}

```

## Ungolfed

```
#include <cstdio>
#include <cstring>
#define L(i) strlen(t[i])

char t[80][80];
int n=0;

int T(int i,int j){
   if(i<0||j<0||j>=L(i)){return 0;}
   char c[]="i";
   c[0]=t[i][j];
   return 1*!!strstr("HIJKLTUVWXYZ",c) +
          2*!!strstr("CEFGHIKNSTUVWXY",c) +
          4*!!strstr("AFHIJKMNPRSX",c) +
          8*!!strstr("ACEHIKLMQRXZ",c);
}

int f(int i,int j){
   if(i<0||j<0||j>=L(i)||t[i][j]<0){return 0;}
   int z=T(i,j);
   t[i][j]=-1;
   if(z&1&&T(i-1,j-1)&8){f(i-1,j-1);}
   if(z&2&&T(i-1,j+1)&4){f(i-1,j+1);}
   if(z&4&&T(i+1,j-1)&2){f(i+1,j-1);}
   if(z&8&&T(i+1,j+1)&1){f(i+1,j+1);}
   return 1;
}

int main(){
   while(scanf("%s\n",t[n++])>0){};
   n--;
   int c=0;
   for(int i=0;i<n;i++){
      for(int j=0;j<L(i);j++){
         c+=f(i,j);
      }
   }
   printf("%d\n",c);return 0;
}

```

## Results

The result I get from the sample input provided in the question is 714.

---
Tags: code-golf, connected-figure
---


title: Rosetta Stone Code Challenge

text:
# Question
Title: Rosetta Stone Code Challenge
I presume you've all heard of the Rosetta Stone, an ancient stone found to have essentially the same passage written on it in three different scripts. This challenge is based upon that: you have to write three versions of the same program in ***different*** languages.

The three programs must output the first `n` iban numbers, `n` being a number input by the user. There are only 30,275 iban numbers, so **if `n` is *greater* than 30275, the program should return an error.**

The challenge of the Rosetta Stone was that the scripts didn't appear to be mutually intelligible–knowing one didn't automatically make the others understandable. Your three programs should similarly be mutually unintelligible. That is, you may not golf down a Python 2 program and then submit it (or a minor modification) as a Python 3 version too, nor a C program as C++, etc. To prevent this, the Levenshtein distance between any two versions should be at least 25% of the length of the shorter of the two versions being compared, and at least 16 as an absolute numerical value. You may check conformance using this online Levenshtein distance calculator.

The winner is the person with the shortest combination of programs.

# Answer
> 4 votes
# 424 ~~425~~: Haskell, Python 2, Perl

As they should for a true Rosetta comparison, all three programs work the same way, taking input from the first command-line argument and building up the list with the same rules, and variable names, where possible.

## Haskell, 171

```
import System.Environment
main=do i<-getArgs;putStr.show$[n|n<-[0..777777],all(`notElem`"5689").show$n,all(\d->n`div`d`mod`100`notElem`13:[30..39])[1,1000]]!!(read.head$i)

```

## Python 2, 156

```
import sys
print[n for n in range(777778)if all([c not in"5689"for c in str(n)])and all([n/d%100not in[13]+range(30,40)for d in[1,1000]])][int(sys.argv[1])]

```

## Perl, 97 ~~98~~

```
$i=@ARGV[0];die if $i>30275;for(0..777777){push@n,$_ if!(/[5689]/ or/(13|3.)(|...)$/)}print@n[$i]

```

---
Tags: code-golf, rosetta-stone
---