-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday_04_2.py
More file actions
61 lines (53 loc) · 1.8 KB
/
day_04_2.py
File metadata and controls
61 lines (53 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
with open('inputs/input_04.txt', 'r') as infile:
passport_list = infile.read().split('\n\n')
required_fields = ['byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid']
def check_field(field: str, value: str) -> bool:
try:
if field == 'byr':
value = int(value)
return 1920 <= value <= 2002
elif field == 'iyr':
value = int(value)
return 2010 <= value <= 2020
elif field == 'eyr':
value = int(value)
return 2020 <= value <= 2030
elif field == 'hgt':
if value[-2:] == 'cm':
height = int(value[:-2])
return 150 <= height <= 193
elif value[-2:] == 'in':
height = int(value[:-2])
return 59 <= height <= 76
else:
return False
elif field == 'hcl':
return (len(value) == 7
and value[0] == '#'
and value[1:].isalnum()
and (value[1:].islower() or value[1:].isnumeric()))
elif field == 'ecl':
return value in ['amb', 'blu', 'brn', 'gry', 'grn', 'hzl', 'oth']
elif field == 'pid':
return value.isnumeric() and len(value) == 9
elif field == 'cid':
return True
else:
return False
except ValueError:
return False
valid_passports = 0
for passport in passport_list:
valid = 1
# check if all required fields exist
for field in required_fields:
valid *= passport.count(field)
if not valid:
continue
# check if the fields are valid
for element in passport.split():
field, value = element.split(':')
valid *= check_field(field, value)
if valid == 1:
valid_passports += 1
print(valid_passports)