adventofcode/2024/07/Part2.pm

28 lines
573 B
Perl
Raw Normal View History

2024-12-10 23:34:16 +00:00
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;