aoc2015

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

Day05.pm (1018B) - raw


      1 package Day05;
      2 
      3 use strict;
      4 use warnings;
      5 
      6 use Exporter qw( import );
      7 our @EXPORT = qw( is_nice_1 is_nice_2 );
      8 
      9 sub is_nice_1 {
     10     my $string = shift @_;
     11 
     12     # ensure that we have at least 3 vowels
     13     return 0 unless ( ( () = $string =~ m/[aeiou]/g ) > 2 );
     14 
     15     # check that we have a repeating character using a back reference
     16     return 0 unless ( $string =~ m/(\w)\1/ );
     17 
     18     # check that we don't have prohibited substrings
     19     return 0 if ( $string =~ m/ab|cd|pq|xy/ );
     20 
     21     # if we've gotten this far, we've met all 3 criteria
     22     return 1;
     23 }
     24 
     25 sub is_nice_2 {
     26     my $string = shift @_;
     27 
     28     # match a pair of any two letters ( (\w\w) ) that appears at least
     29     # twice ( \1 ) in the string without overlapping ( \w* )
     30     return 0 unless ( $string =~ m/(\w\w)\w*\1/ );
     31 
     32     # ensure that string contains at least one letter which repeats
     33     # with exactly one letter between them
     34     return 0 unless ( $string =~ m/(\w)\w\1/ );
     35 
     36     # if we've gotten this far, we've met both criteria
     37     return 1;
     38 }
     39 
     40 1;