adventofcode/2024/07/Part1.pm

30 lines
623 B
Perl
Raw Normal View History

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