aoc2021

Advent of Code 2021 solutions in Perl.
git clone git://git.samirparikh.com/aoc2021
Log | Files | Refs | README

commit a176b8b3e9eab8606ff682fba3908cb449b33e29
parent be56770db53108856ff2a4184c3802ee23a0b977
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Wed,  1 Dec 2021 21:53:38 +0000

Merge branch 'dev'

Diffstat:
Dday01-1.pl | 16----------------
Aday01.pl | 29+++++++++++++++++++++++++++++
2 files changed, 29 insertions(+), 16 deletions(-)

diff --git a/day01-1.pl b/day01-1.pl @@ -1,16 +0,0 @@ -#!/usr/bin/env perl - -use strict; -use warnings; -use v5.22; - -if (@ARGV !=1) { - die "Usage: $0 [input-filename]"; -} - -my $input_filename = $ARGV[0]; -open my $filehandle, '<', $input_filename or - die "Could not open input file $input_filename: $!"; - -chomp( my @input = ( <$filehandle> ) ); -say scalar( grep { $input[$_] > $input[$_ - 1] } (1 .. $#input) ); diff --git a/day01.pl b/day01.pl @@ -0,0 +1,29 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use v5.22; +use diagnostics; + +if (@ARGV !=1) { + die "Usage: $0 [input-filename]"; +} + +my $input_filename = $ARGV[0]; +open my $filehandle, '<', $input_filename or + die "Could not open input file $input_filename: $!"; + +chomp( my @input = ( <$filehandle> ) ); + +# Part 1 +say scalar( grep { $input[$_] > $input[$_ - 1] } (1 .. $#input) ); + +# Part 2 +say scalar( + grep { + my $overlap = $input[$_] + $input[$_ + 1]; + my $first_window = $overlap + $input[$_ - 1]; + my $second_window = $overlap + $input[$_ + 2]; + $second_window > $first_window + } (1 .. $#input - 2) +);