adventofcode/2023/09/Part2.pm
2023-12-09 13:02:55 -05:00

33 lines
498 B
Perl

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