Advertisement
musifter

AoC 2022, day 21 (perl part 1)

Dec 21st, 2022
1,476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.59 KB | Source Code | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my %monkey;
  7. while (<>) {
  8.     chomp;
  9.     my ($monk, $yell) = split ':';
  10.  
  11.     if ($yell =~ m#(\d+)#) {
  12.         $monkey{$monk} = [$1];
  13.     } else {
  14.         $yell =~ m#(\w+) (.) (\w+)#;
  15.         $monkey{$monk} = [$2, $1, $3];
  16.     }
  17. }
  18.  
  19. sub recurse_parse_tree {
  20.     my $monk = shift @_;
  21.     my $node = $monkey{$monk};
  22.  
  23.     return ($node->[0]) if (@$node == 1);
  24.  
  25.     my @arg = map { &recurse_parse_tree( $node->[$_] ) } (1 .. 2);
  26.  
  27.     return (eval "$arg[0] $node->[0] $arg[1]");
  28. }
  29.  
  30. print "Part 1: ", &recurse_parse_tree( 'root' ), "\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement