Monday, 17 March 2014

Brainfuck code generator in Perl

It was 8th march and tomorrow is my sister's ( aka in Tamil) birthday. I was looking around for a present and also I had been a little obsessed with coding that I thought to wish her with in a programming language. I am dabbling with Perl. So I thought about a few things like

#!/usr/bin/perl

say "Happy birthday aka";

or

#!/usr/bin/perl

while(true){
 say "Keep smiling";
}

But they were a little childish and at the same time I also had reddit on an another tab with Brainfuck in it. Its a class of esoteric programming languages and the minimalism of Brainfuck brought me up why not to wish my aka in Brainfuck. So I searched it up a little bit and found that it has only 8 characters and everything else you write is taken up as a comment.

. - Print the character at the current pointer
, - Take an input from the user
[ - Start of the loop
] - End of the loop
- - Decrease the value at the current pointer
+ - Increase the value at the current pointer
> - Move to the next pointer
< - Move to the previous pointer

and tatz it! . No more numbers, alphabets, symbols.

How the fuck could I write a program in it!

Well lets Brainfuck.

Think of the language as block of cells with all initialized to zero and > moves one cell forward and < moves one cell backward with + to increase the value at the pointer and - to decrease the value at the pointer.

The language comprises of pointers and then with + I increase the value at the pointer. With . I print the ASCII value of the value at the pointer. I don't know about Unicode support from it.

So print 'a' . It has an ASCII value of 97.

So + (97 times) and then a dot . Now to print 'b' add another + that increases the pointer value by one and a dot. Now it prints 'ab' . Now add a minus to decrement the value and bring it back to 97 and with dot it prints 'aba'.

So moving on to loops.

[ indicates the start of the loop and ] indicates the end of the loop. The value of the pointer before the ] is checked as the value to be checked to exit the loop. So +[+-] so + increases the value at the pointer making it to 1 and + inside loop increases the value at the pointer making it to 2 and then decrease the value at the pointer with - making it to 1. So it keeps looping with + and - which makes it an infinite loop. 0 at the pointer evaluates to false and exits the loop.

So the logic is to get the minimum value of the string and factorize the value if possible. To get to 16 I have (4,4) , (8,2) , (16,1) but I select the pair with the minimum sum which is (4,4) so I increase the pointer by four each time and do it four times. The code is

++++[>++++<-]>. # prints 16

I have increase the current pointer (a1) by one factor and then move on to the loop and other pointer (a2) which has the other factor. So I first increase the a1 by four and then move to the a2 increase it by four then return to the a1 and decrease by one so that makes a1 =  3 now and a2 = 4. I loop it making a2 = 8 and a1 = 2. Thus by the fourth loop I have 16 at a2 . Incase of prime numbers like 17 I make 18 and then decrease by one.

+++[>++++++<-]>-. # prints 17

I have to print "Happy birthday Sowdhini aka!" ( Sowdhini is my sister's name ) . So the minimum is 32 which is represented by space . Now I make a pointer that has 32 and I use it for another loop.

++++[>++++++++<-]>.[>++<-]>+. # prints ' A' . A space before 'A'

So I make a2 as 32 now and use it as a loop. Thus I move to the next pointer and then increase it by 2. I do it 32 times which makes it 64. Then I add 1 to make it 65 and print it.

The logic is as follows :

Take a string and get the ASCII value of the individual characters.
Take the minimum value of the set of values and factorise it.
Let it be the looping point to generate the other characters.
Each time the value at the pointer increases by ( character ASCII value / Minimum ASCII value ) (Eg. Min value = 32 , Character value = 65 , Number of times to increase it each time is 65 / 32 = 1)
Then increase it by the remaining times ( We have 64 now. We need 65. So 65 % 32 = 1).

I wrote a Perl script that does this and Open sourced it at GitHub. It takes an input string and prints the corresponding Brainfuck code.

By the way "Happy birthday Sowdhini aka!".