dreasgrech / yabfcompiler

Yet another brainfuck (and Ook!) compiler...
http://blog.dreasgrech.com
1 stars 1 forks source link

Group consecutive assignments and outputs into a single output #5

Open dreasgrech opened 11 years ago

dreasgrech commented 11 years ago

Say we have the following (prints "OSLO"):

++++++++[>++++++++++<-]>-.++++.-------.+++. 

All this program does is build up one of memory locations and then outputs its current value 4 times.

This is how I can reduce it:

0+= 8, While[1+=10, 0-=1], 1-=1, OUTPUT, 1+=4, OUTPUT, 1-=7, OUTPUT, 1+=3, OUTPUT. 0+=8, 8(1+=10, 0-=1), 1-=1, OUTPUT, 1+=4, OUTPUT, 1-=7, OUTPUT, 1+=3, OUTPUT. 0+=8, 1+=80, 0-=8, 1-=1, OUTPUT, 1+=4, OUTPUT, 1-=7, OUTPUT, 1+=3, OUTPUT. 1+=79, OUTPUT, 1+=4, OUTPUT, 1-=7, OUTPUT, 1+=3, OUTPUT. 1=79, OUTPUT, 1=83, OUTPUT, 1=76, OUTPUT, 1=79, OUTPUT.

In the third pass, 0+=8 and 0-=8 cancel each other.

Notice that after the last pass, we end up with 4 assignments and 4 outputs. This means that we can concatenate those 4 different values as a string after their respective assignments/reassignments and print only once!

Console.WriteLine("OSLO");