diff options
author | Yigit Sever | 2021-12-13 10:38:11 +0300 |
---|---|---|
committer | Yigit Sever | 2021-12-13 10:38:11 +0300 |
commit | 74b27ccca31bb757c737dd7fdc02f513f57561b2 (patch) | |
tree | e27db4cd0873c81a53d32277446d926d176304e0 /2020/day4/passport.pl | |
parent | 3919f90cfbfbba26c8e39f979280649f5e08aea8 (diff) | |
parent | ac8125750abed263619da4cc6d653bb5ab76f007 (diff) | |
download | aoc-74b27ccca31bb757c737dd7fdc02f513f57561b2.tar.gz aoc-74b27ccca31bb757c737dd7fdc02f513f57561b2.tar.bz2 aoc-74b27ccca31bb757c737dd7fdc02f513f57561b2.zip |
Merge remote-tracking branch 'origin/main'
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"); | ||