Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- my %monkey;
- while (<>) {
- chomp;
- my ($monk, $yell) = split ':';
- if ($yell =~ m#(\d+)#) {
- $monkey{$monk} = [$1];
- } else {
- $yell =~ m#(\w+) (.) (\w+)#;
- $monkey{$monk} = [$2, $1, $3];
- }
- }
- sub recurse_parse_tree {
- my $monk = shift @_;
- my $node = $monkey{$monk};
- return ($node->[0]) if (@$node == 1);
- my @arg = map { &recurse_parse_tree( $node->[$_] ) } (1 .. 2);
- return (eval "$arg[0] $node->[0] $arg[1]");
- }
- print "Part 1: ", &recurse_parse_tree( 'root' ), "\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement