Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- use feature qw(say state);
- use List::Util qw(sum);
- $| = 1;
- # Read sections:
- $/ = '';
- my @section = map {[split /\n/]} <>;
- my @patt = split( /, /, $section[0][0] );
- # Part 1
- my $patt_re = '^(' . join('|', @patt) . ')*$';
- my @good = grep { m#$patt_re# } $section[1]->@*;
- say "Part 1: ", scalar @good;
- # Part 2
- sub recurse_count {
- my ($str) = @_;
- state %memo;
- return ($memo{$str}) if (exists $memo{$str});
- return ($memo{$str} = 1) if ($str eq '');
- my $ret = 0;
- foreach my $patt (@patt) {
- my $new_str = $str;
- $ret += &recurse_count( $new_str ) if ($new_str =~ s#^$patt##);
- }
- return ($memo{$str} = $ret);
- }
- say "Part 2: ", sum map {&recurse_count( $_ )} @good;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement