summaryrefslogtreecommitdiffstats
path: root/2020/day2/pwcountcheck.pl
blob: 636a5d0133e6674080b645dfdf284bc326c0ef83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
use strict;
use warnings;

open my $fh, '<', "input" or die "no input present, $!";

my $valid = 0;

while (my $line = <$fh>) {
    chomp $line;
    if ($line =~ m/(?'first'\d+)-(?'second'\d+) (?'char'\w): (?'rest'\w+)/) {

        my $first = $+{first};
        my $second = $+{second};

        # https://www.effectiveperlprogramming.com/2010/12/count-the-number-of-things-in-a-string/
        my $count = () = $+{rest} =~ /$+{char}/g;

        $valid++ if $count >= $first and $count <= $second;
    }
}

print("$valid");