30 lines
623 B
Perl
30 lines
623 B
Perl
|
use 5.36.0;
|
||
|
|
||
|
package Part1;
|
||
|
|
||
|
use List::AllUtils qw/ sum /;
|
||
|
|
||
|
no warnings qw/ uninitialized /;
|
||
|
|
||
|
sub read_input ($file) {
|
||
|
return [ map { [ split /[: ]+/ ] } $file->lines( { chomp => 1 } ) ];
|
||
|
}
|
||
|
|
||
|
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 0;
|
||
|
}
|
||
|
|
||
|
sub solve ($equations) {
|
||
|
return sum map { $_->[0] } grep { valid_equation(@$_) } @$equations;
|
||
|
}
|
||
|
|
||
|
1;
|