@@ -21,7 +21,7 @@ def __init__(self, fields: Dict[str, str]) -> None:
21
21
@property
22
22
def has_required_fields (self ) -> bool :
23
23
"""Checks whether all required fields are present"""
24
- required = [' byr' , ' iyr' , ' eyr' , ' hgt' , ' hcl' , ' ecl' , ' pid' ]
24
+ required = [" byr" , " iyr" , " eyr" , " hgt" , " hcl" , " ecl" , " pid" ]
25
25
for key in required :
26
26
if key not in self .fields :
27
27
return False
@@ -31,67 +31,69 @@ def has_required_fields(self) -> bool:
31
31
@property
32
32
def has_valid_fields (self ) -> bool :
33
33
"""Checks whether the required fields are valid according to the validation rules"""
34
- return (self .has_required_fields and
35
- self .has_valid_birth_year and
36
- self .has_valid_issue_year and
37
- self .has_valid_expiration_year and
38
- self .has_valid_height and
39
- self .has_valid_hair_color and
40
- self .has_valid_eye_color and
41
- self .has_valid_passport_id )
34
+ return (
35
+ self .has_required_fields
36
+ and self .has_valid_birth_year
37
+ and self .has_valid_issue_year
38
+ and self .has_valid_expiration_year
39
+ and self .has_valid_height
40
+ and self .has_valid_hair_color
41
+ and self .has_valid_eye_color
42
+ and self .has_valid_passport_id
43
+ )
42
44
43
45
@property
44
46
def has_valid_birth_year (self ) -> bool :
45
47
"""Birth Year should be at least 1920 and at most 2002"""
46
- return parse_int (self .fields [' byr' ]) in range (1920 , 2003 )
48
+ return parse_int (self .fields [" byr" ]) in range (1920 , 2003 )
47
49
48
50
@property
49
51
def has_valid_issue_year (self ) -> bool :
50
52
"""Issue Year should be at least 2010 and at most 2020"""
51
- return parse_int (self .fields [' iyr' ]) in range (2010 , 2021 )
53
+ return parse_int (self .fields [" iyr" ]) in range (2010 , 2021 )
52
54
53
55
@property
54
56
def has_valid_expiration_year (self ) -> bool :
55
57
"""Expiration Year should be at least 2020 and at most 2030"""
56
- return parse_int (self .fields [' eyr' ]) in range (2020 , 2031 )
58
+ return parse_int (self .fields [" eyr" ]) in range (2020 , 2031 )
57
59
58
60
@property
59
61
def has_valid_height (self ) -> bool :
60
62
"""Height should be at least 150cm and at most 193cm; or at least 59in and at most 76in"""
61
- height = self .fields [' hgt' ]
62
- if height .endswith ('cm' ):
63
- return parse_int (height .strip ('cm' )) in range (150 , 194 )
63
+ height = self .fields [" hgt" ]
64
+ if height .endswith ("cm" ):
65
+ return parse_int (height .strip ("cm" )) in range (150 , 194 )
64
66
65
- if height .endswith ('in' ):
66
- return parse_int (height .strip ('in' )) in range (59 , 77 )
67
+ if height .endswith ("in" ):
68
+ return parse_int (height .strip ("in" )) in range (59 , 77 )
67
69
68
70
return False
69
71
70
72
@property
71
73
def has_valid_hair_color (self ) -> bool :
72
74
"""Hair Color should contain a valid hexadecimal number"""
73
- if re .match (' ^#[0-9a-f]{6}$' , self .fields [' hcl' ]):
75
+ if re .match (" ^#[0-9a-f]{6}$" , self .fields [" hcl" ]):
74
76
return True
75
77
return False
76
78
77
79
@property
78
80
def has_valid_eye_color (self ) -> bool :
79
81
"""Eye color should be one of the defined values"""
80
- return self .fields [' ecl' ] in [' amb' , ' blu' , ' brn' , ' gry' , ' grn' , ' hzl' , ' oth' ]
82
+ return self .fields [" ecl" ] in [" amb" , " blu" , " brn" , " gry" , " grn" , " hzl" , " oth" ]
81
83
82
84
@property
83
85
def has_valid_passport_id (self ) -> bool :
84
86
"""Passport ID should be a nine-digit number, including leading zeroes"""
85
- return len (self .fields [' pid' ]) == 9 and self .fields [' pid' ].isdigit ()
87
+ return len (self .fields [" pid" ]) == 9 and self .fields [" pid" ].isdigit ()
86
88
87
89
88
90
def parse_passport (data : str ) -> Passport :
89
- pairs = [pair .split (':' ) for pair in data .split ()]
91
+ pairs = [pair .split (":" ) for pair in data .split ()]
90
92
fields = dict (pairs )
91
93
return Passport (fields )
92
94
93
95
94
- with open (' 2020/input/day04.txt' , encoding = ' utf8' ) as file :
96
+ with open (" 2020/input/day04.txt" , encoding = " utf8" ) as file :
95
97
passport_data = file .read ().split (os .linesep + os .linesep )
96
98
passports = [parse_passport (lines ) for lines in passport_data ]
97
99
0 commit comments