summary refs log tree commit diff stats
path: root/2020/day-04/day-04.raku
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2020-12-05 14:02:49 +0530
committerAndinus <andinus@nand.sh>2020-12-05 14:02:49 +0530
commit0c092b17f715f7ebc551a89c24c08bdf5e8c7cc0 (patch)
tree76486c9ee3fd8d48189a55890c3897ce9d4d8d19 /2020/day-04/day-04.raku
parenta79f9b867b99e7d7bb9407cc970341feff97cf19 (diff)
downloadaoc-0c092b17f715f7ebc551a89c24c08bdf5e8c7cc0.tar.gz
Add day-04 solution
Diffstat (limited to '2020/day-04/day-04.raku')
-rwxr-xr-x2020/day-04/day-04.raku48
1 files changed, 48 insertions, 0 deletions
diff --git a/2020/day-04/day-04.raku b/2020/day-04/day-04.raku
new file mode 100755
index 0000000..e7bb0b9
--- /dev/null
+++ b/2020/day-04/day-04.raku
@@ -0,0 +1,48 @@
+#!/usr/bin/env raku
+
+sub MAIN (
+    # Part to run.
+    Int $part where * == 1|2 = 1
+) {
+    my $input = slurp "input";
+    my @passports = $input.split("\n\n");
+    my $valid_passports = 0;
+
+    MAIN: for @passports -> $passport {
+        my %fields;
+
+        for $passport.words -> $field {
+            my ($key, $value) = $field.split(":");
+            %fields{$key} = $value;
+        }
+
+        # Check for fields that are strictly required. `cid' can
+        # be skipped. Skip this passport if it's not valid.
+        for <byr iyr eyr hgt hcl ecl pid> -> $field {
+            next MAIN unless %fields{$field};
+        }
+
+        # Do validation in part 2.
+        if $part == 2 {
+            next MAIN unless (
+                (1920 ≤ %fields{<byr>} ≤ 2002)
+                and (2010 ≤ %fields{<iyr>} ≤ 2020)
+                and (2020 ≤ %fields{<eyr>} ≤ 2030)
+                and (<amb blu brn gry grn hzl oth> ∋ %fields{<ecl>})
+                and (%fields{<pid>} ~~ /^\d ** 9$/)
+                and (%fields{<hcl>} ~~ /^'#' <[\d a..f]> ** 6/)
+            );
+            given substr(%fields{<hgt>}, *-2) {
+                when 'cm' {
+                    next MAIN unless 150 ≤ substr(%fields{<hgt>}, 0, *-2) ≤ 193;
+                }
+		when 'in' {
+                    next MAIN unless 59 ≤ substr(%fields{<hgt>}, 0, *-2) ≤ 76;
+                }
+                default { next MAIN; }
+	    }
+        }
+        $valid_passports++;
+    }
+    say "Part $part: " ~ $valid_passports;
+}