Competition: Make a simple program as complicated as you can!
51 replies, posted
The idea is simple. A simple program idea will be posted, then all people wishing to compete will submit entries. You can submit as many entries as you want, in any language you want to. To win the competition for each idea, you have to implement that simple idea in the most complicated and difficult to read way you possibly can.
Let's start with a good old Hello World application ([url=http://www.facepunch.com/threads/1032903-Programming-Jokes?p=29270330&viewfull=1#post29270330]Idea from AyeGill in this thread[/url])
Here's my entry...
[cpp]#include <iostream>
#define X main
#define Z ()
#define A using
#define F "||==|| ||== || || || ||"
#define C std;
#define E "|| || ||== || || //==\\\\"
#define K << endl;
#define L endl;
#define I " \\\\//\\\\// || || ||_// || || ||"
#define M int
#define N i;
#define B namespace
#define O cin >>
#define P return
#define J " \\/ \\/ \\\\==// || \\\\ ||== ||=//"
#define Q 0;
#define W {
#define H "\\\\ __ // //==\\\\ ||=\\\\ || ||=\\\\"
#define Y }
#define D cout <<
#define G "|| || ||== ||== ||== \\\\==//"
M X Z W A B C D E K D F K D G K D L D H K D I K D J K M N O N P Q Y[/cpp]
Get to it!
Protip: Use something like codepad if you want to test out the code of people's entries.
So I should really just post any of my applications?
[QUOTE=Zeraux;29273872]So I should really just post any of my applications?[/QUOTE]
As long as they are Hello World applications :v:
Anyone wanna do a brainfuck interpereter that runs code of a brainfuck interpereter that runs code of a brainfuck interpereter that runs a simple Hello World program?
[QUOTE=sim642;29274019]Anyone wanna do a brainfuck interpereter that runs code of a brainfuck interpereter that runs code of a brainfuck interpereter that runs a simple Hello World program?[/QUOTE]
oh dear god even the idea of a brainfuck interpreter in brainfuck fucks with my brain
[cpp]public class Hello {
public static void main(String args[]){
String s = "H";
String h = "e";
String i = "l";
String t = "l";
String ss = "oW";
String c = "o";
String a = "r";
String r = "l";
String y = "d";
System.out.println( s + h + i + t + ss + c + a + r + y );
}
}
[/cpp]
It kind of failed.
:smithicide:
[QUOTE=Staneh;29274669][cpp]System.out.println("" + s + h + i + t + ss + c + a + r + y );[/cpp][/QUOTE]
Is the first set of quotes really necessary? :confused:
I don't know really, that's how I learned it.
[QUOTE=Staneh;29275907]I don't know really, that's how I learned it.[/QUOTE]
Unless you don't already have a String there, it's not.
Say you have:
String s
boolean b
int i
it would be like this:
(s);
(""+b);
(""+i);
(s+b);
etc.
[cpp]
#include <iostream>
#define _h typedef
#define h std::
#define _f h string
#define _n main(){
#define e__h(a,b) a ## b
#define _q e__h
#define _e(a) _f a
#define bye; return 0; }
#define i
#define _l(l) _q( l , l )
#define deer =
#define n(a) _f(#a)
#define r +
#define l <
_h _e(_l);_l _o;
_n _o deer
n(_h)
r n(_e)
r n(_l)
r n(_l)
r n(_o) ;
h
e__h(co,ut)
i
_l(l)
_o;
bye
[/cpp]
[QUOTE=Dr Magnusson;29277409][img_thumb]http://i.cubeupload.com/O0Xzi2.png[/img_thumb]
[cpp]<code snip>[/cpp][/QUOTE]
Oho, that is very clever. Nice job
[QUOTE=iNova;29274055]oh dear god even the idea of a brainfuck interpreter in brainfuck fucks with my brain[/QUOTE]
Quite a few exist, actually. I guess once you have a native interpreter you can make that interpret an interpreter and give it an interpreter as input.
Name it "b.cpp" and run, with UTF-8 encoding
[cpp]
#include <iostream>
#include <cstdio>
#include <wchar.h>
//how does this werk? i dont know lol //integral part of program
#define GoAway 35
#define YouWhat b
#define b wchar_t
#define Die b *t=new b [100];
#define o fgetws(t, GoAway, IHateYou)
#define _r(a) fgetwc(a)
#define somejargon(a) _r(a)
#define somenumber 0
#define __r int
#define No delete []
#define _____r typedef
#define _R =
#define _F rewind
#define i
#define Finally return
#define r wprintf
#define Ohgod main(){
#define just return
#define whyareyoustillhere(a,b,c) a ## b ## c
#define ___r(a) a++
#define _Q <
#define _L _wfopen
#define You FILE*
#define while for
#define ____r ;
#define add -
#define two 1
#define six 5
#define seven six
#define spaceshuttle _F
#define minus +
#define times *
#define subtract -
YouWhat g(__r Meaningless, You Argh){
Die
o; o; o;
No t;
while(__r something _R somenumber ____r something _Q Meaningless add two ____r ___r(something)){
somejargon(Argh);
}
just somejargon(Argh);
}
__r
Ohgod
You Launch _R _L(L"b.cpp", L"r");
YouWhat Timetilllaunch[ two * six ];
Timetilllaunch[two add two]=g(two minus two minus two, Launch);
spaceshuttle(Launch);
Timetilllaunch[two add two minus two]=g(six minus six add two, Launch);
spaceshuttle(Launch);
Timetilllaunch[seven add two add two add two]=g(six times six minus six minus six, Launch);
spaceshuttle(Launch);
Timetilllaunch[two minus two minus two]=g(six times two times six minus seven minus six , Launch);
spaceshuttle(Launch);
Timetilllaunch[seven add two]=g(seven subtract six minus 4, Launch);
r (L"%s", Timetilllaunch);
Finally __r (0);
}
[/cpp]
Edit:
I was really bored
[img]http://i.cubeupload.com/L4lcYi.png[/img]
[cpp]
#include <stdio.h>
int main() {
long long t = 9043965888194920, i = 0;
while(i<6) putchar(*(((char*)&t)+(i++)));
return 0;
}
[/cpp]
Not exactly complicated though.
[editline]19th April 2011[/editline]
Bonus:
[cpp]
#include <Windows.h>
char* c = ".... . .-.. .-.. --- .-- --- .-. .-.. -..";
int main() {
while(*(c++)!=0)Beep(*c==' '?0:500,*c=='-'?300:150);
return 0;
}
[/cpp]
[csharp]using System;
namespace HelloWorld
{
struct Token
{
public char TokenType;
public Token[] Args;
}
static class Tokens
{
private static uint Pow(uint b, uint p)
{
if (p == 0)
{
return 1;
}
else if (p == 1)
{
return b;
}
else if (p == 2)
{
return b * b;
}
else if (p == 3)
{
return b * b * b;
}
else
{
if ((p % 2) == 0)
{
return Pow(b * b, p / 2);
}
else
{
return b * Pow(b * b, (p - 1) / 2);
}
}
}
public static Token Tokenize(string str, int idx, out int idxOut)
{
Token result;
byte b = (byte)str[idx];
if ((b >= 48) && (b <= 57))
{
result = new Token();
result.Args = null;
result.TokenType = str[idx];
idxOut = idx + 1;
}
else
{
result = new Token();
result.Args = new Token[2];
result.TokenType = str[idx];
result.Args[0] = Tokenize(str, idx + 1, out idx);
result.Args[1] = Tokenize(str, idx, out idx);
idxOut = idx;
}
return result;
}
public static uint Evaluate(Token token)
{
byte b = (byte)token.TokenType;
if ((b >= 48) && (b <= 57))
{
return (uint)(b - 48);
}
else
{
if (b == 97)
{
return Evaluate(token.Args[0]) + Evaluate(token.Args[1]);
}
else if (b == 109)
{
return Evaluate(token.Args[0]) * Evaluate(token.Args[1]);
}
else if (b == 112)
{
return Pow(Evaluate(token.Args[0]), Evaluate(token.Args[1]));
}
}
return 0;
}
}
static class Program
{
public static void Main()
{
string garbled = "a1mma1m7p24a1m5p23p22mmp22p33a1p28mmp257a1mm2p327a1mm2pa1m2p322a1mm235mmp223a1mmp235a1mmp2235a1mmp25p32a1mp23a1m25";
int[] idxs = new int[] { 0, 21, 34, 49, 70, 93 };
int dummy;
uint[] tokens = new uint[idxs.Length];
int i;
Token tkn;
byte[] bytes;
for (i = 0; i < idxs.Length; i++)
{
tkn = Tokens.Tokenize(garbled, idxs[i], out dummy);
tokens[i] = Tokens.Evaluate(tkn);
}
for (i = 0; i < idxs.Length; i++)
{
bytes = BitConverter.GetBytes(tokens[i]);
Console.Write((char)bytes[1]);
Console.Write((char)bytes[0]);
}
Console.WriteLine();
Console.ReadLine();
}
}
}[/csharp]
[img]http://img217.imageshack.us/img217/118/helloworldx.png[/img]
You won't find any magic numbers or pointer tricks, but you won't find anything related to the words "Hello World!" without some work.
[lua]function string.ToTable ( str )
local tab = {}
for i=1, string.len( str ) do
table.insert( tab, string.sub( str, i, i ) )
end
return tab
end
function string.Explode ( seperator, str )
if ( seperator == "" ) then
return string.ToTable( str )
end
local tble={}
local ll=0
while (true) do
l = string.find( str, seperator, ll, true )
if (l ~= nil) then
table.insert(tble, string.sub(str,ll,l-1))
ll=l+1
else
table.insert(tble, string.sub(str,ll))
break
end
end
return tble
end
--brain-f interpreter! COPYRIGHT 2011 ZENX2 ALL ME ME ME NO STEALY WITHOUT SAYING I MADE IT HUMPH
BF = {}
BF.cmds = {}
BF.a = {}
BF.p = 0
function BF.AddCmd(c, f)
BF.cmds[c] = f
end
BF.AddCmd(">", "BF.p = BF.p + 1")
BF.AddCmd("<", "BF.p = BF.p - 1")
BF.AddCmd("+", [[if not BF.a[BF.p] then BF.a[BF.p] = 0 end
BF.a[BF.p] = BF.a[BF.p] + 1]])
BF.AddCmd("-", [[if not BF.a[BF.p] then BF.a[BF.p] = 0 end
BF.a[BF.p] = BF.a[BF.p] - 1]])
BF.AddCmd(".", "BF.Out(string.char(BF.a[BF.p]))")
BF.AddCmd(",", "BF.a[BF.p] = string.byte(io.read())")
BF.AddCmd("[", [[if not BF.a[BF.p] then BF.a[BF.p] = 0 end
while BF.a[BF.p] and BF.a[BF.p] > 0 do]])
BF.AddCmd("]", "end")
BF.AddCmd("$", "print(\"Program Complete.\")") --an example of a custom command
BF.Out = io.write
function BF.Run(p)
BF.a = {}
BF.p = 0
local s = ""
p = string.ToTable(p)
for k, v in pairs(p) do
if BF.cmds[v] then
s = s.."\n"..BF.cmds[v]
end
end
local f = loadstring(s) --assert(loadstring(s))
return pcall(f)
end
function BF.RunRe(p)
local s = ""
function BF.Out(t)
s = s..t
end
BF.Run(p)
BF.Out = io.write
return s
end
function BF.RunAsLua(p) --a hacky method of encoding lua
local f = assert(loadstring(BF.RunRe(p))) --run the output of the program as lua
f()
end
function Convert(inp)-- These two are to save time
local out = ""
for i = 1, #inp do
out = out.." "..string.byte(string.sub(inp, i, i))
end
return string.sub(out, 2, -1)
end
function ToBrainF(te)
local ta = string.Explode("", te)
local t = {}
for k, v in pairs(ta) do
t[k] = tonumber(string.byte(v))
end
local s = ""
s = s..string.rep("+", 10) --prpare the multiplier
s = s.."["
local tn, to = {}, {}
for k, v in pairs(t) do
tn[k] = (v/10 - (v/10)%1) * 10 --closest multiple of ten
to[k] = v - tn[k] --the offset
end
for k, v in pairs(t) do
s = s..">"..string.rep("+", tn[k] / 10) --get each thing to it's number
end
s = s..string.rep("<", #t).."-]" --decrement counter, close loop
for k, v in pairs(to) do
s = s..">"..string.rep("+", v).."."
end
return s
end
--Actual Hello World
BF.RunAsLua("++++++++++[>++++++++++>+++++++++++>+++++++++++>+++++++++>+++++++++++>++++++++++>+++++++++++>+++++++++++>+++>++++++>+++++++>++++>++++++++>+++++++++++>+++++++++++>++++++>++++++++>++++++++>++++++>++++>+++++++++++>++++>+>+>>++++++++++>+++++++++++>+++++++++>+++++++++>++++++++++>+++>+++++++++++>+++>++++++>+++>+++>+++>+>>++++++++++>+++++++++++>+++++++++++>+++++++++>+++++++++++>++++++++++>+++++++++++>+++++++++++>+++>++++++>+++++++>++++>+++++++>+++++++++++>+++++++++++>++++>+++++++++++>++++>+>+>>>+++++++++++>+++>++++++>+++>+++++++++++>++++>++++>+++++++++++>+>+>>++++++++++>+++++++++++>++++++++++>+>>++++++>+++++++>++++>++++++++>+++++++++++>+++++++++++>++++>+++++++++++>++++>+>>++++++>+++++++>++++>+++++++>+++++++++++>+++++++++++>+++>++++++>+++>++++++++++>+++++++++++>++++>+++++++++++>+++++++++++>++++++++++>+++++++++++>++++++++++>+>>++++++++++>+++++++++++>+++++++++>+++++++++>++++++++++>+++>+++++++++++>+++++>+++>++++++>+++>+++>+++>+>>+>>++++++++++>+++++++++++>+++++++++++>+++>++++++++++>++++>+++>+++++++++++>+++>++++++++++>+++++++++++>+++>+++++++++++>+++++++++>++++++++++>+++++++++++>+++++++++++>++++>+++++++++++>+++++++++++>+++++++++++>++++++++++>+++++++++++>++++++++++>++++>++++++>++++++++++++>+++++++++++>++++++++++>+++++++++++>++++++++++>++++++++++>++++>+++>+++>+++>++++>+++>+++++++++++>++++>++++>+++>++++++++++>+++++++++++>+>>+>>>+++++++++++>+++++>+++>++++++>+++>+++++++++++>+++++>++++>++++>+++++++++++>+++++++++++>+++++++++++>++++++++++>+++++++++++>++++++++++>++++>+++++++++>++++++++++>+++++++++>+++++++++++>++++>+++++++++++>++++>+>>+>>++++++++++>+++++++++++>++++++++++>+>>+>>+++++++++++>+++++++++++>++++++++++>+++++++++++>+++++++++++>++++>+++++++++++>+++++>++++>+>+>++++++++++>+++++++++++>++++++++++>+>+>++++++>+++++++>++++>++++++++>+++++++++++>+++++++++++>++++++>++++++++>++++++++>++++++>++++>++++++++>+++++++++++>++++++>+++++++++++>+++++++++>++++++++++>+++++++++++>+++++++>++++>++++++>+++++++++++>+++++++++++>+++++++++++>++++++++++>+++++++++++>+++++++++++>++++>++++++>+++++++>++++>++++++++>+++++++++++>+++++++++++>++++++++>++++++++++>++++>+++>+++>++++>++++>++++>++++>++++>++++>++++>++++>++++>++++>+++++++++>++++++>++++>++++>++++>++++>++++>++++>++++>++++++>++++>++++>++++>++++>++++>++++>++++>++++>++++>++++>++++++>++++>++++>++++>++++++>++++>++++++>++++++>++++++>++++++>++++>+++++++++>++++++>++++>++++>++++>++++++>++++>++++>++++>++++>++++>++++>++++>++++>++++>++++>++++>++++>++++>++++>++++>++++++>++++>++++>++++>++++++>++++++>++++>++++>++++>++++>++++>++++>++++>++++>++++>++++>++++>++++>++++>++++>++++>++++>++++++>++++>++++>++++>++++>++++>++++>++++>++++>++++>++++>++++>++++>++++>++++>++++>++++>++++>++++>++++>++++>++++>++++++>++++>++++>++++++>++++>+++>++++>++++>++++>++++<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<-]>++.>+++++++.>.>+++++++++.>++++++.>+++++.>+.>.>++.>++++++.>.>++++++.>++.>+++++++.>.>++++++.>+++++++++.>++++.>+++++++++.>.>++.>+.>.>.>+++++++++.>++++++++.>+.>+++++++++.>+++++++.>++++++++.>++.>+++++.>++.>+.>++.>++++.>++++.>.>+++++++++.>++.>+++++++.>.>+++++++++.>++++++.>+++++.>+.>.>++.>++++++.>.>++++++.>+++++++++.>+++++++.>++++++.>.>++++++.>+.>.>.>+++++++++.>+++++++++.>+++++.>++.>+.>++.>+++++.>++++++.>++++++.>++++++.>.>.>+++++++++.>+.>.>.>.>+++++++++.>++++++.>.>++++++.>++.>+++++++.>.>.>++.>+.>.>+++++++++.>++++++.>.>++++++.>+++++++++.>+++++++.>++++++.>++.>+.>++.>+++++.>+.>++++++.>+++++++++.>++++.>+++++.>++++++.>+.>.>+++++++++.>++++++++.>+.>+++++++++.>+++++++.>++++++++.>++.>+++++.>.>++.>+.>++.>++++.>++++.>.>+++++++++.>.>+++++++++.>++.>+.>++++.>++.>+++++++.>++++.>++.>++++++++.>++.>+++++.>.>++.>++.>+++++++.>+++++.>++++.>+++++.>.>+++++.>++++++.>++++.>+++++.>.>+++.>++++++.>+++++++++.>.>++.>++++++++.>+.>.>+.>.>++++.>++.>++++.>++++.>++.>+++++.>+.>+.>++.>.>+.>.>+++++++++.>.>+++++++++.>+++++++++.>+++++.>.>++.>+.>++.>+++++.>.>++++++.>++++++.>+++++.>++++++.>++++.>+++++.>.>+++.>++++++.>+++++++++.>++++.>+++++++.>++++.>.>++++++++.>+.>.>+++++++++.>.>+++++++++.>+.>.>.>.>+++++++++.>.>+++++++++.>++.>++++.>+++++.>.>++++++.>.>+++++.>.>+.>.>.>+.>.>.>.>.>++++++.>.>++++++.>++.>+++++++.>.>++++++.>+++++++++.>++++.>+++++++++.>.>++++.>+.>++++++.>++++.>+++++++.>+++++.>.>.>.>+++++++.>+.>.>++++++++.>+.>++++.>++++++.>.>++++++.>.>++++++.>++.>+++++++.>.>++.>+.>.>++++.>++.>+++.>+++.>+++.>+++.>+++.>+++.>+++.>+++.>+++.>+++.>+.>++.>+++.>+++.>+++.>+++.>+++.>+++.>+++.>++.>+++.>+++.>+++.>+++.>+++.>+++.>+++.>+++.>+++.>+++.>++.>+++.>+++.>+++.>++.>+++.>.>.>.>.>+++++.>+++.>++.>+++.>+++.>++++++.>++.>+++.>++++++.>+++.>+++.>+++.>+++.>+++.>+++.>+++.>++++++.>++++++.>+++.>+++.>+++.>++++++.>++.>+++.>+++.>++++++.>.>.>+++.>+++.>+++.>+++.>+++.>+++.>+++.>+++.>+++.>+++.>+++.>+++.>+++.>+++.>+++.>++++++.>++.>++++++.>+++.>+++.>+++.>++++++.>+++++.>+++++.>+++++.>+++++.>+++++.>+++++.>++++++.>+++++.>+++++.>+++++.>+++++.>+++++.>+++++.>+++++.>+++++.>++++++.>++.>+++.>++++++.>++.>++++++.>++++.>+.>+.>+.>+.")
io.read()
[/lua]
[editline]18th April 2011[/editline]
Basic lua, nothing special needed
[url=http://new-justice.com/misc/upload2/savefiles/New%20Bitmap%20Image.bmp]This might just be a problem with a script I have installed, but anytime someone posts code {} these little guys keep getting fucked up[/url]
[url=http://new-justice.com/misc/upload2/savefiles/New%20Bitmap%20Image%20(2).bmp]Fuck.[/url]
[editline]19th April 2011[/editline]
Automerge :saddowns:
No more content in almost a week.. I guess this isn't an interesting as I hoped it might be.
[QUOTE=bootv2;29422346]nope
[editline]25th April 2011[/editline]
it isn't like I've been helpful at first, and turned into an asshole.
[editline]25th April 2011[/editline]
right?:tinfoil:[/QUOTE]
I know you aren't null, I was only kidding :D
[editline]25th April 2011[/editline]
[QUOTE=sim642;29422439]I made something too :v:
[cpp]#include <iostream>
using namespace std;
template<int i>struct Char{enum{value=0};};
template<>struct Char<0>{enum{value=9<<3};};
#define CHAR(x, y) template<>struct Char<x>{enum{value=Char<x-1>::value+(y)};};
CHAR(1, 29);CHAR(2, 7);CHAR(3, 3);CHAR(4, -79);CHAR(5, 87);CHAR(6, -5);CHAR(7, -14);CHAR(8, -67);
template<int i>struct Offset1{enum{value=((34+(1<<8))>>(i*4))&0xF};};
template<int i>struct Offset2{enum{value=(06424661170>>(i*4))&0xF};};
template<int i>class Output{public:static void Do(){
if (i>=8)
cout<<char(Char<Offset1<i-8>::value>::value);
else
cout<<char(Char<Offset2<i>::value>::value);
Output<i-1>::Do();
}};
template<>class Output<-1>{public:static void Do(){
cout<<endl;
}};
int main()
{
Output<11>::Do();
return 0;
}
[/cpp][/QUOTE]
Thanks for the headache
Edit: After reading it through for a second time my headache has gone away and I understand what you're doing :V
[QUOTE=Chris220;29420038]No more content in almost a week.. I guess this isn't an interesting as I hoped it might be.[/QUOTE]
Not at all!
[cpp]#include <iostream>/////////////////////////////////////////////////////////////
enum{N,J,L,G,S,C,I,i,d,p,o,e};char s[]={L,'H',o,L,'e',o,L,'l',o,L,'l',o,L,'o',o,
L,' ',o,L,'W',o,L,'o',o,L,'r',o,L,'l',o,L,'d',o,L,'!',o,L,'\n',o,L,'P',o,L,'r',o
,L,'e',o,L,'s',o,L,'s',o,L,' ',o,L,'[',o,L,'q',o,L,']',o,L,' ',o,L,'T',o,L,'o',o
,L,' ',o,L,'q',o,L,'u',o,L,'i',o,L,'t',o,L,'!',o,L,'\n',o,p,C,'q',C,0x0,I,0x0,e}
;void main(){char*c=s;char r=0;while(1)switch(*c++){case J:c=s+*c;break;case////
L:r=*c++;break;case G:r=s[*c++];break;case S:s[*c++]=r;break;case C:r=(r==*c++);
break;case I:c=r?s+*c:c++;break;case i:r++;break;case d:r--;break;case p:std:://
cin>>r;break;case o:std::cout<<r;break;case e:return;case N:default:break;}}////[/cpp]
its not really complicated, but:
[cpp]
#include <iostream>
#define chupikachu char*
#define pi(c, d) d c
#define p(a) a
#define c(a) =
#define pika std::cout
#define pikachu(a) #a
#define chu <<
int main(){
pi(ika,chupikachu); p(ika) c(hu) pikachu(pikachu); pika chu p(ika) chu pikachu( pikachu );
}
[/cpp]
So many pikachus! :psyduck:
"Hello World" is too static, you need something more algorithmic in nature, but has an extremely simple implementation, such as printing
*
**
***
****
which is actually taken directly from the poster child for this kind of competition, I don't think I have a link on me however.
Genes, genetic crossover, mutation, and a fitness function. Will evolve Hello World usually in less than 100 generations. It will give up after 250 generations.
[cpp]
package hello;
import java.util.Random;
import java.util.Arrays;
public class Main
{
private static Random random = new Random();
public static void main(String[] args)
{
char[] model = {72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33};
Gene[] testGroup = new Gene[512];
for (int i=0; i<testGroup.length; i++) testGroup[i] = new Gene();
for (Gene gene : testGroup) gene.testFitness(model);
int generation = 0;
while (testGroup[0].fitness != 0)
{
for (Gene gene : testGroup) gene.testFitness(model);
Arrays.sort(testGroup);
for (int i=0; i<256; i++)
{
Gene parentA = testGroup[i];
int a=i+256;
Gene parentB = testGroup[++i];
int b=i+256;
Gene childA = parentA.clone();
Gene childB = parentB.clone();
childA.cross(childB, childA.crossAmount);
testGroup[a] = childA;
testGroup[b] = childB;
childA.mutate(childA.mutAmount);
childB.mutate(childB.mutAmount);
}
generation++;
if (generation > 250) break;
}
System.out.println(testGroup[0]);
}
private static class Gene implements java.lang.Comparable
{
private char[] data = new char[13];
private int fitness;
private int crossAmount = random.nextInt(256), mutAmount = 2+random.nextInt(8);
private Gene()
{
for (int i=0; i<data.length; i++) data[i] = (char)random.nextInt(129);
}
private Gene(char[] data)
{
this.data = data;
}
private void cross(Gene other, int numCrosses)
{
char temp;
for (int i=0; i<numCrosses; i++)
{
int crossPos = random.nextInt(data.length);
if (random.nextBoolean()) for (int x=0; x<crossPos;x++)
{
temp = data[x];
data[x] = other.data[x];
other.data[x] = temp;
}
else for (int x=11; x>=crossPos;x--)
{
temp = data[x];
data[x] = other.data[x];
other.data[x] = temp;
}
}
}
private void mutate(int amount)
{
for (int i=0; i<amount; i++)
{
int mutPos = random.nextInt(data.length);
if (random.nextBoolean()) data[mutPos]++;
else data[mutPos]--;
}
}
private void testFitness(char[] model)
{
if (data.length != model.length)
{
fitness = Integer.MAX_VALUE;
return;
}
fitness = 0;
for (int i=0; i<data.length; i++) fitness += Math.abs(data[i] - model[i]);
}
@Override
public String toString()
{
return String.copyValueOf(data);
}
@Override
protected Gene clone()
{
Gene out = new Gene(data.clone());
out.crossAmount = crossAmount;
out.mutAmount = mutAmount;
return out;
}
public int compareTo(Object o)
{
Gene g = (Gene)o;
return fitness - g.fitness;
}
}
}
[/cpp]
EDIT: And yeah. The code sucks. That's just part of the fun!
EDIT2: Also, 128 lines. Totally intended. Right.