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;
- foreach my $i (1 .. 2) {
- if ($node->[$i] eq 'humn') {
- $arg[$i] = ' $humn ';
- } else {
- $arg[$i] = &recurse_parse_tree( $node->[$i] );
- }
- }
- my $res = "($arg[1] $node->[0] $arg[2])";
- if ($res !~ m#humn#) {
- $res = eval( $res );
- }
- return ($res);
- }
- my $targ1 = &recurse_parse_tree( $monkey{root}[1] );
- my $targ2 = &recurse_parse_tree( $monkey{root}[2] );
- my ($cmd, $num) = ($targ1 =~ m#humn#) ? ($targ1, $targ2) : ($targ2, $targ1);
- print "Looking to balance: $num\n";
- print "Expression: $cmd\n";
- my $humn = 0;
- my $zero = eval( $cmd );
- $humn = 1;
- my $diff = eval( $cmd ) - $zero;
- $humn = ($num - $zero) / $diff;
- print "Human yells 0: $zero\n";
- print "Differnce between human yell values: $diff\n";
- print "Interpolation: $humn\n";
- print "Part 2: $humn\n" if (eval( $cmd ) == $num);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement