summary refs log tree commit diff homepage
path: root/2021
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2021-12-01 13:32:26 -0500
committerJune McEnroe <june@causal.agency>2021-12-01 13:32:26 -0500
commit0885e8011c44dbd62dc24a26caa5e7b588eb7e2c (patch)
treee9bf02211000764bfc3e1c7beb5de4e3e44a4f22 /2021
parentSolve day 1 part 1 (diff)
downloadaoc-0885e8011c44dbd62dc24a26caa5e7b588eb7e2c.tar.gz
aoc-0885e8011c44dbd62dc24a26caa5e7b588eb7e2c.zip
Solve day 1 part 2
Diffstat (limited to '2021')
-rw-r--r--2021/day01.pl13
1 files changed, 11 insertions, 2 deletions
diff --git a/2021/day01.pl b/2021/day01.pl
index f4ab273..ffc5949 100644
--- a/2021/day01.pl
+++ b/2021/day01.pl
@@ -1,6 +1,15 @@
-my $last, $incs;
+use strict;
+use warnings;
+my ($last, @window, $incs, $incs2);
 while (<>) {
 	$incs++ if ($last && $_ > $last);
 	$last = $_;
+	if (scalar @window == 3) {
+		my $prev = $window[0] + $window[1] + $window[2];
+		my $next = $window[1] + $window[2] + $_;
+		$incs2++ if ($next > $prev);
+		shift @window;
+	}
+	push @window, $_;
 }
-print "$incs\n";
+print "$incs\n$incs2\n";