summaryrefslogtreecommitdiffstats
path: root/2020/day4/passport.pl
diff options
context:
space:
mode:
Diffstat (limited to '2020/day4/passport.pl')
-rw-r--r--2020/day4/passport.pl68
1 files changed, 68 insertions, 0 deletions
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 @@
1use strict;
2use warnings;
3use Smart::Comments;
4use DDP;
5
6my $batch;
7{
8 local $/;
9 open my $fh, '<', "input" or die "no input present, $!";
10 $batch = <$fh>;
11}
12
13my $valid = 0;
14
15# the file has to end with a empty line...
16while ($batch =~ m/((?:[^\n][\n]?)+)/gm ) {
17 my $person_passport = $1;
18 my %passport;
19
20 if ($person_passport =~ m/byr:(?<byr>\d{4})\s/) {
21 if ($+{byr} >= 1920 and $+{byr} <= 2002) {
22 $passport{"byr"} = $+{byr};
23 }
24 }
25
26 if ($person_passport =~ m/iyr:(?<iyr>\d{4})\s/) {
27 if ($+{iyr} >= 2010 and $+{iyr} <= 2020) {
28 $passport{"iyr"} = $+{iyr};
29 }
30 }
31
32 if ($person_passport =~ m/eyr:(?<eyr>\d{4})\s/) {
33 if ($+{eyr} >= 2020 and $+{eyr} <= 2030) {
34 $passport{"eyr"} = $+{eyr};
35 }
36 }
37
38 if ($person_passport =~ m/hgt:(?<hgt>\d+)(?<unit>\w{2})\s/) {
39 if ($+{unit} eq "cm") {
40 if ($+{hgt} >= 150 and $+{hgt} <= 193) {
41 $passport{"hgt"} = "$+{hgt}" . $+{unit};
42 }
43 } elsif ($+{unit} eq "in") {
44 if ($+{hgt} >= 59 and $+{hgt} <= 76) {
45 $passport{"hgt"} = "$+{hgt}" . $+{unit};
46 }
47 }
48 }
49
50 if ($person_passport =~ m/hcl:(?<hcl>#[a-f0-9]{6})\s/) {
51 $passport{"hcl"} = $+{hcl};
52 }
53
54 if ($person_passport =~ m/ecl:(?<ecl>amb|blu|brn|gry|grn|hzl|oth)\s/) {
55 $passport{"ecl"} = $+{ecl};
56 }
57
58 if ($person_passport =~ m/pid:(?<pid>[0-9]{9})\s/) {
59 $passport{"pid"} = $+{pid};
60 }
61
62 if (keys %passport == 7) {
63 $valid++;
64 $valids{$passport{"pid"}} = \%passport;
65 }
66}
67
68print("$valid");