aoc2015

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

commit 1357eab0332f1cabf724d762d5dc8ba7221424c7
parent d5f60abc2932d943ec3525f5a3a98b1d4ed62272
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Wed, 19 Oct 2022 12:35:24 +0000

solve part 2 of day05

Diffstat:
Mday05/Day05.pm | 17++++++++++++++++-
Mday05/day05.pl | 7++++---
2 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/day05/Day05.pm b/day05/Day05.pm @@ -4,7 +4,7 @@ use strict; use warnings; use Exporter qw( import ); -our @EXPORT = qw( is_nice_1 ); +our @EXPORT = qw( is_nice_1 is_nice_2 ); sub is_nice_1 { my $string = shift @_; @@ -22,4 +22,19 @@ sub is_nice_1 { return 1; } +sub is_nice_2 { + my $string = shift @_; + + # match a pair of any two letters ( (\w\w) ) that appears at least + # twice ( \1 ) in the string without overlapping ( \w* ) + return 0 unless ( $string =~ m/(\w\w)\w*\1/ ); + + # ensure that string contains at least one letter which repeats + # with exactly one letter between them + return 0 unless ( $string =~ m/(\w)\w\1/ ); + + # if we've gotten this far, we've met both criteria + return 1; +} + 1; diff --git a/day05/day05.pl b/day05/day05.pl @@ -5,17 +5,18 @@ use strict; use warnings; use v5.32; use lib '.'; -use Day05 qw( is_nice_1 ); +use Day05 qw( is_nice_1 is_nice_2 ); @ARGV = "input" unless @ARGV; chomp( my $input = do { local $/; <> } ); my @strings = split /\n/, $input; -my $nice_1 = 0; +my ( $nice_1, $nice_2 ) = ( 0, 0 ); foreach ( @strings ) { $nice_1++ if ( is_nice_1( $_ ) ); + $nice_2++ if ( is_nice_2( $_ ) ); } say "part 1: ", $nice_1; -say "part 2: "; +say "part 2: ", $nice_2;