adventofcode/2023/09/Part2.pm

33 lines
498 B
Perl
Raw Permalink Normal View History

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