diff options
author | Yigit Sever | 2021-12-12 01:24:32 +0300 |
---|---|---|
committer | Yigit Sever | 2021-12-12 01:24:32 +0300 |
commit | 4bb6f8d06c0e384f3394012b1d48da58ed28cc5e (patch) | |
tree | d6478c85c0488a1059567ccd2882cb10039c2546 /2020/day4/passport.pl | |
parent | ae3853b6e8ab02023ccd74baac6dc177b1ee879a (diff) | |
download | aoc-4bb6f8d06c0e384f3394012b1d48da58ed28cc5e.tar.gz aoc-4bb6f8d06c0e384f3394012b1d48da58ed28cc5e.tar.bz2 aoc-4bb6f8d06c0e384f3394012b1d48da58ed28cc5e.zip |
2020, tracking
Diffstat (limited to '2020/day4/passport.pl')
-rw-r--r-- | 2020/day4/passport.pl | 68 |
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 @@ | |||
1 | use strict; | ||
2 | use warnings; | ||
3 | use Smart::Comments; | ||
4 | use DDP; | ||
5 | |||
6 | my $batch; | ||
7 | { | ||
8 | local $/; | ||
9 | open my $fh, '<', "input" or die "no input present, $!"; | ||
10 | $batch = <$fh>; | ||
11 | } | ||
12 | |||
13 | my $valid = 0; | ||
14 | |||
15 | # the file has to end with a empty line... | ||
16 | while ($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 | |||
68 | print("$valid"); | ||