From c2219772863be4c5d66d88617c2a65b7ed0a4e76 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Fri, 15 Dec 2023 20:28:53 -0500 Subject: Solve day 9 part 2 --- 2023/day09.pl | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to '2023/day09.pl') 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"; -- cgit 1.4.1