Skip to content

Instantly share code, notes, and snippets.

@moritz
Created March 13, 2012 08:25
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 moritz/2027585 to your computer and use it in GitHub Desktop.
Save moritz/2027585 to your computer and use it in GitHub Desktop.
Tree depth in Perl 6
use v6;
# inspired by http://www.perlmonks.org/?node_id=959260
my $graphspec = '1 -> 2 2 -> 0 3 -> 4 4 -> 2 5 -> 10 6 -> 10 7 -> 9 8 -> 9
9 -> 6 10 -> 4 11 -> 10 12 -> 10 13 -> 12 14 -> 10 15 -> 2';
my %graph;
for $graphspec.match(:g, rx:s/(\d+) '->' (\d+)/) -> $/ {
%graph{$0} = +$1;
}
sub depth($source) {
($source, { %graph{$_} } ... 0) - 1;
}
say max %graph.keys.map: &depth;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment