summary refs log tree commit diff homepage
path: root/2023
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2023-12-15 20:28:53 -0500
committerJune McEnroe <june@causal.agency>2023-12-15 20:35:04 -0500
commitc2219772863be4c5d66d88617c2a65b7ed0a4e76 (patch)
tree9676378bf0c0d01f949dc5c9a789cd9845d62374 /2023
parentSolve day 9 part 1 (diff)
downloadaoc-c2219772863be4c5d66d88617c2a65b7ed0a4e76.tar.gz
aoc-c2219772863be4c5d66d88617c2a65b7ed0a4e76.zip
Solve day 9 part 2
Diffstat (limited to '')
-rw-r--r--2023/day09.pl13
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";