Advertisement
musifter

AoC 2024, day 7, part 2 (Perl)

Dec 7th, 2024 (edited)
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.60 KB | Source Code | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use feature         qw(say);
  7.  
  8. sub recurse {
  9.     my ($acc, $targ, @list) = @_;
  10.  
  11.     return ($acc == $targ)  if (!@list || $acc > $targ);
  12.  
  13.     my $next = shift @list;
  14.     return (&recurse( $acc + $next, $targ, @list )
  15.             || &recurse( $acc * $next, $targ, @list )
  16.             || &recurse( int($acc . $next), $targ, @list )); # remove for part 1
  17. }
  18.  
  19. my $part2 = 0;
  20. foreach my $line (map {[map {int} m#(\d+)#g]} <>) {
  21.     my $targ  = shift @$line;
  22.     $part2 += $targ if (&recurse( shift @$line, $targ, @$line ));
  23. }
  24.  
  25. say "Part 2: ", $part2;
  26.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement