aoc2015

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

commit 9759bd6a2171086377f2c22b3c412810091d7345
parent df80ef0b7cc0d9f69ab10e29c3db6460e1f85c9d
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Fri,  4 Nov 2022 17:39:59 +0000

solve part 1 of day11

Diffstat:
Aday11/Day11.pm | 31+++++++++++++++++++++++++++++++
Aday11/day11.pl | 25+++++++++++++++++++++++++
2 files changed, 56 insertions(+), 0 deletions(-)

diff --git a/day11/Day11.pm b/day11/Day11.pm @@ -0,0 +1,31 @@ +package Day11; + +use strict; +use warnings; +use v5.32; + +use Exporter qw( import ); +our @EXPORT = qw( is_valid_password ); + +# create expression to match one increasing straight of at least three +# letters, like abc, bcd, cde, and so on, up to xyz. +my $number = ord( 'a' ); +my $inc_straight = join "|", map { chr ( $number + $_ ) . + chr ( $number + $_ + 1 ) . + chr ( $number + $_ + 2 ) } ( 0 .. 23 ); + +sub is_valid_password { + my $string = shift; + say "checking $string..."; + say "repeating pattern not found" if ( $string !~ m/$inc_straight/ ); + say "contains invalid character" if ( $string =~ m/[iol]/ ); + say "no two different, non-overlapping pairs of letters" if ( $string !~ m/(\w)\1.*(\w)\2/ ); + say "done checking $string."; + return 0 if ( $string !~ m/$inc_straight/ ) || + ( $string =~ m/[iol]/ ) || + ( $string !~ m/(\w)\1.*(\w)\2/ ); + return 1; +} + + +1; diff --git a/day11/day11.pl b/day11/day11.pl @@ -0,0 +1,25 @@ +#!/usr/local/bin/perl +# day 2015-11 + +use strict; +use warnings; +use v5.32; +use lib '.'; +use Day11 qw( is_valid_password ); + +my $password = 'hepxcrrq'; + +#is_valid_password( $password ); +chomp( my $input = do { local $/; <> } ); + +foreach ( split /\n/, $input ) { + print "$_ is ", is_valid_password($_) ? "" : "not "; + say "a valid password."; + say "-------------------------"; +} + +do { $password++ } until ( is_valid_password( $password ) ); +say "part 1: ", $password; + +do { $password++ } until ( is_valid_password( $password ) ); +say "part 2: ", $password;