adventofcode/2023/09/Part1.pm

25 lines
396 B
Perl

use 5.38.0;
package Part1;
use List::AllUtils qw/ all sum /;
sub predict(@entries) {
my $sum = 0;
until( all { !$_ } @entries ) {
$sum += $entries[-1];
@entries = map { $entries[$_]-$entries[$_-1] } 1..$#entries;
}
return $sum;
}
sub solution_1 ($input) {
my @lines = map { [split]} split "\n", $input;
return sum map { predict(@$_) } @lines;
}
1;