Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- no warnings 'recursion';
- my %orbs;
- while (<>) {
- chomp;
- my ($parent, $child) = split( /\)/ );
- push( @{$orbs{$parent}}, $child );
- }
- # Recursive function to find YOU and SAN and their closest common parent.
- sub walk_tree {
- my ($node, $depth) = @_;
- if (!exists $orbs{$node}) {
- return ($node => $depth);
- }
- my %ret;
- foreach my $child (@{$orbs{$node}}) {
- %ret = (%ret, &walk_tree( $child, $depth + 1 ));
- }
- if (exists $ret{YOU} && exists $ret{SAN}) {
- print "Transfers: ", $ret{YOU} + $ret{SAN} - 2 * $depth - 2, "\n";
- exit (0);
- }
- return (%ret);
- }
- &walk_tree( 'COM', 0 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement