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/day4/passport.pl | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 2020/day4/passport.pl (limited to '2020/day4/passport.pl') diff --git a/2020/day4/passport.pl b/2020/day4/passport.pl new file mode 100644 index 0000000..26c6e32 --- /dev/null +++ b/2020/day4/passport.pl @@ -0,0 +1,68 @@ +use strict; +use warnings; +use Smart::Comments; +use DDP; + +my $batch; +{ + local $/; + open my $fh, '<', "input" or die "no input present, $!"; + $batch = <$fh>; +} + +my $valid = 0; + +# the file has to end with a empty line... +while ($batch =~ m/((?:[^\n][\n]?)+)/gm ) { + my $person_passport = $1; + my %passport; + + if ($person_passport =~ m/byr:(?\d{4})\s/) { + if ($+{byr} >= 1920 and $+{byr} <= 2002) { + $passport{"byr"} = $+{byr}; + } + } + + if ($person_passport =~ m/iyr:(?\d{4})\s/) { + if ($+{iyr} >= 2010 and $+{iyr} <= 2020) { + $passport{"iyr"} = $+{iyr}; + } + } + + if ($person_passport =~ m/eyr:(?\d{4})\s/) { + if ($+{eyr} >= 2020 and $+{eyr} <= 2030) { + $passport{"eyr"} = $+{eyr}; + } + } + + if ($person_passport =~ m/hgt:(?\d+)(?\w{2})\s/) { + if ($+{unit} eq "cm") { + if ($+{hgt} >= 150 and $+{hgt} <= 193) { + $passport{"hgt"} = "$+{hgt}" . $+{unit}; + } + } elsif ($+{unit} eq "in") { + if ($+{hgt} >= 59 and $+{hgt} <= 76) { + $passport{"hgt"} = "$+{hgt}" . $+{unit}; + } + } + } + + if ($person_passport =~ m/hcl:(?#[a-f0-9]{6})\s/) { + $passport{"hcl"} = $+{hcl}; + } + + if ($person_passport =~ m/ecl:(?amb|blu|brn|gry|grn|hzl|oth)\s/) { + $passport{"ecl"} = $+{ecl}; + } + + if ($person_passport =~ m/pid:(?[0-9]{9})\s/) { + $passport{"pid"} = $+{pid}; + } + + if (keys %passport == 7) { + $valid++; + $valids{$passport{"pid"}} = \%passport; + } +} + +print("$valid"); -- cgit v1.2.3-70-g09d2