Advertisement
musifter

Untitled

Dec 6th, 2019
461
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.71 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. no warnings 'recursion';
  7.  
  8. my %orbs;
  9. while (<>) {
  10.     chomp;
  11.     my ($parent, $child) = split( /\)/ );
  12.     push( @{$orbs{$parent}}, $child );
  13. }
  14.  
  15. # Recursive function to find YOU and SAN and their closest common parent.
  16. sub walk_tree {
  17.     my ($node, $depth) = @_;
  18.  
  19.     if (!exists $orbs{$node}) {
  20.         return ($node => $depth);
  21.     }
  22.  
  23.     my %ret;
  24.     foreach my $child (@{$orbs{$node}}) {
  25.         %ret = (%ret, &walk_tree( $child, $depth + 1 ));
  26.     }
  27.  
  28.     if (exists $ret{YOU} && exists $ret{SAN}) {
  29.         print "Transfers: ", $ret{YOU} + $ret{SAN} - 2 * $depth - 2, "\n";
  30.         exit (0);
  31.     }
  32.  
  33.     return (%ret);
  34. }
  35.  
  36. &walk_tree( 'COM', 0 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement