]> git.sommitrealweird.co.uk Git - advent-of-code-2020.git/blob - day04/count_valid_passports.py
Day 14 - implemented in bash, because, well, I could
[advent-of-code-2020.git] / day04 / count_valid_passports.py
1 #!/usr/bin/python3
2
3 REQUIRED_FIELDS=(('byr', "Birth Year"),('iyr', "Issue Year"),('eyr', "Expiration Year"),('hgt', "Height"),('hcl', "Hair Colour"),('ecl', "Eye Colour"),('pid',"Passport ID"))
4 OPTIONAL_FIELDS=(('cid', "Country ID"))
5
6 def check_passport(passport_data):
7     print(passport_data)
8     for field in REQUIRED_FIELDS:
9         if field[0] not in passport_data:
10             return False
11     return True
12
13 def main():
14     new_passport=False
15     passport_data=dict()
16     valid_passports=0
17     for line in open("input.txt"):
18         if new_passport:
19             if check_passport(passport_data):
20                 valid_passports += 1
21             passport_data.clear()
22         line=line.rstrip()
23         if line == '':
24             new_passport=True
25             continue
26         new_passport=False
27         parts=line.split(" ")
28         for part in parts:
29             (key, value) = part.split(":")
30             passport_data[key] = value
31     if not new_passport:
32         if check_passport(passport_data):
33             valid_passports += 1
34     print("Found", valid_passports, "valid passports")
35
36 main()