Archive for June 2007

You are browsing the www. Michael Righi .com archives for June 2007

Guess What This Java Code Does (2)

Let’s play a game. I wrote a little Java program today for fun. As you’ll see I purposefuly obfuscated the code.

There are two parts to this game.

1.) See if you can figure out what the program does.

2.) If you know Java, see if you can make my code any smaller. The current version is 562 bytes, but I’ll bet somebody can find a way to make it even tinier.

Good luck.

F.java:


import java.io.*;public class F{public static void main(String[]a)throws Exception{char[]m=new char[30000];String d="";FileReader f=new FileReader(a[0]);int p=0,i=0,h,n;while((h=f.read())!=-1)d+=(char)h;for(;i<d.length();i++){n=1;switch(d.charAt(i)){case'+':m[p]+=2;
case'-':m[p]--;break;case'.':System.out.print(m[p]);break;case',':m[p]=(char)
System.in.read();break;case']':if(m[p]!=0)while(n>0)switch(d.charAt(--i)){case']':n+=2;case'[':n--;}p++;case'<':p--;break;case'[':if(m[p]==0)while(n>0)switch(d.charAt(++i)){case'[':n+=2;case']':
n--;}p--;case'>':p++;}}}}