Competition: Make a simple program as complicated as you can!
51 replies, posted
I got the same idea as turb :saddowns:
[csharp]using System;
using System.Collections.Generic;
using System.Data;
using System.Linq.Expressions;
namespace HelloFuck
{
class Program
{
public string Code { get; private set; }
public int CellCount { get; private set; }
public Program(string code, int cells)
{
Code = code;
CellCount = cells;
}
public Action Compile()
{
var idx = Expression.Variable(typeof (int), "idx");
var cells = Expression.Variable(typeof (int[]), "cells");
var expressions = new Stack<List<Expression>>();
expressions.Push(new List<Expression> {Expression.Assign(cells, Expression.Constant(new int[CellCount]))});
var labels = new Stack<LabelTarget>();
var writeChar = typeof (Console).GetMethod("Write", new[] {typeof (char)});
var readChar = typeof (Console).GetMethod("Read");
foreach(var ch in Code)
{
switch(ch)
{
case '>':
expressions.Peek().Add(Expression.PostIncrementAssign(idx));
break;
case '<':
expressions.Peek().Add(Expression.PostDecrementAssign(idx));
break;
case '+':
expressions.Peek().Add(Expression.PostIncrementAssign(Expression.ArrayAccess(cells, idx)));
break;
case '-':
expressions.Peek().Add(Expression.PostDecrementAssign(Expression.ArrayAccess(cells, idx)));
break;
case '[':
labels.Push(Expression.Label());
expressions.Push(new List<Expression>());
break;
case ']':
var lbl = labels.Pop();
var exp = expressions.Pop();
expressions.Peek().Add(
Expression.Loop(
Expression.IfThenElse(
Expression.GreaterThan(Expression.ArrayIndex(cells, idx), Expression.Constant(0)),
Expression.Block(exp), Expression.Break(lbl)), lbl));
break;
case '.':
expressions.Peek().Add(Expression.Call(writeChar,
Expression.Convert(Expression.ArrayIndex(cells, idx),
typeof (char))));
break;
case ',':
expressions.Peek().Add(Expression.Assign(Expression.ArrayAccess(cells, idx),
Expression.Call(readChar)));
break;
}
}
return Expression.Lambda<Action>(Expression.Block(new[] {idx, cells}, expressions.Pop())).Compile();
}
static void Main(string[] args)
{
var program =
new Program(
@"++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.",
100);
program.Compile()();
}
}
}
[/csharp]
First time using expression trees, it's surprisingly easy.
[url]http://dotnetpad.net/ViewPaste/p6ao9khGbUmcT0xFSHBHiA[/url]
Beaten to it, but meh.
[lua]
-- Simple "Hello World" program in Lua 5.1
-- Uses genetic algorithms with Levenshtein distance as the fitness function
GOAL = "helloworld"
N_DUDES = 25
MUTATION_RATE = 0.005
POP_SIZE = 200
-- totally not stolen from Overv, who totally did not steal it from Geoffroy Carrier of lua-users.org
-- ([url]http://evolvemod.googlecode.com/svn/trunk/beta/lua/ev_plugins/sv_chatcommands.lua[/url])
function levenshtein(s, t)
local d, sn, tn = {}, #s, #t
local byte, min = string.byte, math.min
for i = 0, sn do d[i * tn] = i end
for j = 0, tn do d[j] = j end
for i = 1, sn do
local si = byte(s, i)
for j = 1, tn do
d[i*tn+j] = min(d[(i-1)*tn+j]+1, d[i*tn+j-1]+1, d[(i-1)*tn+j-1]+(si == byte(t,j) and 0 or 1))
end
end
return d[#d]
end
-- roulette wheel selection
-- it needs to use the _inverse_ of the score, because the levenshtein function gives a low score for better performers
-- this confused the $%@# out of me when I was debugging
function roulette(dudes)
local total = 0
for dude_i = 1, #dudes do
if dudes[dude_i].score == 0 then
return dude
end
total = total+1/dudes[dude_i].score
end
local the_chosen_value = math.random()*total
local count, dude = 0
for dude_i = 1, #dudes do
dude = dudes[dude_i]
if the_chosen_value >= count and the_chosen_value <= count+1/dude.score then
return dude
end
count = count+1/dude.score
end
end
-- generation iteration function
function the_next_generation(dudes)
local dude
for dude_i = 1, #dudes do
dude = dudes[dude_i]
local len_s = ""
for len_s_i = 1, GOAL_LENGTH_BITS do
len_s = len_s..(dude.code[len_s_i] and 1 or 0)
end
dude.len = #GOAL--tonumber(len_s, 2)
local s = ""
for st_i = 1, dude.len do
local c_bits = ""
for bit_i = 1, GOAL_RANGE_BITS do
c_bits = c_bits..(dude.code[GOAL_LENGTH_BITS+GOAL_RANGE_BITS*st_i+bit_i] and 1 or 0)
end
s = s..string.char(GOAL_LOWEST+tonumber(c_bits, 2))
end
dude.str = s
dude.score = levenshtein(s, GOAL)
--print(dude.score, dude.len, s)
end
table.sort(dudes, function(a, b)
return a.score < b.score
end)
local new_dudes = {}
for new_dude_i = 1, #dudes do
local parental_dude_1 = roulette(dudes)
local parental_dude_2 = roulette(dudes)
local split_point = math.random(0, GOAL_REQUIRED_BITS)
local new_code = {}
for bits_i = 1, split_point do
new_code[bits_i] = parental_dude_1.code[bits_i]
end
for bits_i = split_point+1, GOAL_REQUIRED_BITS do
new_code[bits_i] = parental_dude_2.code[bits_i]
end
for bits_i = 1, GOAL_REQUIRED_BITS do
if math.random() < MUTATION_RATE then
new_code[bits_i] = not new_code[bits_i]
end
end
new_dudes[new_dude_i] = {
code = new_code,
}
parental_dude_1.selected, parental_dude_2.selected = true, true
end
return new_dudes, dudes[1].score, dudes[1].str
end
math.randomseed(os.time())
-- testin'
--[[ roulette selection
do
local n, set, total = math.random(5, 10), {}, 0
for i = 1, n do set[i] = {i=i,score=math.random()*100,p=0} total=total+set[i].score end
local cset = {}
for i = 1, 10000 do
local c = roulette(set).i
set[c].p = set[c].p+1
end
for i = 1, n do print(i, 1/set[i].score, set[i].p) end
return
end
]]
-- calculation stuff
GOAL_LOWEST, GOAL_HIGHEST = math.huge, 0
for i = 1, #GOAL do
GOAL_LOWEST = math.min(string.byte(string.sub(GOAL, i, i)), GOAL_LOWEST)
GOAL_HIGHEST = math.max(string.byte(string.sub(GOAL, i, i)), GOAL_HIGHEST)
end
GOAL_RANGE = GOAL_HIGHEST-GOAL_LOWEST
GOAL_RANGE_BITS = math.ceil(math.log(GOAL_RANGE)/math.log(2))
GOAL_LENGTH = #GOAL
GOAL_LENGTH_BITS = math.ceil(math.log(#GOAL)/math.log(2))
GOAL_REQUIRED_BITS = GOAL_LENGTH_BITS+GOAL_RANGE_BITS*2^GOAL_LENGTH_BITS
print(GOAL_LOWEST, GOAL_HIGHEST, GOAL_RANGE_BITS)
-- initial generation
local dudes = {}
for dude_i = 1, POP_SIZE do
local code = {}
for code_i = 1, GOAL_REQUIRED_BITS do
--code[code_i] = math.random(0xDEADBEEF, 0xDEADBEEF+1) == 0xDEADBEEF -- no idea why but this didn't work
code[code_i] = math.random(1337, 1338) == 1337
end
dude = {
code = code,
}
dudes[dude_i] = dude
end
local wesley_crushers_score, generation_n = math.huge, 0
while wesley_crushers_score ~= 0 do
local new_best_score, new_best_str
dudes, new_best_score, new_best_str = the_next_generation(dudes)
if new_best_score < wesley_crushers_score then
wesley_crushers_score = new_best_score
print(generation_n, "NEW BEST!", new_best_score, new_best_str)
else
print(generation_n,"CURRENT BEST:", new_best_score, new_best_str)
end
generation_n = generation_n+1
local c = ""--io.read()
if string.match(c,"[Ss]") then
print("failure :(")
return
end
end
print("SUCESS!")
[/lua]
[QUOTE=Tangara;29460413]eval is not metaprogramming[/QUOTE]
Thanks for pointing that out because I didn't know that.
Slight bump
Maybe we could change the thread to "Design a sony level security piece of cryptographic software"
Ie, design a program that looks plausibly secure, but with a hidden fatal security hole in.
[csharp]public int randomNumber() //chosen by a fair dice roll, guaranteed to be random
{
return 4;
}[/csharp]
like that?
[QUOTE=robmaister12;29623098][csharp]public int randomNumber() //chosen by a fair dice roll, guaranteed to be random
{
return 4;
}[/csharp]
like that?[/QUOTE]
Four != Hello World
[QUOTE=robmaister12;29623098][csharp]public int randomNumber() //chosen by a fair dice roll, guaranteed to be random
{
return 4;
}[/csharp]
like that?[/QUOTE]
Dude you should totally work for sony.
[QUOTE=Crimor;29628311]Dude you should totally work for sony.[/QUOTE]
someone else got there first
Writer.java
[code]
package com.simssi.helloworld;
public class Writer
{
public void write(char c)
{
System.out.print(c);
}
}
[/code]WordWriter.java
[code]
package com.simssi.helloworld;
public class WordWriter extends Writer
{
private String word;
public WordWriter(String word)
{
this.word = word;
}
public void writeWord(boolean newline)
{
for (char c : word.toCharArray())
{
write(c);
}
if(newline)
{
for(char c2 : System.getProperty("line.separator").toCharArray())
{
write(c2);
}
}
}
public void setWord(String word)
{
this.word = word;
}
}
[/code]HexToAsciiConverter.java
[code]
package com.simssi.helloworld;
public class HexToAsciiConverter
{
private String s;
public HexToAsciiConverter(String s)
{
this.s = s;
}
public String convert()
{
String result = "";
for(int i = 0; i < s.length()-1; i+=2)
{
result += (char)Integer.parseInt(s.substring(i, i + 2), 16);
}
return result;
}
public void setString(String s)
{
this.s = s;
}
}
[/code]HelloWorldWriter.java
[code]
package com.simssi.helloworld;
public class HelloWorldWriter
{
private WordWriter wordWriter;
private HexToAsciiConverter converter;
private static String helloWorldText = "48656c6c6f20776f726c6421";
public HelloWorldWriter()
{
converter = new HexToAsciiConverter(helloWorldText);
wordWriter = new WordWriter(converter.convert());
}
public void writeHelloWorld()
{
wordWriter.writeWord(true);
}
}
[/code]Main.java
[code]
package com.simssi.helloworld;
public class Main
{
public static void main(String[] args)
{
HelloWorldWriter writer = new HelloWorldWriter();
writer.writeHelloWorld();
}
}
[/code]Output:
[code]
Hello world!
[/code]
edit:
Made another one.
ImageWriter.java
[code]
import java.awt.Color;
import java.awt.image.*;
public final class ImageWriter
{
public static BufferedImage GenerateImage(String s)
{
int imageWidth = (int)Math.floor(s.length() / 3);
if(s.length() % 3 > 0)
imageWidth++;
BufferedImage image = new BufferedImage(imageWidth, 1, BufferedImage.TYPE_INT_RGB);
int pos = 0;
for(int i = 0; i < imageWidth; i++)
{
if(pos + 2 < s.length()-1)
{
String cd = s.substring(pos, pos + 3);
image.setRGB(i, 0, new Color(cd.charAt(2), cd.charAt(1), cd.charAt(0)).getRGB());
pos+=3;
}
else if(pos +1 < s.length()-1)
{
String cd = s.substring(pos, pos + 2);
image.setRGB(i, 0, new Color(0, cd.charAt(1),cd.charAt(0)).getRGB());
pos+=2;
}
else
{
image.setRGB(i, 0, new Color(0, 0,s.charAt(pos)).getRGB());
pos++;
}
}
return image;
}
}
[/code]
Main.java
[code]
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Main
{
public static void main(String[] args)
{
String source =
"\n#include <iostream>\n" +
"using namespace std;\n" +
"int main()\n" +
"{\n" +
"\tcout << \"Hello world!\" << endl;\n"+
"\treturn 0;\n"+
"}\n\n"
;
BufferedImage img = ImageWriter.GenerateImage(source);
try
{
File output = new File("output.bmp");
ImageIO.write(img, "bmp", output);
}
catch(IOException ex)
{
System.out.println(ex.toString());
}
System.out.println("Done.");
}
}
[/code]
Output:
[img]http://www.1337upload.net/files/output.bmp[/img]
Output as text:
[img]http://www.1337upload.net/files/SS-2011-05-08_01.07.28.png[/img]
[media]http://dl.dropbox.com/u/5124198/ComplicatedHelloWorld.png[/media]
[cpp]
#include "letterHandle.h"
using namespace std;
int main()
{
letterHandle* word = new letterHandle("Hello there my good world\n",26);
word->print();
delete word;
return 0;
}
[/cpp]
The main code. Doesn't look too complicated?
(I just realized that I put the "using namespace std;" even if I don't use it...)
Look at the class diagram:
[media]http://dl.dropbox.com/u/5124198/chwcd.png[/media]
There is a class for every character in the the alphabet plus space and \n
They inherits from an interface class. And a handle converts every character in the string to it's appropriate class (done through Switch case, since C++ doesn't support reflection, could've probably been done better but whatever). It supports both uppercase and lowercase.
Here's the source if anyone wants:
[url]http://dl.dropbox.com/u/5124198/ComplicatedHelloWorld.rar[/url]
How did you generate that class diagram? Looks handy!
Action Script 3.0: (Its a document class)
[code]package
{
import flash.display.MovieClip;
public class HelloWorld extends MovieClip
{
private var target:String = "Hello World!";
public function HelloWorld()
{
var i = 0;
if (1 == 1)
{
if (target.length > -1)
{
i = 0;
while (i < target.length)
{
var lollol:Object;
lollol = new Object();
lollol.lol = target.charAt(i);
say(lollol);
i++;
}
}
}
else
{
trace("You win. 1 isnt equal to 1");
}
}
public function say(toSay:Object):String
{
trace(toSay.lol);
return toSay.lol;
}
}
}
[/code]
[QUOTE=Chris220;29798908]How did you generate that class diagram? Looks handy![/QUOTE]
In Visual Studio, right click the project in solution explorer -> view class diagram.
[QUOTE=Darwin226;29803455]In Visual Studio, right click the project in solution explorer -> view class diagram.[/QUOTE]
When i tried it, it didnt show the connections only the class members
It shows them fine for me. The connections are the inheritance.
Here, have some Blitzmax:
[code]Framework brl.standardio
Global st$,ok$=" ",piece$[8],scan[12,5],kk$="##",nk$
piece[0]=nk;piece[1]=kk+ok+kk;piece[2]=kk+kk+kk;piece[3]=kk+kk
piece[4]=kk;piece[5]=ok+kk;piece[6]=ok;piece[7]=kk+ok
setscan 0,1,2,7,4,2,8,1,2,2,4,3,3;setscan 2,2,3,7,7,1,0,1,1,3,4,9,5
setscan 4,1,1,4,8,9,3,2,2,0,1,5,1;setscan 3,9,4,7,4,1,5,2,1,9,4,1,6
setscan 1,1,4,7,4,1,6,1,9,9,7,9,5;setscan 4,9,2,2,2,2,3,1,2,1,2,3,5
Function printscans(sk@=4)
For Local sx@=1 To 5
printcsans sx-sk
Next
Input
End Function
Function setscan(sk@,s0@,s1@,s2@,s3@,s4@,s5@,s6@,s7@,s8@,s9@,sa@,sb@)
scan[0,sk]=s0;scan[1,sk]=s1;scan[2,sk]=s2;scan[3,sk]=s3;scan[4,sk]=s4;scan[5,sk]=s5;scan[6,sk]=s6
scan[7,sk]=s7;scan[8,sk]=s8;scan[9,sk]=s9;scan[10,sk]=sa;scan[11,sk]=sb
End Function
printscans(1)
Function printcsans(sk@)
For Local sx@=0 To 11
Local ss$=piece[scan[sx,sk]Mod (scan[1,sx-sx]*Len(piece[7]))]
If Len(ss)=Len(piece[4]) And Len(piece[7])>Len(piece[0]) ss:+" "
If Len(ss)=Len(piece[2])-Len(piece[3])+Len(piece[6]) ss:+" "
If Len(ss)=Len(piece[1])-Len(piece[3])-Len(piece[6]) ss:+" "
st:+" "+ss+" "
Next
Print st;st=""
End Function
End[/code]
[img]http://dl.dropbox.com/u/10116881/images/ehello.png[/img]
Sorry, you need to Log In to post a reply to this thread.