Last active
December 11, 2015 12:19
-
-
Save masak/4600143 to your computer and use it in GitHub Desktop.
Solution to the Sierpinski mini-challenge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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