aoc2021

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

commit bb3b36bbe05a9542e4d58a6121801734fa2b1b14
parent 9e04b14633f111fba29d08216bbe54fef87f1b77
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Wed,  1 Dec 2021 21:52:15 +0000

add Day 01 Part 1 and Part 2 solutions as 1 file

Diffstat:
Aday01.pl | 29+++++++++++++++++++++++++++++
1 file changed, 29 insertions(+), 0 deletions(-)

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) +);