From 4bb6f8d06c0e384f3394012b1d48da58ed28cc5e Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Sun, 12 Dec 2021 01:24:32 +0300 Subject: 2020, tracking --- 2020/day2/pwcountcheck.pl | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 2020/day2/pwcountcheck.pl (limited to '2020/day2/pwcountcheck.pl') diff --git a/2020/day2/pwcountcheck.pl b/2020/day2/pwcountcheck.pl new file mode 100644 index 0000000..636a5d0 --- /dev/null +++ b/2020/day2/pwcountcheck.pl @@ -0,0 +1,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"); -- cgit v1.2.3-70-g09d2