diff options
-rw-r--r-- | 2023/day09.pl | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/2023/day09.pl b/2023/day09.pl index 5ec28b2..c9814c9 100644 --- a/2023/day09.pl +++ b/2023/day09.pl @@ -13,16 +13,21 @@ sub extrapolate { while (any { $_ != 0 } @{$diffs[0]}) { unshift @diffs, [diff(@{$diffs[0]})]; } + unshift @{$diffs[0]}, 0; push @{$diffs[0]}, 0; for (1 .. $#diffs) { - push @{$diffs[$_]}, $diffs[$_-1]->[-1] + $diffs[$_]->[-1]; + unshift @{$diffs[$_]}, $diffs[$_][0] - $diffs[$_-1][0]; + push @{$diffs[$_]}, $diffs[$_-1][-1] + $diffs[$_][-1]; } - $diffs[-1]->[-1]; + ($diffs[-1][0], $diffs[-1][-1]); } -my $sum = 0; +my ($sum, $sum2) = (0, 0); while (<>) { chomp; my @history = split(/ /); - $sum += extrapolate(@history); + my ($first, $last) = extrapolate(@history); + $sum += $last; + $sum2 += $first; } print $sum, "\n"; +print $sum2, "\n"; |