Skip to content

Instantly share code, notes, and snippets.

@TimToady
Created July 31, 2011 21:57
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 TimToady/1117259 to your computer and use it in GitHub Desktop.
Save TimToady/1117259 to your computer and use it in GitHub Desktop.
my $pairs = 5;
my $size = $pairs + 1;
my @x;
@x[0] = 1 xx $size;
for 1 ..^ $size -> $row {
for reverse 0 .. ($size - $row - 1) -> $col {
@x[$row][$col] = (@x[$row][$col+1]//0) + @x[$row-1][$col];
}
}
.say for @x;
my %hash;
for ^1000 {
my $r = $size-1;
my $c = 0;
my $string = '';
loop {
if not defined @x[$r][$c+1] {
if $r == 0 {
last;
}
else {
$r -= 1;
$string ~= '[';
}
}
elsif $r == 0 {
$c += 1;
$string ~= ']';
}
elsif (@x[$r-1][$c] + @x[$r][$c+1]) * rand < @x[$r-1][$c] {
$r -= 1;
$string ~= '[';
}
else {
$c += 1;
$string ~= ']';
}
}
%hash{$string}++;
}
for %hash.pairs.sort(*.key) {
say .key, ' ', .value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment