Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@grondilu
Created January 20, 2014 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grondilu/8527805 to your computer and use it in GitHub Desktop.
Save grondilu/8527805 to your computer and use it in GitHub Desktop.
constant target = "Hello world!";
constant mutate_chance = .08;
constant alphabet = map &chr, ^128; # ASCII characters
constant C = 100;
sub mutate {
$^code.comb.map(
{ (rand < mutate_chance ?? alphabet.pick !! $_) x (1 xx 10, 0, 2).pick }
).join
}
sub fitness($code) {
my $string = .Str given try {
EVAL $code;
CATCH { return 0 }
}
( [+] $string.comb Zeq state @ = target.comb )
/ [+] 1, grep * > 1, $code.comb.BagHash.values;
}
loop (
my $parent = "say 'Hello world!'";
$parent ne target;
$parent = max :by(&fitness), mutate($parent) xx C
) { printf "%6d: '%s'\n", (state $)++, $parent }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment