]> git.sommitrealweird.co.uk Git - advent-of-code-2020.git/blobdiff - day04/count_valid_passports.py
Move to have day be 2 digit always
[advent-of-code-2020.git] / day04 / count_valid_passports.py
diff --git a/day04/count_valid_passports.py b/day04/count_valid_passports.py
new file mode 100644 (file)
index 0000000..3ea48a8
--- /dev/null
@@ -0,0 +1,36 @@
+#!/usr/bin/python3
+
+REQUIRED_FIELDS=(('byr', "Birth Year"),('iyr', "Issue Year"),('eyr', "Expiration Year"),('hgt', "Height"),('hcl', "Hair Colour"),('ecl', "Eye Colour"),('pid',"Passport ID"))
+OPTIONAL_FIELDS=(('cid', "Country ID"))
+
+def check_passport(passport_data):
+    print(passport_data)
+    for field in REQUIRED_FIELDS:
+        if field[0] not in passport_data:
+            return False
+    return True
+
+def main():
+    new_passport=False
+    passport_data=dict()
+    valid_passports=0
+    for line in open("input.txt"):
+        if new_passport:
+            if check_passport(passport_data):
+                valid_passports += 1
+            passport_data.clear()
+        line=line.rstrip()
+        if line == '':
+            new_passport=True
+            continue
+        new_passport=False
+        parts=line.split(" ")
+        for part in parts:
+            (key, value) = part.split(":")
+            passport_data[key] = value
+    if not new_passport:
+        if check_passport(passport_data):
+            valid_passports += 1
+    print("Found", valid_passports, "valid passports")
+
+main()