Skip to content

Instantly share code, notes, and snippets.

@masak
Last active December 11, 2015 12:19
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 masak/4600143 to your computer and use it in GitHub Desktop.
Save masak/4600143 to your computer and use it in GitHub Desktop.
Solution to the Sierpinski mini-challenge
sub MAIN($n) {
whole($n, node());
}
my $counter = 0;
sub node(*@parents) {
my $node = "node " ~ ++$counter;
say "Creating $node", @parents && " with parents " ~ @parents.grep(&defined).join(', ');
return $node;
}
sub whole($n, $top) {
if $n == 0 {
return $top, $top;
}
my ($BOTTOM_join_top, $BOTTOM_join_right, $right) = chipped($n, $top);
my $bottom = node($BOTTOM_join_top, $BOTTOM_join_right);
return $bottom, $right;
}
sub chipped($n, $top, $JOIN_top?, $JOIN_right?) {
if $n == 1 {
my $right = node($top, $JOIN_top, $JOIN_right);
return $top, $right, $right;
}
my ($TOP_bottom, $TOP_right) = whole($n - 1, $top);
my ($RIGHT_join_top, $RIGHT_join_right, $RIGHT_right) = chipped($n - 1, $TOP_right, $JOIN_top, $JOIN_right);
my ($BOTTOM_join_top, $BOTTOM_join_right, $) = chipped($n - 1, $TOP_bottom, $RIGHT_join_top, $RIGHT_join_right);
return $BOTTOM_join_top, $BOTTOM_join_right, $RIGHT_right;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment