adventofcode/2024/07/Part2.pm
2024-12-10 18:34:16 -05:00

28 lines
573 B
Perl

use lib '.';
use Part1;
package Part2;
use List::AllUtils qw/ sum /;
use 5.36.0;
sub valid_equation ( $total, @numbers ) {
return $numbers[0] == $total if @numbers == 1;
return 0 if $numbers[0] > $total;
my $i = shift @numbers;
my $j = shift @numbers;
return 1 if valid_equation( $total, "$i$j", @numbers );
return 1 if valid_equation( $total, $i*$j, @numbers );
return 1 if valid_equation( $total, $i+$j, @numbers );
return 0;
}
sub solve ($equations) {
return sum map { $_->[0] } grep { valid_equation(@$_) } @$equations;
}
1;